@@ -5,7 +5,7 @@ import Foundation
55 - parameter algorithm: The algorithm to sign the payload with
66 - returns: The JSON web token as a String
77 */
8- public func encode( claims: ClaimSet , algorithm: Algorithm ) -> String {
8+ public func encode( claims: ClaimSet , algorithm: Algorithm , headers : [ String : String ] ? = nil ) -> String {
99 func encodeJSON( _ payload: [ String : Any ] ) -> String ? {
1010 if let data = try ? JSONSerialization . data ( withJSONObject: payload) {
1111 return base64encode ( data)
@@ -14,7 +14,13 @@ public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
1414 return nil
1515 }
1616
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) !
1824 let payload = encodeJSON ( claims. claims) !
1925 let signingInput = " \( header) . \( payload) "
2026 let signature = algorithm. sign ( signingInput)
@@ -26,8 +32,8 @@ public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
2632 - parameter algorithm: The algorithm to sign the payload with
2733 - returns: The JSON web token as a String
2834 */
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 )
3137}
3238
3339
0 commit comments