From 1e90155dc009ad5d073788a3df5f7d511a3b40b7 Mon Sep 17 00:00:00 2001 From: Goncalo Frade Date: Fri, 31 May 2024 12:44:55 +0100 Subject: [PATCH] fix: allow JWT header field type to be different then JWT --- README.md | 2 +- Sources/JSONWebToken/JWT+Encryption.swift | 12 +++++++++--- Sources/JSONWebToken/JWT+Signing.swift | 12 +++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e1cb911..cba82e8 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ let jws = try JWS(payload: payload, key: key) let jwsString = jws.compactSerialization -try JWS(jwsString: jwsString).verify(key: keyJWK) +try JWS(jwsString: jwsString).verify(key: key) ``` If you want to add additional headers beyond the default to the JWS: diff --git a/Sources/JSONWebToken/JWT+Encryption.swift b/Sources/JSONWebToken/JWT+Encryption.swift index 3057db1..7abb1a0 100644 --- a/Sources/JSONWebToken/JWT+Encryption.swift +++ b/Sources/JSONWebToken/JWT+Encryption.swift @@ -54,7 +54,9 @@ extension JWT { additionalAuthenticationData: Data? = nil ) throws -> JWT { var protectedHeader = protectedHeader - protectedHeader.type = "JWT" + if protectedHeader.type == nil { + protectedHeader.type = "JWT" + } let encodedPayload = try JSONEncoder.jwt.encode(payload) return JWT( payload: encodedPayload, @@ -86,7 +88,9 @@ extension JWT { additionalAuthenticationData: Data? = nil ) throws -> JWT { var protectedHeader = protectedHeader - protectedHeader.type = "JWT" + if protectedHeader.type == nil { + protectedHeader.type = "JWT" + } let encodedPayload = try JSONEncoder.jwt.encode(payload().value) return JWT( payload: encodedPayload, @@ -139,7 +143,9 @@ extension JWT { additionalAuthenticationData: Data? = nil ) throws -> JWE { var protectedHeader = protectedHeader - protectedHeader.contentType = "JWT" + if protectedHeader.contentType == nil { + protectedHeader.contentType = "JWT" + } return try JWE( payload: jwt.jwtString.tryToData(), diff --git a/Sources/JSONWebToken/JWT+Signing.swift b/Sources/JSONWebToken/JWT+Signing.swift index be1410b..98fe99e 100644 --- a/Sources/JSONWebToken/JWT+Signing.swift +++ b/Sources/JSONWebToken/JWT+Signing.swift @@ -40,7 +40,9 @@ extension JWT { key: Key? ) throws -> JWT { var protectedHeader = protectedHeader - protectedHeader.type = "JWT" + if protectedHeader.type == nil { + protectedHeader.type = "JWT" + } let encodedPayload = try JSONEncoder.jwt.encode(payload) return JWT( payload: encodedPayload, @@ -72,7 +74,9 @@ extension JWT { key: Key? ) throws -> JWT { var protectedHeader = protectedHeader - protectedHeader.type = "JWT" + if protectedHeader.type == nil { + protectedHeader.type = "JWT" + } let encodedPayload = try JSONEncoder.jwt.encode(payload().value) return JWT( payload: encodedPayload, @@ -179,7 +183,9 @@ extension JWT { key: KeyRepresentable? ) throws -> JWS { var protectedHeader = protectedHeader - protectedHeader.contentType = "JWT" + if protectedHeader.contentType == nil { + protectedHeader.contentType = "JWT" + } return try JWS( payload: JSONEncoder.jwt.encode(jwtString.tryToData()),