Protocols

The following protocols are available globally.

  • A TypeSafeCredentials plugin for HTTP basic authentication. This protocol will be implemented by a Swift object defined by the user. The plugin must implement a verifyPassword function which takes a username and password as input and returns an instance of Self on success or nil on failure. This instance must contain the authentication provider (defaults to “HTTPBasic”) and an id, uniquely identifying the user. The users object can then be used in TypeSafeMiddlware routes to authenticate with HTTP basic.

    Usage Example:

    public struct MyHTTPBasic: TypeSafeHTTPBasic {
    
       public var id: String
    
       static let users = ["John" : "12345", "Mary" : "qwerasdf"]
    
       public static let realm = "Login message"
    
       public static func verifyPassword(username: String, password: String, callback: @escaping (MyHTTPBasic?) -> Void) {
           if let storedPassword = users[username], storedPassword == password {
               callback(MyHTTPBasic(id: username))
           } else {
               callback(nil)
           }
       }
    }
    
    struct User: Codable {
       let name: String
    }
    
    router.get("/protected") { (authedUser: MyHTTPBasic, respondWith: (User?, RequestError?) -> Void) in
       let user = User(name: authedUser.id)
       respondWith(user, nil)
    }
    
    See more

    Declaration

    Swift

    public protocol TypeSafeHTTPBasic : TypeSafeCredentials