TypeSafeMultiCredentials
public protocol TypeSafeMultiCredentials : TypeSafeCredentials
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 acceptable TypeSafeCredentials
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:
public 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)
}
-
An array of authentication types that conform to
TypeSafeCredentials
. Each type will be asked, in turn, to authenticate an incoming request.Declaration
Swift
static var authenticationMethods: [TypeSafeCredentials.Type] { get }
-
This initalizer creates an instance of the type conforming to
TypeSafeMultiCredentials
from a successfully authenticatedTypeSafeCredentials
instance.Usage Example:
init(successfulAuth: TypeSafeCredentials) { self.id = successfulAuth.id self.provider = successfulAuth.provider switch(successAuth.self) { case let googleProfile as GoogleTokenProfile: self.name = googleProfile.name default: self.name = nil } }
Declaration
Swift
init(successfulAuth: TypeSafeCredentials)
-
authenticate(request:
Extension methodresponse: onSuccess: onFailure: onSkip: ) Static function that iterates through the
authenticationMethods
array ofTypeSafeCredentials
types calling their authenticate function. If a type successfully authenticates and returns onSuccess(Self),TypeSafeMultiCredentials
will call init(successfulAuth:) with the successful instance. If a type matches its authorization header but fails to authenticate it returns onFailure() and an .unauthorized response is sent. If a type doesn’t match its authorization header then it returns onSkip() and the next authentication method is called.Declaration
Swift
public 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 plugin doesn’t recognize the authentication data in the request.