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
#!/usr/bin/env bash | |
VERSION="3.0.1" | |
echo "Swift $VERSION Continuous Integration"; | |
# Determine OS | |
UNAME=`uname`; | |
if [[ $UNAME == "Darwin" ]]; | |
then | |
OS="macos"; |
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
import Vapor | |
import VaporPostgreSQL | |
import Fluent | |
final class User { | |
var id: Node? | |
var username: String | |
var password: String | |
init(username: String, password: String) { |
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
static func authenticate(credentials: Credentials) throws -> Auth.User { | |
switch credentials { | |
case let id as Identifier: | |
guard let user = try User.find(id.id) else { | |
throw Abort.custom(status: .forbidden, message: "Invalid user identifier.") | |
} | |
return user | |
case let usernamePassword as UsernamePassword: |
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
import Auth | |
import BCrypt | |
final class StandardCredentials: Credentials { | |
var username: String | |
var password: String | |
init(username: String, password: String) { | |
self.username = username | |
self.password = BCrypt.hash(password: password) | |
} | |
} |
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
import Auth | |
import BCrypt | |
final class StandardCredentials: Credentials { | |
var username: String | |
var password: String | |
} |
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
func adminLogin(_ request: Request)throws -> ResponseRepresentable { | |
guard let username = request.data["username"]?.string, | |
let password = request.data["password"]?.string else { | |
throw Abort.badRequest | |
} | |
let credentials = UsernamePassword(username: username, password: password) | |
do { | |
try request.auth.login(credentials, persist: true) | |
return Response(redirect: "/admin/new-post") |
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
case let id as Identifier: | |
guard let user = try User.find(id.id) else { | |
throw Abort.custom(status: .forbidden, message: "Invalid user identifier.") | |
} | |
return user | |
case let accessToken as AccessToken: | |
guard let user = try User.query().filter("access_token", accessToken.string).first() else { | |
throw Abort.custom(status: .forbidden, message: "Invalid access token.") | |
} |
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
if try BCrypt.verify(password: usernamePassword.password, matchesHash: user.password) { | |
return user | |
} else { | |
throw Abort.custom(status: .networkAuthenticationRequired, message: "Invalid user name or password.") | |
} |
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
case let apiKey as APIKey: | |
guard let user = try User.query().filter("username", apiKey.id).first(), | |
try BCrypt.verify(password: apiKey.secret, matchesHash: user.password) else { | |
throw Abort.custom(status: .networkAuthenticationRequired, message: "Invalid user name or password.") | |
} |
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
static func authenticate(credentials: Credentials) throws -> Auth.User { | |
switch credentials { | |
default: | |
let type = type(of: credentials) | |
throw Abort.custom(status: .forbidden, message: "Unsupported credential type: \(type).") | |
} | |
} |
OlderNewer