Skip to content

Commit

Permalink
Rename HBAuthRequestContext types (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Jan 31, 2024
1 parent 4f04aab commit 9867667
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import NIOCore

/// Protocol that all request contexts should conform to if they want to support
/// authentication middleware
public protocol HBAuthRequestContextProtocol: HBRequestContext {
public protocol HBAuthRequestContext: HBRequestContext {
/// Login cache
var auth: HBLoginCache { get set }
}

/// Implementation of a basic request context that supports everything the Hummingbird library needs
public struct HBAuthRequestContext: HBAuthRequestContextProtocol {
public struct HBBasicAuthRequestContext: HBAuthRequestContext {
/// core context
public var coreContext: HBCoreRequestContext
/// Login cache
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdAuth/Authenticator/Authenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public protocol HBAuthenticatable: Sendable {}
/// }
/// }
/// ```
public protocol HBAuthenticator: HBMiddlewareProtocol where Context: HBAuthRequestContextProtocol {
public protocol HBAuthenticator: HBMiddlewareProtocol where Context: HBAuthRequestContext {
/// type to be authenticated
associatedtype Value: HBAuthenticatable
/// Called by middleware to see if request can authenticate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Hummingbird

/// Middleware returning 404 for unauthenticated requests
public struct IsAuthenticatedMiddleware<Auth: HBAuthenticatable, Context: HBAuthRequestContextProtocol>: HBMiddlewareProtocol {
public struct IsAuthenticatedMiddleware<Auth: HBAuthenticatable, Context: HBAuthRequestContext>: HBMiddlewareProtocol {
public init(_: Auth.Type) {}

public func handle(_ request: HBRequest, context: Context, next: (HBRequest, Context) async throws -> HBResponse) async throws -> HBResponse { guard context.auth.has(Auth.self) else { throw HBHTTPError(.unauthorized) }
Expand Down
20 changes: 10 additions & 10 deletions Tests/HummingbirdAuthTests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class AuthTests: XCTestCase {
}

func testBearer() async throws {
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.get { request, _ -> String? in
return request.headers.bearer?.token
}
Expand All @@ -67,7 +67,7 @@ final class AuthTests: XCTestCase {
}

func testBasic() async throws {
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.get { request, _ -> String? in
return request.headers.basic.map { "\($0.username):\($0.password)" }
}
Expand All @@ -82,7 +82,7 @@ final class AuthTests: XCTestCase {

func testBcryptThread() async throws {
let persist = HBMemoryPersistDriver()
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.put { request, _ -> HTTPResponse.Status in
guard let basic = request.headers.basic else { throw HBHTTPError(.unauthorized) }
let hash = try await NIOThreadPool.singleton.runIfActive {
Expand Down Expand Up @@ -118,7 +118,7 @@ final class AuthTests: XCTestCase {
struct User: HBAuthenticatable {
let name: String
}
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.get { _, context -> HTTPResponse.Status in
var context = context
context.auth.login(User(name: "Test"))
Expand All @@ -142,12 +142,12 @@ final class AuthTests: XCTestCase {
struct User: HBAuthenticatable {
let name: String
}
struct HBTestAuthenticator<Context: HBAuthRequestContextProtocol>: HBAuthenticator {
struct HBTestAuthenticator<Context: HBAuthRequestContext>: HBAuthenticator {
func authenticate(request: HBRequest, context: Context) async throws -> User? {
User(name: "Adam")
}
}
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.middlewares.add(HBTestAuthenticator())
router.get { _, context -> HTTPResponse.Status in
guard context.auth.has(User.self) else { return .unauthorized }
Expand All @@ -166,12 +166,12 @@ final class AuthTests: XCTestCase {
struct User: HBAuthenticatable {
let name: String
}
struct HBTestAuthenticator<Context: HBAuthRequestContextProtocol>: HBAuthenticator {
struct HBTestAuthenticator<Context: HBAuthRequestContext>: HBAuthenticator {
func authenticate(request: HBRequest, context: Context) async throws -> User? {
User(name: "Adam")
}
}
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
router.group()
.add(middleware: HBTestAuthenticator())
.add(middleware: IsAuthenticatedMiddleware(User.self))
Expand Down Expand Up @@ -199,14 +199,14 @@ final class AuthTests: XCTestCase {
struct User: HBAuthenticatable {
let name: String
}
struct MySessionAuthenticator<Context: HBAuthRequestContextProtocol>: HBSessionAuthenticator {
struct MySessionAuthenticator<Context: HBAuthRequestContext>: HBSessionAuthenticator {
let sessionStorage: HBSessionStorage

func getValue(from session: Int, request: HBRequest, context: Context) async throws -> User? {
return User(name: "Adam")
}
}
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
let persist = HBMemoryPersistDriver()
let sessions = HBSessionStorage(persist)

Expand Down
8 changes: 4 additions & 4 deletions Tests/HummingbirdAuthTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ final class SessionTests: XCTestCase {
struct User: HBAuthenticatable {
let name: String
}
struct MySessionAuthenticator<Context: HBAuthRequestContextProtocol>: HBSessionAuthenticator {
struct MySessionAuthenticator<Context: HBAuthRequestContext>: HBSessionAuthenticator {
let sessionStorage: HBSessionStorage
func getValue(from session: Int, request: HBRequest, context: Context) async throws -> User? {
return User(name: "Adam")
}
}
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
let persist = HBMemoryPersistDriver()
let sessions = HBSessionStorage(persist)
router.put("session") { _, _ -> HBResponse in
Expand Down Expand Up @@ -64,7 +64,7 @@ final class SessionTests: XCTestCase {
let name: String
}

let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
let persist = HBMemoryPersistDriver()
let sessions = HBSessionStorage(persist)
router.post("save") { request, _ -> HBResponse in
Expand Down Expand Up @@ -105,7 +105,7 @@ final class SessionTests: XCTestCase {
}

func testSessionUpdateError() async throws {
let router = HBRouter(context: HBAuthRequestContext.self)
let router = HBRouter(context: HBBasicAuthRequestContext.self)
let persist = HBMemoryPersistDriver()
let sessions = HBSessionStorage(persist)

Expand Down

0 comments on commit 9867667

Please sign in to comment.