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

Commit

Permalink
Add AuthorizationHeader initializers for Basic and Bearer
Browse files Browse the repository at this point in the history
  • Loading branch information
cardoso committed Jul 17, 2017
1 parent 2e697a4 commit 9f6608b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 10 additions & 6 deletions Sources/Authentication/Header/Basic.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import Core

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

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

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

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

return Password(username: username, password: password)
}

public init(basic: Password) {
let credentials = "\(basic.username):\(basic.password)"
let encoded = credentials.makeBytes().base64Encoded.makeString()
self.init(string: "Basic \(encoded)")
}
}
6 changes: 5 additions & 1 deletion Sources/Authentication/Header/Bearer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ extension AuthorizationHeader {
guard let range = string.range(of: "Bearer ") else {
return nil
}

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

public init(bearer: Token) {
self.init(string: "Bearer \(bearer.string)")
}
}

0 comments on commit 9f6608b

Please sign in to comment.