@@ -10,86 +10,85 @@ class DecodeTests: XCTestCase {
10
10
XCTAssertEqual ( claims [ " name " ] as? String , " Kyle " )
11
11
}
12
12
13
- func testDecodingValidJWT( ) {
13
+ func testDecodingValidJWT( ) throws {
14
14
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
15
15
16
- assertSuccess ( try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) ) { payload in
17
- XCTAssertEqual ( payload as! [ String : String ] , [ " name " : " Kyle " ] )
18
- }
16
+ let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
17
+ XCTAssertEqual ( claims [ " name " ] as? String , " Kyle " )
19
18
}
20
19
21
20
func testFailsToDecodeInvalidStringWithoutThreeSegments( ) {
22
- assertDecodeError ( try decode ( " a.b " , algorithm: . none) , error : " Not enough segments " )
21
+ XCTAssertThrowsError ( try decode ( " a.b " , algorithm: . none) , " Not enough segments " )
23
22
}
24
23
25
24
// MARK: Disable verify
26
25
27
- func testDisablingVerify( ) {
26
+ func testDisablingVerify( ) throws {
28
27
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
29
- assertSuccess ( try decode ( jwt, algorithm: . none, verify: false , issuer: " fuller.li " ) )
28
+ _ = try decode ( jwt, algorithm: . none, verify: false , issuer: " fuller.li " ) as ClaimSet
30
29
}
31
30
32
31
// MARK: Issuer claim
33
32
34
- func testSuccessfulIssuerValidation( ) {
33
+ func testSuccessfulIssuerValidation( ) throws {
35
34
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ "
36
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) , issuer : " fuller.li " ) ) { payload in
37
- XCTAssertEqual ( payload as! [ String : String ] , [ " iss " : " fuller.li " ] )
38
- }
35
+
36
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
37
+ XCTAssertEqual ( claims . issuer , " fuller.li " )
39
38
}
40
39
41
40
func testIncorrectIssuerValidation( ) {
42
41
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI "
43
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " querykit.org " ) )
42
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " querykit.org " ) )
44
43
}
45
44
46
45
func testMissingIssuerValidation( ) {
47
46
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
48
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " fuller.li " ) )
47
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " fuller.li " ) )
49
48
}
50
49
51
50
// MARK: Expiration claim
52
51
53
52
func testExpiredClaim( ) {
54
53
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0MjgxODg0OTF9.cy6b2szsNkKnHFnz2GjTatGjoHBTs8vBKnPGZgpp91I "
55
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
54
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
56
55
}
57
56
58
57
func testInvalidExpiaryClaim( ) {
59
58
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOlsiMTQyODE4ODQ5MSJdfQ.OwF-wd3THjxrEGUhh6IdnNhxQZ7ydwJ3Z6J_dfl9MBs "
60
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
59
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
61
60
}
62
61
63
- func testUnexpiredClaim( ) {
62
+ func testUnexpiredClaim( ) throws {
64
63
// If this just started failing, hello 2024!
65
64
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.EW7k-8Mvnv0GpvOKJalFRLoCB3a3xGG3i7hAZZXNAz0 "
66
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
67
- XCTAssertEqual ( payload as! [ String : Int ] , [ " exp " : 1728188491 ] )
68
- }
65
+
66
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
67
+ XCTAssertEqual ( claims . expiration ? . timeIntervalSince1970 , 1728188491 )
69
68
}
70
69
71
- func testUnexpiredClaimString( ) {
70
+ func testUnexpiredClaimString( ) throws {
72
71
// If this just started failing, hello 2024!
73
72
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk "
74
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
75
- XCTAssertEqual ( payload as! [ String : String ] , [ " exp " : " 1728188491 " ] )
76
- }
73
+
74
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
75
+ XCTAssertEqual ( claims . expiration ? . timeIntervalSince1970 , 1728188491 )
77
76
}
78
77
79
78
// MARK: Not before claim
80
79
81
- func testNotBeforeClaim( ) {
80
+ func testNotBeforeClaim( ) throws {
82
81
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0MjgxODk3MjB9.jFT0nXAJvEwyG6R7CMJlzNJb7FtZGv30QRZpYam5cvs "
83
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
84
- XCTAssertEqual ( payload as! [ String : Int ] , [ " nbf " : 1428189720 ] )
85
- }
82
+
83
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
84
+ XCTAssertEqual ( claims . notBefore ? . timeIntervalSince1970 , 1428189720 )
86
85
}
87
86
88
- func testNotBeforeClaimString( ) {
87
+ func testNotBeforeClaimString( ) throws {
89
88
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI "
90
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
91
- XCTAssertEqual ( payload as! [ String : String ] , [ " nbf " : " 1428189720 " ] )
92
- }
89
+
90
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
91
+ XCTAssertEqual ( claims . notBefore ? . timeIntervalSince1970 , 1428189720 )
93
92
}
94
93
95
94
func testInvalidNotBeforeClaim( ) {
@@ -100,29 +99,29 @@ class DecodeTests: XCTestCase {
100
99
func testUnmetNotBeforeClaim( ) {
101
100
// If this just started failing, hello 2024!
102
101
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MjgxODg0OTF9.Tzhu1tu-7BXcF5YEIFFE1Vmg4tEybUnaz58FR4PcblQ "
103
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
102
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
104
103
}
105
104
106
105
// MARK: Issued at claim
107
106
108
- func testIssuedAtClaimInThePast( ) {
107
+ func testIssuedAtClaimInThePast( ) throws {
109
108
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MjgxODk3MjB9.I_5qjRcCUZVQdABLwG82CSuu2relSdIyJOyvXWUAJh4 "
110
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
111
- XCTAssertEqual ( payload as! [ String : Int ] , [ " iat " : 1428189720 ] )
112
- }
109
+
110
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
111
+ XCTAssertEqual ( claims . issuedAt ? . timeIntervalSince1970 , 1428189720 )
113
112
}
114
113
115
- func testIssuedAtClaimInThePastString( ) {
114
+ func testIssuedAtClaimInThePastString( ) throws {
116
115
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k "
117
- assertSuccess ( try decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
118
- XCTAssertEqual ( payload as! [ String : String ] , [ " iat " : " 1428189720 " ] )
119
- }
116
+
117
+ let claims : ClaimSet = try JWT . decode ( jwt , algorithm : . hs256 ( " secret " . data ( using : . utf8 ) ! ) )
118
+ XCTAssertEqual ( claims . issuedAt ? . timeIntervalSince1970 , 1428189720 )
120
119
}
121
120
122
121
func testIssuedAtClaimInTheFuture( ) {
123
122
// If this just started failing, hello 2024!
124
123
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MjgxODg0OTF9.owHiJyJmTcW1lBW5y_Rz3iBfSbcNiXlbZ2fY9qR7-aU "
125
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
124
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
126
125
}
127
126
128
127
func testInvalidIssuedAtClaim( ) {
@@ -150,12 +149,12 @@ class DecodeTests: XCTestCase {
150
149
151
150
func testMismatchAudienceClaim( ) {
152
151
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.VEB_n06pTSLlTXPFkc46ARADJ9HXNUBUPo3VhL9RDe4 " // kyle
153
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) )
152
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) )
154
153
}
155
154
156
155
func testMissingAudienceClaim( ) {
157
156
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
158
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) )
157
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) )
159
158
}
160
159
161
160
// MARK: Signature verification
@@ -169,7 +168,7 @@ class DecodeTests: XCTestCase {
169
168
170
169
func testNoneFailsWithSecretAlgorithm( ) {
171
170
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
172
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
171
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
173
172
}
174
173
175
174
func testMatchesAnyAlgorithm( ) {
0 commit comments