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 averifyPassword
function which takes a username and password as input and returns an instance ofSelf
on success ornil
on failure. This instance must contain the authenticationprovider
(defaults to “HTTPBasic”) and anid
, uniquely identifying the user. The users object can then be used in TypeSafeMiddlware routes to authenticate with HTTP basic.Usage Example:
See morepublic 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) }
Declaration
Swift
public protocol TypeSafeHTTPBasic : TypeSafeCredentials