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
Self
is returned. On failure, theHTTPStatusCode
and any response headers to be set are returned. On skip (Meaning the plugin didn’t recognize the authentication header), theHTTPStatusCode
and 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
request
The
RouterRequest
object used to get information about the request.response
The
RouterResponse
object used to respond to the request.onSuccess
The closure to invoke in the case of successful authentication.
onFailure
The closure to invoke in the case of an authentication failure.
onSkip
The 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
Self
by callingauthenticate
. On success, thisSelf
instance is returned so it can be used by aTypeSafeMiddleware
route. On failure, an unauthorized response is sent immediately. If the authentication header isn’t recognised,RequestError.unauthorized
is returned to theTypeSafeMiddleware
route. 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
request
The
RouterRequest
object used to get information about the request.response
The
RouterResponse
object used to respond to the request.completion
The closure to invoke once middleware processing is complete. Either an instance of
Self
or aRequestError
should be provided, indicating a successful or failed attempt to authenticate the request.