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

Use RouterController #127

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions auth-cognito/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.10
// swift-tools-version:6.0
import PackageDescription

let package = Package(
Expand All @@ -7,9 +7,9 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.2.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/adam-fowler/soto-cognito-authentication-kit.git", from: "5.0.0-rc.3"),
.package(url: "https://github.com/adam-fowler/soto-cognito-authentication-kit.git", from: "5.0.0-rc.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion auth-cognito/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func buildApplication(
UserController(
cognitoAuthenticatable: authenticatable,
cognitoIdentityProvider: cognitoIdentityProvider
).endpoints
)
}

var app = Application(router: router)
Expand Down
10 changes: 5 additions & 5 deletions auth-cognito/Sources/App/Controllers/UserController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import HummingbirdRouter
import SotoCognitoAuthenticationKit
import SotoCognitoAuthenticationSRP

extension CognitoAccessToken: ResponseEncodable {}
extension CognitoAuthenticateResponse: ResponseEncodable {}
extension CognitoCreateUserResponse: ResponseEncodable {}
extension CognitoAccessToken: @retroactive ResponseEncodable {}
extension CognitoAuthenticateResponse: @retroactive ResponseEncodable {}
extension CognitoCreateUserResponse: @retroactive ResponseEncodable {}

struct UserController {
struct UserController: RouterController {
typealias Context = AuthCognitoRequestContext

let cognitoAuthenticatable: CognitoAuthenticatable
let cognitoIdentityProvider: CognitoIdentityProvider

var endpoints: some RouterMiddleware<Context> {
var body: some RouterMiddleware<Context> {
RouteGroup("user") {
Put(handler: self.create)
Patch(handler: self.resend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Hummingbird
import HummingbirdAuth
import SotoCognitoAuthenticationKit

extension CognitoAuthenticateResponse: Authenticatable {}
extension CognitoAccessToken: Authenticatable {}
extension CognitoAuthenticateResponse: @retroactive Authenticatable {}
extension CognitoAccessToken: @retroactive Authenticatable {}

/// Authenticator for Cognito username and password
struct CognitoBasicAuthenticator: AuthenticatorMiddleware {
Expand Down
2 changes: 1 addition & 1 deletion webauthn/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.2.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-rc.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta.2"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0-beta.1"),
Expand Down
4 changes: 2 additions & 2 deletions webauthn/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func buildApplication(_ arguments: AppArguments) async throws -> some Applicatio
mustacheLibrary: library,
fluent: fluent,
webAuthnSessionAuthenticator: webAuthnSessionAuthenticator
).endpoints
)
RouteGroup("api") {
HBWebAuthnController(
webauthn: .init(
Expand All @@ -88,7 +88,7 @@ func buildApplication(_ arguments: AppArguments) async throws -> some Applicatio
),
fluent: fluent,
webAuthnSessionAuthenticator: webAuthnSessionAuthenticator
).endpoints
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions webauthn/Sources/App/Controllers/HTMLController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct RedirectMiddleware<Context: AuthRequestContext>: RouterMiddleware {
}

/// Serves HTML pages
struct HTMLController {
struct HTMLController: RouterController {
typealias Context = WebAuthnRequestContext

let homeTemplate: MustacheTemplate
Expand All @@ -56,7 +56,7 @@ struct HTMLController {
}

// return Route for home page
var endpoints: some RouterMiddleware<Context> {
var body: some RouterMiddleware<Context> {
Get("/") {
self.webAuthnSessionAuthenticator
RedirectMiddleware(to: "/login.html")
Expand Down
9 changes: 7 additions & 2 deletions webauthn/Sources/App/Controllers/WebAuthnController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import HummingbirdFluent
import HummingbirdRouter
import WebAuthn

struct HBWebAuthnController {
struct HBWebAuthnController: RouterController {
typealias Context = WebAuthnRequestContext

let webauthn: WebAuthnManager
let fluent: Fluent
let webAuthnSessionAuthenticator: SessionAuthenticator<Context, UserRepository>

// return RouteGroup with user login endpoints
var endpoints: some RouterMiddleware<Context> {
var body: some RouterMiddleware<Context> {
/// Authenticator storing the WebAuthn session state
let webAuthnSessionStateAuthenticator = SessionAuthenticator(
sessionStorage: self.webAuthnSessionAuthenticator.sessionStorage,
Expand Down Expand Up @@ -168,5 +168,10 @@ struct HBWebAuthnController {
}
}

#if compiler(>=6.0)
extension PublicKeyCredentialCreationOptions: @retroactive ResponseEncodable {}
extension PublicKeyCredentialRequestOptions: @retroactive ResponseEncodable {}
#else
extension PublicKeyCredentialCreationOptions: ResponseEncodable {}
extension PublicKeyCredentialRequestOptions: ResponseEncodable {}
#endif
12 changes: 10 additions & 2 deletions webauthn/Sources/App/Extensions/Fluent+Mustache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Mustache
/// to access the wrappedValue. In the mustache template you would access this with
/// `{{wrappedValue(_myProperty)}}`. Note the `_` prefix on the property name. This is
/// required as this is how property wrappers appear in the Mirror reflection data.
extension FieldProperty: MustacheTransformable {
extension FieldProperty {
public func transform(_ name: String) -> Any? {
switch name {
case "wrappedValue":
Expand All @@ -20,7 +20,7 @@ extension FieldProperty: MustacheTransformable {
/// to access the wrappedValue. In the mustache template you would access this with
/// `{{wrappedValue(_myID)}}`. Note the `_` prefix on the property name. This is
/// required as this is how property wrappers appear in the Mirror reflection data.
extension IDProperty: MustacheTransformable {
extension IDProperty {
public func transform(_ name: String) -> Any? {
switch name {
case "wrappedValue":
Expand All @@ -30,3 +30,11 @@ extension IDProperty: MustacheTransformable {
}
}
}

#if compiler(>=6.0)
extension FieldProperty: @retroactive MustacheTransformable {}
extension IDProperty: @retroactive MustacheTransformable {}
#else
extension FieldProperty: MustacheTransformable {}
extension IDProperty: MustacheTransformable {}
#endif