Protocols
The following protocols are available globally.
-
The protocol that defines the API for
See moreCredentials
plugins for authentication of incoming requests.Declaration
Swift
public protocol CredentialsPluginProtocol
-
Protocol to make it easier to add token TTL (Time To Live) to credentials plugins.
Using this protocol:
Step 1) Conform to the protocol Step 2) Call one of the two getProfileAndCacheIfNeeded methods– probably at the end of your authenticate method:
Either: Step 2a) Typical plugins will call the getProfileAndCacheIfNeeded method with the onSuccess and onFailure closures. I.e., typical plugins will either simply fail or succeed when attempting to generate a user profile when generateNewProfile is called. E.g., see https://github.com/crspybits/CredentialsMicrosoft/blob/master/Sources/CredentialsMicrosoft/CredentialsMicrosoftToken.swift
Or: Step 2b) More complicated plugins will call the getProfileAndCacheIfNeeded method with the single, completion, closure. These plugins (e.g., see https://github.com/Kitura/Kitura-CredentialsJWT/blob/master/Sources/CredentialsJWT/CredentialsJWT.swift) not only either succeed or fail, but they can have a third, unprocessable result.
See moreDeclaration
Swift
public protocol CredentialsTokenTTL : AnyObject
-
A
TypeSafeMiddleware
protocol for using multiple authentication methods on a Codable route. An object conforming to this protocol must contain a static array of the acceptableTypeSafeCredentials
types and be initializable from the authentication instance that succeeded. If an authentication fails or you reach the end of your array, an unauthorized response is sent.Usage Example:
See morepublic final class AuthedUser: TypeSafeMultiCredentials { public let id: String public let provider: String public let name: String? } extension TypeSafeMultiCredentials { static let authenticationMethods: [TypeSafeCredentials.Type] = [MyBasicAuth.self, GoogleTokenProfile.self] init(successfulAuth: TypeSafeCredentials) { self.id = successfulAuth.id self.provider = successfulAuth.provider } } router.get("/protected") { (authedUser: AuthedUser, respondWith: (AuthedUser?, RequestError?) -> Void) in print("user: \(authedUser.id) successfully authenticated using: \(authedUser.provider)") respondWith(authedUser, nil) }
Declaration
Swift
public protocol TypeSafeMultiCredentials : TypeSafeCredentials
-
A
TypeSafeMiddleware
for authenticating users. This protocol is implemented by plugins that identify the user using information supplied by theRouterRequest
. The plugin must implement a staticauthenticate
function which returns an instance ofSelf
on success. This instance defines the name of the authentication provider (e.g. “HTTPBasic”), and anid
that uniquely identifies a user for that provider.Usage Example:
See morepublic final class TypeSafeHTTPBasic : TypeSafeCredentials { public let id: String public let provider: String = "HTTPBasic" private static let users = ["John" : "123"] public static func authenticate(request: RouterRequest, response: RouterResponse, onSuccess: @escaping (TypeSafeHTTPBasic) -> Void, onFailure: @escaping (HTTPStatusCode?, [String : String]?) -> Void, onSkip: @escaping (HTTPStatusCode?, [String : String]?) -> Void { if let user = request.urlURL.user, let password = request.urlURL.password { if users[user] == password { return onSuccess(UserHTTPBasic(id: user)) } else { return onFailure() } } else { return onSkip() } } }
Declaration
Swift
public protocol TypeSafeCredentials : TypeSafeMiddleware, Decodable, Encodable
-
A protocol for
See moreUserProfile
manipulation. The current default implementation only tries to fill in the standardUserProfile
fields. In case this default behaviour is insufficient, additional data can be stored inUserProfile.extendedProperties
and filled in using this delegate. An implementation should be passed in theoptions
argument with the keyuserProfileDelegate
to the corresponding plugin’s constructor.Declaration
Swift
public protocol UserProfileDelegate