Skip to content

Commit

Permalink
fix: allow JWT header field type to be different then JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
beatt83 committed May 31, 2024
1 parent 1d36e20 commit 1e90155
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 9 additions & 3 deletions Sources/JSONWebToken/JWT+Encryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
12 changes: 9 additions & 3 deletions Sources/JSONWebToken/JWT+Signing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit 1e90155

Please sign in to comment.