This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AuthorizationHeader initializers for Basic and Bearer
- Loading branch information
Showing
2 changed files
with
15 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
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)") | ||
} | ||
} |
This file contains 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