Skip to content

Commit

Permalink
fix: jwt date encoding was not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
beatt83 committed Apr 23, 2024
1 parent c65a47f commit 23af19a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/JSONWebToken/JWT+Encryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension JWT {
return JWT(
payload: payload,
format: .jwe(try JWE(
payload: JSONEncoder.jose.encode(payload),
payload: JSONEncoder.jwt.encode(payload),
protectedHeader: protectedHeader,
unprotectedHeader: unprotectedHeader,
senderKey: senderKey,
Expand Down
4 changes: 2 additions & 2 deletions Sources/JSONWebToken/JWT+Signing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension JWT {
return JWT(
payload: payload,
format: .jws(try JWS(
payload: JSONEncoder.jose.encode(payload),
payload: JSONEncoder.jwt.encode(payload),
protectedHeader: protectedHeader,
key: key
))
Expand Down Expand Up @@ -103,7 +103,7 @@ extension JWT {
protectedHeader.contentType = "JWT"

return try JWS(
payload: JSONEncoder.jose.encode(jwtString.tryToData()),
payload: JSONEncoder.jwt.encode(jwtString.tryToData()),
protectedHeader: protectedHeader,
key: key
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JSONWebToken/JWT+Verification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension JWT {
expectedAudience: expectedAudience
)
}
let payload = try JSONDecoder().decode(C.self, from: jws.payload)
let payload = try JSONDecoder.jwt.decode(C.self, from: jws.payload)

guard try jws.verify(key: senderKey) else {
throw JWTError.invalidSignature
Expand Down Expand Up @@ -95,7 +95,7 @@ extension JWT {
expectedAudience: expectedAudience
)
}
let payload = try JSONDecoder().decode(C.self, from: decryptedPayload)
let payload = try JSONDecoder.jwt.decode(C.self, from: decryptedPayload)
return .init(payload: payload, format: .jwe(jwe))
default:
throw JWTError.somethingWentWrong
Expand Down
2 changes: 1 addition & 1 deletion Sources/JSONWebToken/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public struct JWT<C: JWTRegisteredFieldsClaims> {

public extension JWT {
static func getPayload<Payload: JWTRegisteredFieldsClaims>(jwtString: String) throws -> Payload {
return try JSONDecoder().decode(Payload.self, from: getPayload(jwtString: jwtString))
return try JSONDecoder.jwt.decode(Payload.self, from: getPayload(jwtString: jwtString))
}

static func getPayload(jwtString: String) throws -> Data {
Expand Down
34 changes: 34 additions & 0 deletions Sources/Tools/JWTCodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 Gonçalo Frade
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

extension JSONEncoder {
public static var jwt: JSONEncoder {
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes]
encoder.dateEncodingStrategy = .secondsSince1970
return encoder
}
}

extension JSONDecoder {
public static var jwt: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
return decoder
}
}
11 changes: 4 additions & 7 deletions Tests/JWTTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class JWTTests: XCTestCase {

func testParseSignedJWT() throws {
let jwtString = """
eyJhbGciOiJub25lIn0.eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ.
eyJhbGciOiJub25lIn0.eyJpc3MiOiJ0ZXN0QWxpY2UiLCJzdWIiOiJBbGljZSIsInRlc3RDbGFpbSI6InRlc3RlZENsYWltIn0.
"""

let jwt = try JWT<DefaultJWTClaimsImpl>.verify(jwtString: jwtString)
Expand All @@ -20,13 +20,11 @@ final class JWTTests: XCTestCase {
XCTFail("Wrong JWT format")
}

let expirationTime = jwt.payload.exp?.timeIntervalSince1970
XCTAssertEqual(jwt.payload.iss, "joe")
XCTAssertEqual(jwt.payload.exp!, Date(timeIntervalSince1970: 2279126580.0))
XCTAssertEqual(jwt.payload.iss, "testAlice")
}

func testSignAndVerify() throws {
let issuedAt = Date(timeIntervalSince1970: 0)
let issuedAt = Date(timeIntervalSince1970: 200)
let mockClaims = MockExampleClaims(
iss: "testAlice",
sub: "Alice",
Expand All @@ -45,7 +43,7 @@ final class JWTTests: XCTestCase {
let jwtString = jwt.jwtString

XCTAssertTrue(jwtString.contains("eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9"))
XCTAssertTrue(jwtString.contains("eyJpYXQiOi05NzgzMDcyMDAsImlzcyI6InRlc3RBbGljZSIsInN1YiI6IkFsaWNlIiwidGVzdENsYWltIjoidGVzdGVkQ2xhaW0ifQ"))
XCTAssertTrue(jwtString.contains("eyJpYXQiOjIwMCwiaXNzIjoidGVzdEFsaWNlIiwic3ViIjoiQWxpY2UiLCJ0ZXN0Q2xhaW0iOiJ0ZXN0ZWRDbGFpbSJ9"))

let verifiedJWT = try JWT<MockExampleClaims>.verify(jwtString: jwtString, senderKey: key)
let verifiedPayload = verifiedJWT.payload
Expand Down Expand Up @@ -150,7 +148,6 @@ final class JWTTests: XCTestCase {
}

func testFailAudienceValidation() throws {
let nbf = Date(timeIntervalSinceNow: 1000)
let mockClaims = DefaultJWTClaimsImpl(
iss: "testAlice",
sub: "Alice",
Expand Down

0 comments on commit 23af19a

Please sign in to comment.