@@ -15,7 +15,7 @@ class JWTEncodeTests : XCTestCase {
15
15
builder. issuer = " fuller.li "
16
16
}
17
17
18
- assertSuccess ( JWT . decode ( jwt, algorithm: algorithm) ) { payload in
18
+ assertSuccess ( try JWT . decode ( jwt, algorithm: algorithm) ) { payload in
19
19
XCTAssertEqual ( payload as NSDictionary , [ " iss " : " fuller.li " ] )
20
20
}
21
21
}
@@ -76,58 +76,58 @@ class JWTPayloadBuilder : XCTestCase {
76
76
class JWTDecodeTests : XCTestCase {
77
77
func testDecodingValidJWT( ) {
78
78
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
79
- let result = JWT . decode ( jwt , algorithm : . HS256 ( " secret " ) )
80
- assertSuccess ( result ) { payload in
79
+
80
+ assertSuccess ( try JWT . decode ( jwt , algorithm : . HS256 ( " secret " ) ) ) { payload in
81
81
XCTAssertEqual ( payload as NSDictionary , [ " name " : " Kyle " ] )
82
82
}
83
83
}
84
84
85
85
func testFailsToDecodeInvalidStringWithoutThreeSegments( ) {
86
- assertDecodeError ( decode ( " a.b " , algorithm: . None) , error: " Not enough segments " )
86
+ assertDecodeError ( try decode ( " a.b " , algorithm: . None) , error: " Not enough segments " )
87
87
}
88
88
89
89
// MARK: Disable verify
90
90
91
91
func testDisablingVerify( ) {
92
92
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
93
- assertSuccess ( decode ( jwt, algorithm: . None, verify: false , issuer: " fuller.li " ) )
93
+ assertSuccess ( try decode ( jwt, algorithm: . None, verify: false , issuer: " fuller.li " ) )
94
94
}
95
95
96
96
// MARK: Issuer claim
97
97
98
98
func testSuccessfulIssuerValidation( ) {
99
99
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ "
100
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " fuller.li " ) ) { payload in
100
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " fuller.li " ) ) { payload in
101
101
XCTAssertEqual ( payload as NSDictionary , [ " iss " : " fuller.li " ] )
102
102
}
103
103
}
104
104
105
105
func testIncorrectIssuerValidation( ) {
106
106
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI "
107
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " querykit.org " ) )
107
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " querykit.org " ) )
108
108
}
109
109
110
110
func testMissingIssuerValidation( ) {
111
111
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
112
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " fuller.li " ) )
112
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) , issuer: " fuller.li " ) )
113
113
}
114
114
115
115
// MARK: Expiration claim
116
116
117
117
func testExpiredClaim( ) {
118
118
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0MjgxODg0OTF9.cy6b2szsNkKnHFnz2GjTatGjoHBTs8vBKnPGZgpp91I "
119
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) ) )
119
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) ) )
120
120
}
121
121
122
122
func testInvalidExpiaryClaim( ) {
123
123
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOlsiMTQyODE4ODQ5MSJdfQ.OwF-wd3THjxrEGUhh6IdnNhxQZ7ydwJ3Z6J_dfl9MBs "
124
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) ) )
124
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) ) )
125
125
}
126
126
127
127
func testUnexpiredClaim( ) {
128
128
// If this just started failing, hello 2024!
129
129
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.EW7k-8Mvnv0GpvOKJalFRLoCB3a3xGG3i7hAZZXNAz0 "
130
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
130
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
131
131
XCTAssertEqual ( payload as NSDictionary , [ " exp " : 1728188491 ] )
132
132
}
133
133
}
@@ -136,131 +136,127 @@ class JWTDecodeTests : XCTestCase {
136
136
137
137
func testNotBeforeClaim( ) {
138
138
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0MjgxODk3MjB9.jFT0nXAJvEwyG6R7CMJlzNJb7FtZGv30QRZpYam5cvs "
139
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
139
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
140
140
XCTAssertEqual ( payload as NSDictionary , [ " nbf " : 1428189720 ] )
141
141
}
142
142
}
143
143
144
144
func testInvalidNotBeforeClaim( ) {
145
145
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOlsxNDI4MTg5NzIwXX0.PUL1FQubzzJa4MNXe2D3d5t5cMaqFr3kYlzRUzly-C8 "
146
- assertDecodeError ( decode ( jwt, algorithm: . HS256( " secret " ) ) , error: " Not before claim (nbf) must be an integer " )
146
+ assertDecodeError ( try decode ( jwt, algorithm: . HS256( " secret " ) ) , error: " Not before claim (nbf) must be an integer " )
147
147
}
148
148
149
149
func testUnmetNotBeforeClaim( ) {
150
150
// If this just started failing, hello 2024!
151
151
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MjgxODg0OTF9.Tzhu1tu-7BXcF5YEIFFE1Vmg4tEybUnaz58FR4PcblQ "
152
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) ) )
152
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) ) )
153
153
}
154
154
155
155
// MARK: Issued at claim
156
156
157
157
func testIssuedAtClaimInThePast( ) {
158
158
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MjgxODk3MjB9.I_5qjRcCUZVQdABLwG82CSuu2relSdIyJOyvXWUAJh4 "
159
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
159
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) ) ) { payload in
160
160
XCTAssertEqual ( payload as NSDictionary , [ " iat " : 1428189720 ] )
161
161
}
162
162
}
163
163
164
164
func testIssuedAtClaimInTheFuture( ) {
165
165
// If this just started failing, hello 2024!
166
166
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MjgxODg0OTF9.owHiJyJmTcW1lBW5y_Rz3iBfSbcNiXlbZ2fY9qR7-aU "
167
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) ) )
167
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) ) )
168
168
}
169
169
170
170
func testInvalidIssuedAtClaim( ) {
171
171
// If this just started failing, hello 2024!
172
172
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOlsxNzI4MTg4NDkxXX0.ND7QMWtLkXDXH38OaXM3SQgLo3Z5TNgF_pcfWHV_alQ "
173
- assertDecodeError ( decode ( jwt, algorithm: . HS256( " secret " ) ) , error: " Issued at claim (iat) must be an integer " )
173
+ assertDecodeError ( try decode ( jwt, algorithm: . HS256( " secret " ) ) , error: " Issued at claim (iat) must be an integer " )
174
174
}
175
175
176
176
// MARK: Audience claims
177
177
178
178
func testAudiencesClaim( ) {
179
179
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibWF4aW5lIiwia2F0aWUiXX0.-PKvdNLCClrWG7CvesHP6PB0-vxu-_IZcsYhJxBy5JM "
180
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) , audience: " maxine " ) ) { payload in
180
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) , audience: " maxine " ) ) { payload in
181
181
XCTAssertEqual ( payload as NSDictionary , [ " aud " : [ " maxine " , " katie " ] ] )
182
182
}
183
183
}
184
184
185
185
func testAudienceClaim( ) {
186
186
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.dpgH4JOwueReaBoanLSxsGTc7AjKUvo7_M1sAfy_xVE "
187
- assertSuccess ( decode ( jwt, algorithm: . HS256( " secret " ) , audience: " kyle " ) ) { payload in
187
+ assertSuccess ( try decode ( jwt, algorithm: . HS256( " secret " ) , audience: " kyle " ) ) { payload in
188
188
XCTAssertEqual ( payload as NSDictionary , [ " aud " : " kyle " ] )
189
189
}
190
190
}
191
191
192
192
func testMismatchAudienceClaim( ) {
193
193
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.VEB_n06pTSLlTXPFkc46ARADJ9HXNUBUPo3VhL9RDe4 " // kyle
194
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) , audience: " maxine " ) )
194
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) , audience: " maxine " ) )
195
195
}
196
196
197
197
func testMissingAudienceClaim( ) {
198
198
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
199
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) , audience: " kyle " ) )
199
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) , audience: " kyle " ) )
200
200
}
201
201
202
202
// MARK: Signature verification
203
203
204
204
func testNoneAlgorithm( ) {
205
205
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
206
- assertSuccess ( decode ( jwt, algorithm: . None) ) { payload in
206
+ assertSuccess ( try decode ( jwt, algorithm: . None) ) { payload in
207
207
XCTAssertEqual ( payload as NSDictionary , [ " test " : " ing " ] )
208
208
}
209
209
}
210
210
211
211
func testNoneFailsWithSecretAlgorithm( ) {
212
212
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
213
- assertFailure ( decode ( jwt, algorithm: . HS256( " secret " ) ) )
213
+ assertFailure ( try decode ( jwt, algorithm: . HS256( " secret " ) ) )
214
214
}
215
215
216
216
func testMatchesAnyAlgorithm( ) {
217
217
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w. "
218
- assertFailure ( decode ( jwt, algorithms: [ . HS256( " anothersecret " ) , . HS256( " secret " ) ] ) )
218
+ assertFailure ( try decode ( jwt, algorithms: [ . HS256( " anothersecret " ) , . HS256( " secret " ) ] ) )
219
219
}
220
220
221
221
func testHS384Algorithm( ) {
222
222
let jwt = " eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4 "
223
- assertSuccess ( decode ( jwt, algorithm: . HS384( " secret " ) ) ) { payload in
223
+ assertSuccess ( try decode ( jwt, algorithm: . HS384( " secret " ) ) ) { payload in
224
224
XCTAssertEqual ( payload as NSDictionary , [ " some " : " payload " ] )
225
225
}
226
226
}
227
227
228
228
func testHS512Algorithm( ) {
229
229
let jwt = " eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA "
230
- assertSuccess ( decode ( jwt, algorithm: . HS512( " secret " ) ) ) { payload in
230
+ assertSuccess ( try decode ( jwt, algorithm: . HS512( " secret " ) ) ) { payload in
231
231
XCTAssertEqual ( payload as NSDictionary , [ " some " : " payload " ] )
232
232
}
233
233
}
234
234
}
235
235
236
236
// MARK: Helpers
237
237
238
- func assertSuccess( result: DecodeResult , closure: ( Payload -> ( ) ) ? = nil ) {
239
- switch result {
240
- case . Success( let payload) :
241
- if let closure = closure {
242
- closure ( payload)
243
- }
244
- case . Failure( let failure) :
245
- XCTFail ( " Failed to decode while expecting success. \( failure) " )
246
- break
238
+ func assertSuccess( @autoclosure decoder: ( ) throws -> Payload , closure: ( Payload -> ( ) ) ? = nil ) {
239
+ do {
240
+ let payload = try decoder ( )
241
+ closure ? ( payload)
242
+ } catch {
243
+ XCTFail ( " Failed to decode while expecting success. \( error) " )
247
244
}
248
245
}
249
246
250
- func assertFailure( result: DecodeResult , closure: ( InvalidToken -> ( ) ) ? = nil ) {
251
- switch result {
252
- case . Success:
253
- XCTFail ( " Decoded when expecting a failure. " )
254
- case . Failure( let failure) :
255
- if let closure = closure {
256
- closure ( failure)
257
- }
258
- break
247
+ func assertFailure( @autoclosure decoder: ( ) throws -> Payload , closure: ( InvalidToken -> ( ) ) ? = nil ) {
248
+ do {
249
+ _ = try decoder ( )
250
+ XCTFail ( " Decoding succeeded, expected a failure. " )
251
+ } catch let error as InvalidToken {
252
+ closure ? ( error)
253
+ } catch {
254
+ XCTFail ( " Unexpected error " )
259
255
}
260
256
}
261
257
262
- func assertDecodeError( result : DecodeResult , error: String ) {
263
- assertFailure ( result ) { failure in
258
+ func assertDecodeError( @ autoclosure decoder : ( ) throws -> Payload , error: String ) {
259
+ assertFailure ( try decoder ( ) ) { failure in
264
260
switch failure {
265
261
case . DecodeError( let decodeError) :
266
262
if decodeError != error {
0 commit comments