Skip to content

Instantly share code, notes, and snippets.

View calebkleveter's full-sized avatar
🔨
Build Succeeded

Caleb Kleveter calebkleveter

🔨
Build Succeeded
View GitHub Profile
@calebkleveter
calebkleveter / travis-build.sh
Created December 13, 2016 17:06
Modified version of Vapor Travis test that excludes test cases.
#!/usr/bin/env bash
VERSION="3.0.1"
echo "Swift $VERSION Continuous Integration";
# Determine OS
UNAME=`uname`;
if [[ $UNAME == "Darwin" ]];
then
OS="macos";
import Vapor
import VaporPostgreSQL
import Fluent
final class User {
var id: Node?
var username: String
var password: String
init(username: String, password: String) {
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:
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)
}
}
import Auth
import BCrypt
final class StandardCredentials: Credentials {
var username: String
var password: String
}
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")
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.")
}
if try BCrypt.verify(password: usernamePassword.password, matchesHash: user.password) {
return user
} else {
throw Abort.custom(status: .networkAuthenticationRequired, message: "Invalid user name or password.")
}
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.")
}
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).")
}
}