@@ -5,7 +5,7 @@ import Foundation
5
5
- parameter algorithm: The algorithm to sign the payload with
6
6
- returns: The JSON web token as a String
7
7
*/
8
- public func encode( claims: ClaimSet , algorithm: Algorithm ) -> String {
8
+ public func encode( claims: ClaimSet , algorithm: Algorithm , headers : [ String : String ] ? = nil ) -> String {
9
9
func encodeJSON( _ payload: [ String : Any ] ) -> String ? {
10
10
if let data = try ? JSONSerialization . data ( withJSONObject: payload) {
11
11
return base64encode ( data)
@@ -14,7 +14,13 @@ public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
14
14
return nil
15
15
}
16
16
17
- let header = encodeJSON ( [ " typ " : " JWT " , " alg " : algorithm. description] ) !
17
+ var headers = headers ?? [ : ]
18
+ if !headers. keys. contains ( " typ " ) {
19
+ headers [ " typ " ] = " JWT "
20
+ }
21
+ headers [ " alg " ] = algorithm. description
22
+
23
+ let header = encodeJSON ( headers) !
18
24
let payload = encodeJSON ( claims. claims) !
19
25
let signingInput = " \( header) . \( payload) "
20
26
let signature = algorithm. sign ( signingInput)
@@ -26,8 +32,8 @@ public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
26
32
- parameter algorithm: The algorithm to sign the payload with
27
33
- returns: The JSON web token as a String
28
34
*/
29
- public func encode( claims: [ String : Any ] , algorithm: Algorithm ) -> String {
30
- return encode ( claims: ClaimSet ( claims: claims) , algorithm: algorithm)
35
+ public func encode( claims: [ String : Any ] , algorithm: Algorithm , headers : [ String : String ] ? = nil ) -> String {
36
+ return encode ( claims: ClaimSet ( claims: claims) , algorithm: algorithm, headers : headers )
31
37
}
32
38
33
39
0 commit comments