Created
December 23, 2015 05:34
-
-
Save jparishy/5fa024cd0bf1dea41990 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// SessionsController.swift | |
// Swerver | |
// | |
// Created by Julius Parishy on 12/17/15. | |
// Copyright © 2015 Julius Parishy. All rights reserved. | |
// | |
import Foundation | |
class SessionsController : Controller { | |
override func index(request: Request, parameters: Parameters, session inSession: Session, transaction t: Transaction) throws -> ControllerResponse { | |
let mq = ModelQuery<User>(transaction: t) | |
let user: User? | |
if let userID = inSession["user_id"] as? Int, u = try mq.findWhere(["id":userID]).first { | |
user = u | |
} else { | |
user = nil | |
} | |
return view(SessionIndexView(user: user)) | |
} | |
override func create(request: Request, parameters: Parameters, session inSession: Session, transaction t: Transaction) throws -> ControllerResponse { | |
let mq = ModelQuery<User>(transaction: t) | |
if let email = parameters["email"] as? String, let password = parameters["password"] as? String { | |
if let user = try mq.findWhere(["email":email]).first where user.authenticateWithPassword(password) { | |
var session = Session() | |
session.update("user_id", user.id.value()) | |
return try redirect(to: "/sessions", session: session) | |
} else { | |
return view(SessionIndexView(user: nil), flash: ["error":"Invalid Email or Password"]) | |
} | |
} else { | |
return view(SessionIndexView(user: nil), flash: ["error":"Missing Email or Password"]) | |
} | |
} | |
func signOut(request: Request, parameters: Parameters, session inSession: Session, transaction t: Transaction) throws /* UserError, InternalServerError */ -> ControllerResponse { | |
var session = Session() | |
session.update("user_id", nil) | |
return try redirect(to: "/sessions", session: session) | |
} | |
} |
This file contains hidden or 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
// | |
// User.swift | |
// Swerver | |
// | |
// Created by Julius Parishy on 12/22/15. | |
// Copyright © 2015 Julius Parishy. All rights reserved. | |
// | |
import Foundation | |
class User : Model { | |
required init() { | |
} | |
let id = IntProperty(column: "id") | |
let email = StringProperty(column: "email") | |
let encryptedPassword = StringProperty(column: "encrypted_password") | |
let passwordSalt = StringProperty(column: "password_salt") | |
class override var table: String { | |
return "users" | |
} | |
class override var columns: [String] { | |
return [ | |
"id", | |
"email", | |
"encrypted_password", | |
"password_salt" | |
] | |
} | |
class override var primaryKey: String { | |
return "id" | |
} | |
override var properties: [BaseProperty] { | |
return [ | |
self.id, | |
self.email, | |
self.encryptedPassword, | |
self.passwordSalt | |
] | |
} | |
} | |
extension User { | |
static func hashPassword(password: String, salt: String? = nil) -> String { | |
if let salt = salt { | |
return (password + salt).sha1() | |
} else { | |
return password.sha1() | |
} | |
} | |
static func randomPasswordSalt() -> String { | |
let bytes = (0..<16).map { _ in Character(UnicodeScalar(cs_arc4random_uniform(256))) } | |
return String(bytes).sha1() | |
} | |
func authenticateWithPassword(password: String) -> Bool { | |
return self.encryptedPassword.value() == User.hashPassword(password + self.passwordSalt.value()) | |
} | |
func updatePassword(password: String) { | |
let salt = User.randomPasswordSalt() | |
self.passwordSalt.update(salt) | |
let encrypted = User.hashPassword(password, salt: salt) | |
self.encryptedPassword.update(encrypted) | |
} | |
} | |
extension User : CustomStringConvertible { | |
var description: String { | |
return "<Todo: id=\(id); email=\(String(email));>" | |
} | |
} |
This file contains hidden or 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
// | |
// UsersController.swift | |
// Swerver | |
// | |
// Created by Julius Parishy on 12/17/15. | |
// Copyright © 2015 Julius Parishy. All rights reserved. | |
// | |
import Foundation | |
class UsersController : Controller { | |
override func index(request: Request, parameters: Parameters, session: Session, transaction t: Transaction) throws -> ControllerResponse { | |
return view(UserIndexView()) | |
} | |
override func new(request: Request, parameters: Parameters, session inSession: Session, transaction t: Transaction) throws -> ControllerResponse { | |
return view(UserNewView()) | |
} | |
override func create(request: Request, parameters: Parameters, session: Session, transaction t: Transaction) throws -> ControllerResponse { | |
if let email = parameters["email"] as? String, let password = parameters["password"] as? String { | |
let mq = ModelQuery<User>(transaction: t) | |
if try mq.findWhere(["email":email]).count != 0 { | |
return view(UserNewView(), flash: ["error":"Email already exists"]) | |
} else { | |
let user = User() | |
user.email.update(email) | |
user.updatePassword(password) | |
let outUser = try mq.insert(user) | |
var outSession = Session() | |
outSession.update("user_id", outUser.id.value()) | |
return try redirect(to: "/", session: outSession) | |
} | |
} else { | |
return view(UserNewView(), flash: ["error":"Missing Username or Password"]) | |
} | |
} | |
override func show(request: Request, parameters: Parameters, session: Session, transaction t: Transaction) throws -> ControllerResponse { | |
if let param = parameters["id"] as? String, userID = Int(param) { | |
return view(UserShowView(userID: userID)) | |
} else { | |
return builtin(.NotFound) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment