Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow JWT header field type to be different then JWT #23

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading