This is the Vapor wrapper for Soto Cognito Authentication Kit. It provides application storage for configurations and authentication calls on request. Documentation on Soto Cognito Authentication Kit can be found here
Using with Vapor
Configuration
Store your CognitoConfiguration on the Application object. In configure.swift add the following with your configuration details
Functions like createUser, signUp, authenticate with username and password and responseToChallenge are all accessed through request.application.cognito.authenticatable. The following login route will return the full response from CognitoAuthenticable.authenticate.
If id, access or refresh tokens are provided in the ‘Authorization’ header as Bearer tokens the following functions in Request can be used to verify them authenticate(idToken:), authenticate(accessToken:), refresh. as in the following
Three authenticators are available. See the Vapor docs for more details on authentication in Vapor.CognitoBasicAuthenticator will do username, password authentication and returns a CognitoAuthenticateResponse. CognitoAccessAuthenticator will do access token authentication and returns an CognitoAccessToken which holds all the information that could be extracted from the access token. CognitoIdAuthenticator<Payload> does id token authentication and extracts information from the id token into your own Payload type. The standard list of claims that can be found in an id token are detailed in the [OpenID spec] (https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims). Your Payload type needs to decode using these tags, the username tag “cognito:username” and any custom tags you may have setup for the user pool. Below is an example of using the id token authenticator.
First create a User type to store your id token payload in.
struct User: Content & Authenticatable {
let username: String
let email: String
private enum CodingKeys: String, CodingKey {
case username = "cognito:username"
case email = "email"
}
}
Add a route using the authenticator. The CognitoIdAuthenticator authenticates the request, the guardMiddleware ensures the user is authenticated. The actual function accesses the User type via req.auth.require.
app.grouped(CognitoIdAuthenticator<User>())
.grouped(User.guardMiddleware())
.get("user") { (req) throws -> EventLoopFuture<User> in
let user = try req.auth.require(User.self)
return req.eventLoop.next().makeSucceededFuture(user)
}
Soto Cognito Authentication
This is the Vapor wrapper for Soto Cognito Authentication Kit. It provides application storage for configurations and authentication calls on request. Documentation on Soto Cognito Authentication Kit can be found here
Using with Vapor
Configuration
Store your
CognitoConfiguration
on the Application object. In configure.swift add the following with your configuration detailsThe CognitoIdentity configuration can be setup in a similar way.
Accessing functionality
Functions like
createUser
,signUp
,authenticate
with username and password andresponseToChallenge
are all accessed throughrequest.application.cognito.authenticatable
. The following login route will return the full response fromCognitoAuthenticable.authenticate
.If id, access or refresh tokens are provided in the ‘Authorization’ header as Bearer tokens the following functions in Request can be used to verify them
authenticate(idToken:)
,authenticate(accessToken:)
,refresh
. as in the followingAuthenticators
Three authenticators are available. See the Vapor docs for more details on authentication in Vapor.
CognitoBasicAuthenticator
will do username, password authentication and returns aCognitoAuthenticateResponse
.CognitoAccessAuthenticator
will do access token authentication and returns anCognitoAccessToken
which holds all the information that could be extracted from the access token.CognitoIdAuthenticator<Payload>
does id token authentication and extracts information from the id token into your ownPayload
type. The standard list of claims that can be found in an id token are detailed in the [OpenID spec] (https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims). YourPayload
type needs to decode using these tags, the username tag “cognito:username” and any custom tags you may have setup for the user pool. Below is an example of using the id token authenticator.First create a User type to store your id token payload in.
Add a route using the authenticator. The
CognitoIdAuthenticator
authenticates the request, theguardMiddleware
ensures the user is authenticated. The actual function accesses theUser
type viareq.auth.require
.