Swiftpack.co -  nodes-vapor/keychain as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nodes-vapor/keychain
Easily scaffold a keychain using JWT for Vapor โ›“
.package(url: "https://github.com/nodes-vapor/keychain.git", from: "2.0.0-rc.3")

Keychain โ›“

Swift Version Vapor Version tests codebeat badge Readme Score GitHub license

Add a complete and customizable user authentication system for your API project.

๐Ÿ“ฆ Installation

Update your Package.swift file.

.package(url: "https://github.com/nodes-vapor/keychain.git", from: "1.0.1")
targets: [
    .target(
        name: "App",
        dependencies: [
            ...
            .product(name: "Keychain", package: "keychain"),
        ]
    ),
    ...
]

Usage

Setup

app.keychain.configure(
    signer: JWTSigner(
        algorithm: TestJWTAlgorithm(name: UserAccessKeychainConfig.jwkIdentifier.string)
    ),
    config: UserAccessKeychainConfig()
)
app.keychain.configure(
    signer: JWTSigner(
        algorithm: TestJWTAlgorithm(name: UserRefreshKeychainConfig.jwkIdentifier.string)
    ),
    config: UserRefreshKeychainConfig()
)
app.keychain.configure(
    signer: JWTSigner(
        algorithm: TestJWTAlgorithm(name: UserResetKeychainConfig.jwkIdentifier.string)
    ),
    config: UserResetKeychainConfig()
)

JWTPayload

import JWT
import Keychain
import Vapor

struct UserJWTPayload: KeychainPayload {
    let exp: ExpirationClaim
    let sub: SubjectClaim

    init(expirationDate: Date, user: User) {
        self.exp = .init(value: expirationDate)
        self.sub = .init(value: user.id)
    }

    func findUser(request: Request) -> EventLoopFuture<User> {
        request.eventLoop.future(request.testUser).unwrap(or: TestError.userNotFound)
    }

    func verify(using signer: JWTSigner) throws {
        // don't verify anything since we're not testing the JWT package itself
    }
}

KeychainConfigs

import JWT
import Keychain

struct UserAccessKeychainConfig: KeychainConfig, Equatable {
    typealias JWTPayload = UserJWTPayload

    static var jwkIdentifier: JWKIdentifier = "access"

    let expirationTimeInterval: TimeInterval = 300
}

struct UserRefreshKeychainConfig: KeychainConfig, Equatable {
    typealias JWTPayload = UserJWTPayload

    static var jwkIdentifier: JWKIdentifier = "refresh"

    let expirationTimeInterval: TimeInterval = 600
}

struct UserResetKeychainConfig: KeychainConfig, Equatable {
    typealias JWTPayload = UserJWTPayload

    static var jwkIdentifier: JWKIdentifier = "reset"

    let expirationTimeInterval: TimeInterval = 400
}

UserController

import Keychain

struct UserController {
    let currentDate: () -> Date

    func login(request: Request) -> EventLoopFuture<AuthenticationResponse<UserResponse>> {
        UserLoginRequest
            .logIn(
                on: request,
                errorOnWrongPassword: TestError.incorrectCredentials,
                currentDate: currentDate()
            ).map { $0.map(UserResponse.init) }
    }

    func register(request: Request) -> EventLoopFuture<AuthenticationResponse<UserResponse>> {
        UserRegisterRequest
            .register(
                on: request,
                currentDate: currentDate()
            ).map {
                request.testUser = $0.user
                return $0.map(UserResponse.init)
            }
    }

    func forgotPassword(request: Request) -> EventLoopFuture<HTTPStatus> {
        UserForgotPasswordRequest
            .sendToken(on: request, currentDate: currentDate())
            .transform(to: .accepted)
    }

    func resetPassword(request: Request) -> EventLoopFuture<HTTPStatus> {
        UserResetPasswordRequest
            .updatePassword(on: request)
            .map { request.testUser = $0}
            .transform(to: .ok)
    }

    func refreshToken(request: Request) throws -> Response {
        let token = try UserRefreshKeychainConfig.makeToken(on: request, currentDate: currentDate())

        // here we encode the token string as JSON but you might include your token in a struct
        // conforming to `Content`
        let response = Response()
        try response.content.encode(token, as: .json)
        return response
    }

    func me(request: Request) throws -> UserResponse {
        try .init(user: request.auth.require(User.self))
    }
}

๐Ÿ† Credits

This package is developed and maintained by the Vapor team at Nodes.

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license

GitHub

link
Stars: 35
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jackโ€™d and SCRUFF. We are two of the worldโ€™s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Add workaround for Swift 5.3 bug
47 weeks ago

Fixes

  • Add workaround for Swift 5.3 bug

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics