@@ -3,75 +3,68 @@ import XCTest
3
3
@testable import JWT
4
4
5
5
class DecodeTests : XCTestCase {
6
- func testDecodingValidJWTAsClaimSet( ) throws {
7
- let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
8
-
9
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
10
- XCTAssertEqual ( claims [ " name " ] as? String , " Kyle " )
11
- }
12
-
13
6
func testDecodingValidJWT( ) throws {
14
7
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
15
8
16
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
9
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
17
10
XCTAssertEqual ( claims [ " name " ] as? String , " Kyle " )
18
11
}
19
12
20
13
func testFailsToDecodeInvalidStringWithoutThreeSegments( ) {
21
- XCTAssertThrowsError ( try decode ( " a.b " , algorithm: . none) as ClaimSet , " Not enough segments " )
14
+ XCTAssertThrowsError ( try decode ( " a.b " , algorithm: . none) , " Not enough segments " )
22
15
}
23
16
24
17
// MARK: Disable verify
25
18
26
19
func testDisablingVerify( ) throws {
27
20
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
28
- _ = try decode ( jwt, algorithm: . none, verify: false , issuer: " fuller.li " ) as ClaimSet
21
+ _ = try decode ( jwt, algorithm: . none, verify: false , issuer: " fuller.li " )
29
22
}
30
23
31
24
// MARK: Issuer claim
32
25
33
26
func testSuccessfulIssuerValidation( ) throws {
34
27
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ "
35
28
36
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
29
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
37
30
XCTAssertEqual ( claims. issuer, " fuller.li " )
38
31
}
39
32
40
33
func testIncorrectIssuerValidation( ) {
41
34
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI "
42
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " querykit.org " ) as ClaimSet )
35
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " querykit.org " ) )
43
36
}
44
37
45
38
func testMissingIssuerValidation( ) {
46
39
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
47
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " fuller.li " ) as ClaimSet )
40
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , issuer: " fuller.li " ) )
48
41
}
49
42
50
43
// MARK: Expiration claim
51
44
52
45
func testExpiredClaim( ) {
53
46
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0MjgxODg0OTF9.cy6b2szsNkKnHFnz2GjTatGjoHBTs8vBKnPGZgpp91I "
54
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet )
47
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
55
48
}
56
49
57
50
func testInvalidExpiaryClaim( ) {
58
51
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOlsiMTQyODE4ODQ5MSJdfQ.OwF-wd3THjxrEGUhh6IdnNhxQZ7ydwJ3Z6J_dfl9MBs "
59
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet )
52
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
60
53
}
61
54
62
55
func testUnexpiredClaim( ) throws {
63
56
// If this just started failing, hello 2024!
64
57
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.EW7k-8Mvnv0GpvOKJalFRLoCB3a3xGG3i7hAZZXNAz0 "
65
58
66
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
59
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
67
60
XCTAssertEqual ( claims. expiration? . timeIntervalSince1970, 1728188491 )
68
61
}
69
62
70
63
func testUnexpiredClaimString( ) throws {
71
64
// If this just started failing, hello 2024!
72
65
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk "
73
66
74
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
67
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
75
68
XCTAssertEqual ( claims. expiration? . timeIntervalSince1970, 1728188491 )
76
69
}
77
70
@@ -80,48 +73,48 @@ class DecodeTests: XCTestCase {
80
73
func testNotBeforeClaim( ) throws {
81
74
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0MjgxODk3MjB9.jFT0nXAJvEwyG6R7CMJlzNJb7FtZGv30QRZpYam5cvs "
82
75
83
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
76
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
84
77
XCTAssertEqual ( claims. notBefore? . timeIntervalSince1970, 1428189720 )
85
78
}
86
79
87
80
func testNotBeforeClaimString( ) throws {
88
81
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI "
89
82
90
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
83
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
91
84
XCTAssertEqual ( claims. notBefore? . timeIntervalSince1970, 1428189720 )
92
85
}
93
86
94
87
func testInvalidNotBeforeClaim( ) {
95
88
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOlsxNDI4MTg5NzIwXX0.PUL1FQubzzJa4MNXe2D3d5t5cMaqFr3kYlzRUzly-C8 "
96
- assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet , error: " Not before claim (nbf) must be an integer " )
89
+ assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) , error: " Not before claim (nbf) must be an integer " )
97
90
}
98
91
99
92
func testUnmetNotBeforeClaim( ) {
100
93
// If this just started failing, hello 2024!
101
94
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MjgxODg0OTF9.Tzhu1tu-7BXcF5YEIFFE1Vmg4tEybUnaz58FR4PcblQ "
102
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet )
95
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
103
96
}
104
97
105
98
// MARK: Issued at claim
106
99
107
100
func testIssuedAtClaimInThePast( ) throws {
108
101
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MjgxODk3MjB9.I_5qjRcCUZVQdABLwG82CSuu2relSdIyJOyvXWUAJh4 "
109
102
110
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
103
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
111
104
XCTAssertEqual ( claims. issuedAt? . timeIntervalSince1970, 1428189720 )
112
105
}
113
106
114
107
func testIssuedAtClaimInThePastString( ) throws {
115
108
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k "
116
109
117
- let claims : ClaimSet = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
110
+ let claims = try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) )
118
111
XCTAssertEqual ( claims. issuedAt? . timeIntervalSince1970, 1428189720 )
119
112
}
120
113
121
114
func testIssuedAtClaimInTheFuture( ) {
122
115
// If this just started failing, hello 2024!
123
116
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MjgxODg0OTF9.owHiJyJmTcW1lBW5y_Rz3iBfSbcNiXlbZ2fY9qR7-aU "
124
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet )
117
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
125
118
}
126
119
127
120
func testInvalidIssuedAtClaim( ) {
@@ -134,58 +127,58 @@ class DecodeTests: XCTestCase {
134
127
135
128
func testAudiencesClaim( ) {
136
129
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibWF4aW5lIiwia2F0aWUiXX0.-PKvdNLCClrWG7CvesHP6PB0-vxu-_IZcsYhJxBy5JM "
137
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) as ClaimSet ) { payload in
130
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) ) { payload in
138
131
XCTAssertEqual ( payload. count, 1 )
139
132
XCTAssertEqual ( payload [ " aud " ] as! [ String ] , [ " maxine " , " katie " ] )
140
133
}
141
134
}
142
135
143
136
func testAudienceClaim( ) {
144
137
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.dpgH4JOwueReaBoanLSxsGTc7AjKUvo7_M1sAfy_xVE "
145
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) as ClaimSet ) { payload in
138
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) ) { payload in
146
139
XCTAssertEqual ( payload as! [ String : String ] , [ " aud " : " kyle " ] )
147
140
}
148
141
}
149
142
150
143
func testMismatchAudienceClaim( ) {
151
144
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.VEB_n06pTSLlTXPFkc46ARADJ9HXNUBUPo3VhL9RDe4 " // kyle
152
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) as ClaimSet )
145
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) )
153
146
}
154
147
155
148
func testMissingAudienceClaim( ) {
156
149
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
157
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) as ClaimSet )
150
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) )
158
151
}
159
152
160
153
// MARK: Signature verification
161
154
162
155
func testNoneAlgorithm( ) {
163
156
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
164
- assertSuccess ( try decode ( jwt, algorithm: . none) as ClaimSet ) { payload in
157
+ assertSuccess ( try decode ( jwt, algorithm: . none) ) { payload in
165
158
XCTAssertEqual ( payload as! [ String : String ] , [ " test " : " ing " ] )
166
159
}
167
160
}
168
161
169
162
func testNoneFailsWithSecretAlgorithm( ) {
170
163
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
171
- XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet )
164
+ XCTAssertThrowsError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) )
172
165
}
173
166
174
167
func testMatchesAnyAlgorithm( ) {
175
168
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w. "
176
- assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " . data ( using: . utf8) !) , . hs256( " secret " . data ( using: . utf8) !) ] ) as ClaimSet )
169
+ assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " . data ( using: . utf8) !) , . hs256( " secret " . data ( using: . utf8) !) ] ) )
177
170
}
178
171
179
172
func testHS384Algorithm( ) {
180
173
let jwt = " eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4 "
181
- assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " . data ( using: . utf8) !) ) as ClaimSet ) { payload in
174
+ assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " . data ( using: . utf8) !) ) ) { payload in
182
175
XCTAssertEqual ( payload as! [ String : String ] , [ " some " : " payload " ] )
183
176
}
184
177
}
185
178
186
179
func testHS512Algorithm( ) {
187
180
let jwt = " eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA "
188
- assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " . data ( using: . utf8) !) ) as ClaimSet ) { claims in
181
+ assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " . data ( using: . utf8) !) ) ) { claims in
189
182
XCTAssertEqual ( claims as! [ String : String ] , [ " some " : " payload " ] )
190
183
}
191
184
}
0 commit comments