This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from nodes-vapor/feature/request-creatable
Submissions related update
- Loading branch information
Showing
15 changed files
with
248 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/// Types conforming to this protocol can be used for login or register requests. | ||
public protocol HasReadablePassword: Decodable { | ||
public protocol HasReadablePassword { | ||
/// Key path to the readable password. | ||
static var readablePasswordKey: KeyPath<Self, String> { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import Authentication | ||
import Vapor | ||
|
||
/// Expresses the ability for a type (e.g. a user type) to be logged in. | ||
public protocol Loginable { | ||
|
||
/// Payload containing the information required to perform a login. | ||
associatedtype Login: HasReadablePassword | ||
|
||
/// Used to perform any pre-login steps such as validation. | ||
/// | ||
/// - Parameter req: the request. | ||
/// - Returns: a `Future<Void>` signaling success or failure. | ||
static func preLogin(on req: Request) -> Future<Void> | ||
|
||
/// Performs the actual login. Called after `preLogin(on:)`. | ||
/// | ||
/// - Parameters: | ||
/// - login: the login information. | ||
/// - worker: a worker on which to perform any database related actions like user lookup. | ||
/// - Returns: an instance of self in the `Future` on successful login. | ||
static func logIn(with login: Login, on worker: DatabaseConnectable) -> Future<Self> | ||
} | ||
|
||
extension Loginable { | ||
|
||
/// Default implementation that does nothing. | ||
/// See `Loginable`. | ||
public static func preLogin(on req: Request) -> Future<Void> { | ||
return req.future() | ||
} | ||
} | ||
|
||
extension Loginable where | ||
Self: PasswordAuthenticatable, | ||
Self.Login: HasReadableUsername | ||
{ | ||
/// Default implementation that uses functionality provided by `PasswordAuthenticatable`. | ||
/// See `Loginable`. | ||
public static func logIn(with login: Login, on worker: DatabaseConnectable) -> Future<Self> { | ||
return Self | ||
.authenticate( | ||
username: login[keyPath: Login.readableUsernameKey], | ||
password: login[keyPath: Login.readablePasswordKey], | ||
using: BCrypt, | ||
on: worker | ||
) | ||
.unwrap(or: AuthenticationError.userNotFound) | ||
} | ||
} | ||
|
||
extension Loginable where Self.Login: Decodable { | ||
|
||
/// Convenience function that combines `preLogin` and `logIn`. | ||
/// | ||
/// - Parameter req: the request. | ||
/// - Returns: an instance of self in the `Future` on successful login. | ||
public static func logIn(on req: Request) -> Future<Self> { | ||
return preLogin(on: req) | ||
.flatMap { | ||
try req.content.decode(Login.self) | ||
} | ||
.flatMap(to: Self.self) { login in | ||
Self.logIn(with: login, on: req) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.