-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathEncode.swift
56 lines (47 loc) · 1.91 KB
/
Encode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
/*** Encode a set of claims
- parameter claims: The set of claims
- parameter algorithm: The algorithm to sign the payload with
- returns: The JSON web token as a String
*/
public func encode(claims: ClaimSet, algorithm: Algorithm, headers: [String: String]? = nil) throws -> String {
func encodeJSON(_ payload: [String: Any]) -> String? {
if let data = try? JSONSerialization.data(withJSONObject: payload) {
return base64encode(data)
}
return nil
}
var headers = headers ?? [:]
if !headers.keys.contains("typ") {
headers["typ"] = "JWT"
}
headers["alg"] = algorithm.description
let header = encodeJSON(headers)!
let payload = encodeJSON(claims.claims)!
let signingInput = "\(header).\(payload)"
let signature = try algorithm.sign(signingInput)
return "\(signingInput).\(signature)"
}
/*** Encode a dictionary of claims
- parameter claims: The dictionary of claims
- parameter algorithm: The algorithm to sign the payload with
- returns: The JSON web token as a String
*/
public func encode(claims: [String: Any], algorithm: Algorithm, headers: [String: String]? = nil) throws -> String {
return try encode(claims: ClaimSet(claims: claims), algorithm: algorithm, headers: headers)
}
/// Encode a set of claims using the builder pattern
public func encode(_ algorithm: Algorithm, closure: ((ClaimSetBuilder) -> Void)) throws -> String {
let builder = ClaimSetBuilder()
closure(builder)
return try encode(claims: builder.claims, algorithm: algorithm)
}
/*** Encode a payload
- parameter payload: The payload to sign
- parameter algorithm: The algorithm to sign the payload with
- returns: The JSON web token as a String
*/
@available(*, deprecated, message: "use encode(claims: algorithm:) instead")
public func encode(_ payload: Payload, algorithm: Algorithm) throws -> String {
return try encode(claims: ClaimSet(claims: payload), algorithm: algorithm)
}