Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
token + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Feb 16, 2017
1 parent 470241a commit dfdb80c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 88 deletions.
6 changes: 0 additions & 6 deletions Sources/Authentication/Credentials/Custom.swift

This file was deleted.

33 changes: 0 additions & 33 deletions Sources/Authentication/Credentials/Identifier.swift

This file was deleted.

35 changes: 20 additions & 15 deletions Sources/Authentication/Credentials/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ public struct Token: Crendentials {

public protocol TokenAuthenticatable: Authenticatable {
/// The token entity that contains a foreign key
/// pointer to the user table
associatedtype TokenType: TokenProtocol
/// pointer to the user table (or on the user table itself)
associatedtype TokenType

/// Returns the user matching the supplied token.
static func authenticate(_: Token) throws -> Self
}
static func authenticate(_ token: Token) throws -> Self

public protocol TokenProtocol {
/// The column under which the tokens are stored
static var tokenKey: String { get }
static func findUser<U: Entity>(for: Token) throws -> U
}

extension TokenProtocol {
extension TokenAuthenticatable {
public static var tokenKey: String {
return "token"
}
Expand All @@ -34,20 +32,27 @@ extension TokenProtocol {

import Fluent

extension TokenAuthenticatable where Self: Entity {
extension TokenAuthenticatable where Self: Entity, Self.TokenType: Entity {
public static func authenticate(_ token: Token) throws -> Self {
return try TokenType.findUser(for: token)
guard let user = try Self.query()
.join(Self.TokenType.self)
.filter(Self.TokenType.self, tokenKey, token.string)
.first()
else {
throw AuthenticationError.invalidCredentials
}

return user
}
}

extension TokenProtocol where Self: Entity {
public static func findUser<U: Entity>(for token: Token) throws -> U {
guard let user = try U.query()
.join(self)
.filter(self, tokenKey, token.string)
extension TokenAuthenticatable where Self: Entity, Self.TokenType: Entity, Self.TokenType == Self {
public static func authenticate(_ token: Token) throws -> Self {
guard let user = try Self.query()
.filter(tokenKey, token.string)
.first()
else {
throw AuthenticationError.invalidCredentials
throw AuthenticationError.invalidCredentials
}

return user
Expand Down
8 changes: 4 additions & 4 deletions Sources/Authentication/Header/Authorization.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public struct Authorization {
public let header: String
public struct AuthorizationHeader {
public let string: String

public init(header: String) {
self.header = header
public init(string: String) {
self.string = string
}
}
13 changes: 6 additions & 7 deletions Sources/Authentication/Header/Basic.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import Core

extension Authorization {
extension AuthorizationHeader {
public var basic: Password? {
guard let range = header.range(of: "Basic ") else {
guard let range = string.range(of: "Basic ") else {
return nil
}

let token = header.substring(from: range.upperBound)

let token = string.substring(from: range.upperBound)

let decodedToken = token.makeBytes().base64Decoded.string
guard let separatorRange = decodedToken.range(of: ":") else {
return nil
}

let apiKeyID = decodedToken.substring(to: separatorRange.lowerBound)
let apiKeySecret = decodedToken.substring(from: separatorRange.upperBound)
let username = decodedToken.substring(to: separatorRange.lowerBound)
let password = decodedToken.substring(from: separatorRange.upperBound)

return Password(username: apiKeyID, password: apiKeySecret)
return Password(username: username, password: password)
}
}
6 changes: 3 additions & 3 deletions Sources/Authentication/Header/Bearer.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extension Authorization {
extension AuthorizationHeader {
public var bearer: Token? {
guard let range = header.range(of: "Bearer ") else {
guard let range = string.range(of: "Bearer ") else {
return nil
}

let token = header.substring(from: range.upperBound)
let token = string.substring(from: range.upperBound)
return Token(string: token)
}
}
3 changes: 0 additions & 3 deletions Sources/Authentication/User/User.swift

This file was deleted.

5 changes: 0 additions & 5 deletions Tests/AuthenticationTests/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ class ExampleTests: XCTestCase {

func testExample() throws {
do {
let id = Identifier(id: Node.string("5"))
let token = Token(string: "5")

let user1 = try AuthUser.authenticate(id)
XCTAssertEqual(user1.name, "5")

let user2 = try AuthUser.authenticate(token)
XCTAssertEqual(user2.name, "6")
} catch {
Expand Down
12 changes: 0 additions & 12 deletions Tests/AuthenticationTests/Utilities/AuthUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,10 @@ final class AuthUser: Entity {
}
}

extension AuthUser: IdentifierAuthenticatable {
static func authenticate(_ id: Identifier) throws -> Self {
return self.init(name: "5")
}
}

extension AuthUser: TokenAuthenticatable {
typealias TokenType = AuthUser

static func authenticate(_ id: Token) throws -> Self {
return self.init(name: "6")
}
}

extension AuthUser: TokenProtocol {
static var tokenKey: String {
return ""
}
}

0 comments on commit dfdb80c

Please sign in to comment.