Swiftpack.co -  Package - nicholascross/CognitoAuth
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nicholascross/CognitoAuth
Swift cognito SRP auth flow (experimental)
.package(url: "https://github.com/nicholascross/CognitoAuth.git", from: "0.1.0")

CognitoAuth

A simple cognito SRP authenticator. This was made mainly for testing purposes and may not be suitable for production usage. A better option for production usage is probably soto-cognito-authentication-kit if you don't mind the additional dependencies.

Usage example

import Foundation
import CognitoAuth

let config = AuthConfig(
    endpointURL: URL(string: "https://cognito-idp.ap-southeast-2.amazonaws.com")!,
    clientId: "theclientid",
    clientSecret: "thelcientsecret",
    poolId: "1Q2Q3Q4Q"
)

let username = "ausername"
let password = "apassword"
let service = AuthService(config: config, username: username)

public final class AuthDelegate: AuthServiceDelegate {
    public func authService(_ service: AuthService, authenticationSuccessful tokens: AuthTokens) {
        print("access: \(tokens.accessToken)")
        print("id: \(tokens.idToken)")
        print("refresh: \(tokens.refreshToken)")
    }

    public func authService(_ service: AuthService, authenticationFailedWithError error: Error) {
        print("failed: \(error)")
    }

    public func authService(_ service: AuthService, provideMFACode: (String) -> ()) {
        print("enter mfa code:")
        guard let code = readLine() else {
            print("code unavailable")
            return
        }
        provideMFACode(code)
    }
}

let delegate = AuthDelegate()

service.delegate = delegate

service.authenticateWithPassword(password)

Acknlowedgements

The hard part of this process was done by Adam Fowler. I cannot locate the original source repository.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

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