Protocols

The following protocols are available globally.

  • The protocol that defines the API for Credentials plugins for authentication of incoming requests.

    See more

    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 more

    Declaration

    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 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)
    }
    
    See more

    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 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()
            }
        }
    }
    
    See more

    Declaration

    Swift

    public protocol TypeSafeCredentials : TypeSafeMiddleware, Decodable, Encodable
  • A protocol for UserProfile manipulation. The current default implementation only tries to fill in the standard UserProfile fields. In case this default behaviour is insufficient, additional data can be stored in UserProfile.extendedProperties and filled in using this delegate. An implementation should be passed in the options argument with the key userProfileDelegate to the corresponding plugin’s constructor.

    See more

    Declaration

    Swift

    public protocol UserProfileDelegate