Skip to content

Commit aef1055

Browse files
committed
Merge pull request #12 from parrots/master
Allow casting from String -> Double when checking timestamps
2 parents 751bd84 + c5bf651 commit aef1055

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

JWT/Claims.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func validateIssuer(payload:Payload, issuer:String?) -> InvalidToken? {
4040
}
4141

4242
func validateDate(payload:Payload, key:String, comparison:NSComparisonResult, failure:InvalidToken, decodeError:String) -> InvalidToken? {
43-
if let timestamp = payload[key] as? NSTimeInterval {
43+
if let timestamp = payload[key] as? NSTimeInterval ?? payload[key]?.doubleValue as NSTimeInterval? {
4444
let date = NSDate(timeIntervalSince1970: timestamp)
4545
if date.compare(NSDate()) == comparison {
4646
return failure

JWTTests/JWTTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ class JWTDecodeTests : XCTestCase {
131131
XCTAssertEqual(payload as NSDictionary, ["exp": 1728188491])
132132
}
133133
}
134+
135+
func testUnexpiredClaimString() {
136+
// If this just started failing, hello 2024!
137+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk"
138+
assertSuccess(try decode(jwt, algorithm: .HS256("secret"))) { payload in
139+
XCTAssertEqual(payload as NSDictionary, ["exp": "1728188491"])
140+
}
141+
}
134142

135143
// MARK: Not before claim
136144

@@ -140,6 +148,13 @@ class JWTDecodeTests : XCTestCase {
140148
XCTAssertEqual(payload as NSDictionary, ["nbf": 1428189720])
141149
}
142150
}
151+
152+
func testNotBeforeClaimString() {
153+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI"
154+
assertSuccess(try decode(jwt, algorithm: .HS256("secret"))) { payload in
155+
XCTAssertEqual(payload as NSDictionary, ["nbf": "1428189720"])
156+
}
157+
}
143158

144159
func testInvalidNotBeforeClaim() {
145160
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOlsxNDI4MTg5NzIwXX0.PUL1FQubzzJa4MNXe2D3d5t5cMaqFr3kYlzRUzly-C8"
@@ -160,6 +175,13 @@ class JWTDecodeTests : XCTestCase {
160175
XCTAssertEqual(payload as NSDictionary, ["iat": 1428189720])
161176
}
162177
}
178+
179+
func testIssuedAtClaimInThePastString() {
180+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k"
181+
assertSuccess(try decode(jwt, algorithm: .HS256("secret"))) { payload in
182+
XCTAssertEqual(payload as NSDictionary, ["iat": "1428189720"])
183+
}
184+
}
163185

164186
func testIssuedAtClaimInTheFuture() {
165187
// If this just started failing, hello 2024!

0 commit comments

Comments
 (0)