Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. static func authenticate(credentials: Credentials) throws -> Auth.User {
  2. switch credentials {
  3. case let id as Identifier:
  4. guard let user = try User.find(id.id) else {
  5. throw Abort.custom(status: .forbidden, message: “Invalid user identifier.”)
  6. }
  7. return user
  8. case let accessToken as AccessToken:
  9. guard let user = try User.query().filter(“access_token”, accessToken.string).first() else {
  10. throw Abort.custom(status: .forbidden, message: “Invalid access token.”)
  11. }
  12. return user
  13. case let apiKey as APIKey:
  14. guard let user = try User.query().filter(“username”, apiKey.id).first(),
  15. try BCrypt.verify(password: apiKey.secret, matchesHash: user.password) else {
  16. throw Abort.custom(status: .networkAuthenticationRequired, message: “Invalid user name or password.”)
  17. }
  18. if try BCrypt.verify(password: apiKey.secret, matchesHash: user.password) {
  19. return user
  20. } else {
  21. throw Abort.custom(status: .networkAuthenticationRequired, message: “Invalid user name or password.”)
  22. }
  23. default:
  24. let type = type(of: credentials)
  25. throw Abort.custom(status: .forbidden, message: “Unsupported credential type: \(type).”)
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement