Skip to content

Commit d945892

Browse files
committed
fix: Support validating dates on linux
1 parent 8cd089b commit d945892

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Sources/Claims.swift

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,31 @@ func validateIssuer(_ payload: Payload, issuer: String?) throws {
3939
}
4040

4141
func validateDate(_ payload:Payload, key:String, comparison:ComparisonResult, failure:InvalidToken, decodeError:String) throws {
42-
if let timestamp = payload[key] as? TimeInterval ?? (payload[key] as? NSString)?.doubleValue as TimeInterval? {
43-
let date = Date(timeIntervalSince1970: timestamp)
44-
if date.compare(Date()) == comparison {
45-
throw failure
46-
}
47-
} else if payload[key] != nil {
42+
if payload[key] == nil {
43+
return
44+
}
45+
46+
guard let date = extractDate(payload: payload, key: key) else {
4847
throw InvalidToken.decodeError(decodeError)
4948
}
49+
50+
if date.compare(Date()) == comparison {
51+
throw failure
52+
}
53+
}
54+
55+
fileprivate func extractDate(payload: Payload, key: String) -> Date? {
56+
if let timestamp = payload[key] as? TimeInterval {
57+
return Date(timeIntervalSince1970: timestamp)
58+
}
59+
60+
if let timestamp = payload[key] as? Int {
61+
return Date(timeIntervalSince1970: Double(timestamp))
62+
}
63+
64+
if let timestampString = payload[key] as? String, let timestamp = Double(timestampString) {
65+
return Date(timeIntervalSince1970: timestamp)
66+
}
67+
68+
return nil
5069
}

Tests/JWTTests/JWTTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class DecodeTests: XCTestCase {
176176
XCTAssertEqual(payload as! [String: Int], ["iat": 1428189720])
177177
}
178178
}
179-
179+
180180
func testIssuedAtClaimInThePastString() {
181181
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k"
182182
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!))) { payload in

0 commit comments

Comments
 (0)