Skip to content

Commit

Permalink
Add typ, cty fields in the JWT header (#27)
Browse files Browse the repository at this point in the history
* Missing typ field in the header #5

Add `typ` header as parameter making "JWT" the default.

* Add missing typ, cty fields in the header

* Update JWTSigner.swift

* minor fix

Co-authored-by: Lukas Kasakaitis <[email protected]>
Co-authored-by: Maxim Anisimov <[email protected]>
  • Loading branch information
3 people authored Jun 25, 2020
1 parent b5aa3d4 commit b5b711f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Sources/JWTKit/JWTSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ struct JWTSerializer {
func sign<Payload>(
_ payload: Payload,
using signer: JWTSigner,
kid: JWKIdentifier? = nil
typ: String = "JWT",
kid: JWKIdentifier? = nil,
cty: String? = nil
) throws -> String
where Payload: JWTPayload
{
Expand All @@ -14,6 +16,8 @@ struct JWTSerializer {
// encode header, copying header struct to mutate alg
var header = JWTHeader()
header.kid = kid
header.typ = typ
header.cty = cty
header.alg = signer.algorithm.name

let headerData = try jsonEncoder.encode(header)
Expand Down
5 changes: 3 additions & 2 deletions Sources/JWTKit/Signing/JWTSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public final class JWTSigner {
}

public func sign<Payload>(
_ payload: Payload
_ payload: Payload,
cty: String? = nil
) throws -> String
where Payload: JWTPayload
{
try JWTSerializer().sign(payload, using: self, kid: nil)
try JWTSerializer().sign(payload, using: self, kid: nil, cty: cty)
}

public func unverified<Payload>(
Expand Down
2 changes: 1 addition & 1 deletion Tests/JWTKitTests/JWTKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JWTKitTests: XCTestCase {
let exp = ExpirationClaim(value: Date(timeIntervalSince1970: 2_000_000_000))
let jwt = try JWTSigner.hs256(key: "secret".bytes)
.sign(ExpirationPayload(exp: exp))
XCTAssertEqual(jwt, "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjIwMDAwMDAwMDB9.4W6egHvMSp9bBiGUnE7WhVfXazOfg-ADcjvIYILgyPU")
XCTAssertEqual(jwt, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIwMDAwMDAwMDB9.JgCO_GqUQnbS0z2hCxJLE9Tpt5SMoZObHBxzGBWuTYQ")
}

func testSigners() throws {
Expand Down

0 comments on commit b5b711f

Please sign in to comment.