TypeSafeCredentials
public protocol TypeSafeCredentials : TypeSafeMiddleware, Decodable, Encodable
A TypeSafeMiddleware for authenticating users. This protocol is implemented by plugins that identify the user using information supplied by the RouterRequest. The plugin must implement a static authenticate function which returns an instance of Self on success. This instance defines the name of the authentication provider (e.g. “HTTPBasic”), and an id that uniquely identifies a user for that provider.
Usage Example:
public 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()
}
}
}
-
An identifier that uniquely identifies a user within the context of an authentication provider.
Declaration
Swift
var id: String { get } -
The name of the authentication provider.
Declaration
Swift
var provider: String { get } -
Authenticate an incoming request. On success, an instance of
Selfis returned. On failure, theHTTPStatusCodeand any response headers to be set are returned. On skip (Meaning the plugin didn’t recognize the authentication header), theHTTPStatusCodeand any response headers to be set are returned.Declaration
Swift
static func authenticate (request: RouterRequest, response: RouterResponse, onSuccess: @escaping (Self) -> Void, onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void, onSkip: @escaping (HTTPStatusCode?, [String:String]?) -> Void )Parameters
requestThe
RouterRequestobject used to get information about the request.responseThe
RouterResponseobject used to respond to the request.onSuccessThe closure to invoke in the case of successful authentication.
onFailureThe closure to invoke in the case of an authentication failure.
onSkipThe closure to invoke when the request does not contain authentication data that this plugin recognises (such as a named token).
-
handle(request:Extension methodresponse: completion: ) Static function that attempts to create an instance of
Selfby callingauthenticate. On success, thisSelfinstance is returned so it can be used by aTypeSafeMiddlewareroute. On failure, an unauthorized response is sent immediately. If the authentication header isn’t recognised,RequestError.unauthorizedis returned to theTypeSafeMiddlewareroute. This means the current route will not be invoked but other routes can still be matched.Declaration
Swift
public static func handle(request: RouterRequest, response: RouterResponse, completion: @escaping (Self?, RequestError?) -> Void)Parameters
requestThe
RouterRequestobject used to get information about the request.responseThe
RouterResponseobject used to respond to the request.completionThe closure to invoke once middleware processing is complete. Either an instance of
Selfor aRequestErrorshould be provided, indicating a successful or failed attempt to authenticate the request.
View on GitHub
TypeSafeCredentials Protocol Reference