Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication framework #21

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
swift format
  • Loading branch information
adam-fowler committed Jan 29, 2021
commit 8b04a45ae0bedae59ee490e74b34c972089030e1
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let package = Package(
.product(name: "Logging", package: "swift-log"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "ExtrasBase64", package: "swift-extras-base64")
.product(name: "ExtrasBase64", package: "swift-extras-base64"),
]),
.target(name: "HummingbirdFiles", dependencies: [
.byName(name: "Hummingbird"),
Expand Down
28 changes: 14 additions & 14 deletions Sources/Hummingbird/Authentication/Request+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ extension HBRequest {
public func login<Auth>(_ auth: Auth) {
var logins = self.loginCache ?? [:]
logins[ObjectIdentifier(Auth.self)] = auth
request.extensions.set(\.auth.loginCache, value: logins)
self.request.extensions.set(\.auth.loginCache, value: logins)
}

/// Logout type
/// - Parameter auth: authentication type
public func logout<Auth>(_ auth: Auth.Type) {
public func logout<Auth>(_: Auth.Type) {
if var logins = self.loginCache {
logins[ObjectIdentifier(Auth.self)] = nil
request.extensions.set(\.auth.loginCache, value: logins)
self.request.extensions.set(\.auth.loginCache, value: logins)
}
}

/// Return authenticated type
/// - Parameter auth: Type required
public func get<Auth>(_ auth: Auth.Type) -> Auth? {
return loginCache?[ObjectIdentifier(Auth.self)] as? Auth
public func get<Auth>(_: Auth.Type) -> Auth? {
return self.loginCache?[ObjectIdentifier(Auth.self)] as? Auth
}

/// Return if request is authenticated with type
/// - Parameter auth: Authentication type
public func has<Auth>(_ auth: Auth.Type) -> Bool {
return loginCache?[ObjectIdentifier(Auth.self)] != nil
public func has<Auth>(_: Auth.Type) -> Bool {
return self.loginCache?[ObjectIdentifier(Auth.self)] != nil
}
var loginCache: [ObjectIdentifier: Any]? { request.extensions.get(\.auth.loginCache) }

var loginCache: [ObjectIdentifier: Any]? { self.request.extensions.get(\.auth.loginCache) }

let request: HBRequest
}

/// Authentication object
public var auth: Auth { return .init(request: self) }
}
19 changes: 9 additions & 10 deletions Tests/HummingbirdTests/AuthenticationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ class AuthenticationTests: XCTestCase {
}
app.XCTStart()
defer { app.XCTStop() }

let basic = "adamfowler:testpassword"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
app.XCTExecute(uri: "/authenticate", method: .GET, headers: ["Authorization": basicHeader]) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), #"["username": "adamfowler", "password": "testpassword"]"#)
}

}

func testBearerAuthentication() {
Expand All @@ -28,7 +27,7 @@ class AuthenticationTests: XCTestCase {
}
app.XCTStart()
defer { app.XCTStop() }

app.XCTExecute(
uri: "/authenticate",
method: .GET,
Expand All @@ -38,33 +37,33 @@ class AuthenticationTests: XCTestCase {
XCTAssertEqual(String(buffer: body), "jh345jjefgi34rj")
}
}

func testAuthenticator() {
struct MyAuthenticator: HBAuthenticator {
func authenticate(request: HBRequest) -> EventLoopFuture<Void> {
guard let basic = request.auth.basic else { return request.success(Void()) }
if basic.username == "adamfowler" && basic.password == "password" {
guard let basic = request.auth.basic else { return request.success(()) }
if basic.username == "adamfowler", basic.password == "password" {
request.auth.login(basic)
}
return request.success(Void())
return request.success(())
}
}
let app = HBApplication(testing: .embedded, configuration: .init(maxUploadSize: 65536))
app.middleware.add(MyAuthenticator())
app.router.get("/authenticate") { request -> HTTPResponseStatus in
return request.auth.has(BasicAuthentication.self) ? .ok: .unauthorized
return request.auth.has(BasicAuthentication.self) ? .ok : .unauthorized
}
app.XCTStart()
defer { app.XCTStop() }

do {
let basic = "adamfowler:nopassword"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
app.XCTExecute(uri: "/authenticate", method: .GET, headers: ["Authorization": basicHeader]) { response in
XCTAssertEqual(response.status, .unauthorized)
}
}

do {
let basic = "adamfowler:password"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
Expand Down