Skip to content

Commit ae07a08

Browse files
committed
[decode] Support expiration claims
1 parent 20f6d20 commit ae07a08

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

JWT/JWT.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ public typealias Payload = [String:AnyObject]
55
public enum InvalidToken : Printable {
66
case DecodeError(String)
77
case InvalidIssuer
8+
case ExpiredSignature
89

910
public var description:String {
1011
switch self {
1112
case .DecodeError(let error):
1213
return "Decode Error: \(error)"
1314
case .InvalidIssuer:
1415
return "Invalid Issuer"
16+
case .ExpiredSignature:
17+
return "Expired Signature"
1518
}
1619
}
1720
}
@@ -112,5 +115,15 @@ func validateClaims(payload:Payload, audience:String?, issuer:String?) -> Invali
112115
return .InvalidIssuer
113116
}
114117
}
118+
119+
if let exp = payload["exp"] as? NSTimeInterval {
120+
let expiary = NSDate(timeIntervalSince1970: exp)
121+
if expiary.compare(NSDate()) == .OrderedAscending {
122+
return .ExpiredSignature
123+
}
124+
} else if let exp:AnyObject = payload["exp"] {
125+
return .DecodeError("Expiration time claim (exp) must be an integer")
126+
}
127+
115128
return nil
116129
}

JWTTests/JWTTests.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@ class JWTDecodeTests : XCTestCase {
1313
assertDecodeError(decode("a.b"), "Not enough segments")
1414
}
1515

16-
// MARK : Issuer validation
16+
// MARK: Disable verify
17+
18+
func testDisablingVerify() {
19+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w"
20+
assertSuccess(decode(jwt, verify:false, issuer:"fuller.li"))
21+
}
22+
23+
// MARK: Issuer claim
1724

1825
func testSuccessfulIssuerValidation() {
1926
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI"
@@ -31,6 +38,26 @@ class JWTDecodeTests : XCTestCase {
3138
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w"
3239
assertFailure(decode(jwt, issuer:"fuller.li"))
3340
}
41+
42+
// MARK: Expiration claim
43+
44+
func testExpiredClaim() {
45+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0MjgxODg0OTF9.cy6b2szsNkKnHFnz2GjTatGjoHBTs8vBKnPGZgpp91I"
46+
assertFailure(decode(jwt))
47+
}
48+
49+
func testInvalidExpiaryClaim() {
50+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOlsiMTQyODE4ODQ5MSJdfQ.OwF-wd3THjxrEGUhh6IdnNhxQZ7ydwJ3Z6J_dfl9MBs"
51+
assertFailure(decode(jwt))
52+
}
53+
54+
func testUnexpiredClaim() {
55+
// If this just started failing, hello 2024!
56+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.7QIdg6ijLJpeiG4m_TqIG9alXLhHMidWDBELkhtUqYw"
57+
assertSuccess(decode(jwt)) { payload in
58+
XCTAssertEqual(payload as NSDictionary, ["exp": 1728188491])
59+
}
60+
}
3461
}
3562

3663
// MARK: Helpers

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ pod 'JWT'
1414

1515
## Usage
1616

17-
### Verify a JWT
17+
### Decoding a JWT
1818

1919
```swift
2020
import JWT
2121

22-
JWT.verify("eyJhbG...y5w")
22+
JWT.decode("eyJhbG...y5w")
2323
```
2424

2525
#### Supported claims
2626

2727
- Issuer (`iss`) Claim
28+
- Expiration Time (`exp`) Claim
2829

2930
## License
3031

0 commit comments

Comments
 (0)