1
+ import Foundation
1
2
import XCTest
2
3
import JWT
3
4
4
5
class JWTEncodeTests : XCTestCase {
5
6
func testEncodingJWT( ) {
6
7
let payload = [ " name " : " Kyle " ] as Payload
7
- let jwt = JWT . encode ( payload, algorithm: . hs256( " secret " ) )
8
+ let jwt = JWT . encode ( payload, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) )
8
9
let fixture = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
9
10
XCTAssertEqual ( jwt, fixture)
10
11
}
11
12
12
13
func testEncodingWithBuilder( ) {
13
- let algorithm = Algorithm . hs256 ( " secret " )
14
+ let algorithm = Algorithm . hs256 ( " secret " . data ( using : . utf8 ) ! )
14
15
let jwt = JWT . encode ( algorithm) { builder in
15
16
builder. issuer = " fuller.li "
16
17
}
@@ -77,7 +78,7 @@ class JWTDecodeTests : XCTestCase {
77
78
func testDecodingValidJWT( ) {
78
79
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
79
80
80
- assertSuccess ( try JWT . decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
81
+ assertSuccess ( try JWT . decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
81
82
XCTAssertEqual ( payload as NSDictionary , [ " name " : " Kyle " ] )
82
83
}
83
84
}
@@ -97,45 +98,45 @@ class JWTDecodeTests : XCTestCase {
97
98
98
99
func testSuccessfulIssuerValidation( ) {
99
100
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ "
100
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) , issuer: " fuller.li " ) ) { payload in
101
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , issuer: " fuller.li " ) ) { payload in
101
102
XCTAssertEqual ( payload as NSDictionary , [ " iss " : " fuller.li " ] )
102
103
}
103
104
}
104
105
105
106
func testIncorrectIssuerValidation( ) {
106
107
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI "
107
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) , issuer: " querykit.org " ) )
108
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , issuer: " querykit.org " ) )
108
109
}
109
110
110
111
func testMissingIssuerValidation( ) {
111
112
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
112
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) , issuer: " fuller.li " ) )
113
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , issuer: " fuller.li " ) )
113
114
}
114
115
115
116
// MARK: Expiration claim
116
117
117
118
func testExpiredClaim( ) {
118
119
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0MjgxODg0OTF9.cy6b2szsNkKnHFnz2GjTatGjoHBTs8vBKnPGZgpp91I "
119
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) ) )
120
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) )
120
121
}
121
122
122
123
func testInvalidExpiaryClaim( ) {
123
124
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOlsiMTQyODE4ODQ5MSJdfQ.OwF-wd3THjxrEGUhh6IdnNhxQZ7ydwJ3Z6J_dfl9MBs "
124
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) ) )
125
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) )
125
126
}
126
127
127
128
func testUnexpiredClaim( ) {
128
129
// If this just started failing, hello 2024!
129
130
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.EW7k-8Mvnv0GpvOKJalFRLoCB3a3xGG3i7hAZZXNAz0 "
130
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
131
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
131
132
XCTAssertEqual ( payload as NSDictionary , [ " exp " : 1728188491 ] )
132
133
}
133
134
}
134
135
135
136
func testUnexpiredClaimString( ) {
136
137
// If this just started failing, hello 2024!
137
138
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk "
138
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
139
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
139
140
XCTAssertEqual ( payload as NSDictionary , [ " exp " : " 1728188491 " ] )
140
141
}
141
142
}
@@ -144,81 +145,81 @@ class JWTDecodeTests : XCTestCase {
144
145
145
146
func testNotBeforeClaim( ) {
146
147
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0MjgxODk3MjB9.jFT0nXAJvEwyG6R7CMJlzNJb7FtZGv30QRZpYam5cvs "
147
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
148
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
148
149
XCTAssertEqual ( payload as NSDictionary , [ " nbf " : 1428189720 ] )
149
150
}
150
151
}
151
152
152
153
func testNotBeforeClaimString( ) {
153
154
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI "
154
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
155
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
155
156
XCTAssertEqual ( payload as NSDictionary , [ " nbf " : " 1428189720 " ] )
156
157
}
157
158
}
158
159
159
160
func testInvalidNotBeforeClaim( ) {
160
161
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOlsxNDI4MTg5NzIwXX0.PUL1FQubzzJa4MNXe2D3d5t5cMaqFr3kYlzRUzly-C8 "
161
- assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " ) ) , error: " Not before claim (nbf) must be an integer " )
162
+ assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) , error: " Not before claim (nbf) must be an integer " )
162
163
}
163
164
164
165
func testUnmetNotBeforeClaim( ) {
165
166
// If this just started failing, hello 2024!
166
167
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MjgxODg0OTF9.Tzhu1tu-7BXcF5YEIFFE1Vmg4tEybUnaz58FR4PcblQ "
167
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) ) )
168
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) )
168
169
}
169
170
170
171
// MARK: Issued at claim
171
172
172
173
func testIssuedAtClaimInThePast( ) {
173
174
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MjgxODk3MjB9.I_5qjRcCUZVQdABLwG82CSuu2relSdIyJOyvXWUAJh4 "
174
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
175
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
175
176
XCTAssertEqual ( payload as NSDictionary , [ " iat " : 1428189720 ] )
176
177
}
177
178
}
178
179
179
180
func testIssuedAtClaimInThePastString( ) {
180
181
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k "
181
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) ) ) { payload in
182
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
182
183
XCTAssertEqual ( payload as NSDictionary , [ " iat " : " 1428189720 " ] )
183
184
}
184
185
}
185
186
186
187
func testIssuedAtClaimInTheFuture( ) {
187
188
// If this just started failing, hello 2024!
188
189
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MjgxODg0OTF9.owHiJyJmTcW1lBW5y_Rz3iBfSbcNiXlbZ2fY9qR7-aU "
189
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) ) )
190
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) )
190
191
}
191
192
192
193
func testInvalidIssuedAtClaim( ) {
193
194
// If this just started failing, hello 2024!
194
195
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOlsxNzI4MTg4NDkxXX0.ND7QMWtLkXDXH38OaXM3SQgLo3Z5TNgF_pcfWHV_alQ "
195
- assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " ) ) , error: " Issued at claim (iat) must be an integer " )
196
+ assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) , error: " Issued at claim (iat) must be an integer " )
196
197
}
197
198
198
199
// MARK: Audience claims
199
200
200
201
func testAudiencesClaim( ) {
201
202
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibWF4aW5lIiwia2F0aWUiXX0.-PKvdNLCClrWG7CvesHP6PB0-vxu-_IZcsYhJxBy5JM "
202
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) , audience: " maxine " ) ) { payload in
203
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , audience: " maxine " ) ) { payload in
203
204
XCTAssertEqual ( payload as NSDictionary , [ " aud " : [ " maxine " , " katie " ] ] )
204
205
}
205
206
}
206
207
207
208
func testAudienceClaim( ) {
208
209
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.dpgH4JOwueReaBoanLSxsGTc7AjKUvo7_M1sAfy_xVE "
209
- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " ) , audience: " kyle " ) ) { payload in
210
+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , audience: " kyle " ) ) { payload in
210
211
XCTAssertEqual ( payload as NSDictionary , [ " aud " : " kyle " ] )
211
212
}
212
213
}
213
214
214
215
func testMismatchAudienceClaim( ) {
215
216
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.VEB_n06pTSLlTXPFkc46ARADJ9HXNUBUPo3VhL9RDe4 " // kyle
216
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) , audience: " maxine " ) )
217
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , audience: " maxine " ) )
217
218
}
218
219
219
220
func testMissingAudienceClaim( ) {
220
221
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w "
221
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) , audience: " kyle " ) )
222
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) , audience: " kyle " ) )
222
223
}
223
224
224
225
// MARK: Signature verification
@@ -232,24 +233,24 @@ class JWTDecodeTests : XCTestCase {
232
233
233
234
func testNoneFailsWithSecretAlgorithm( ) {
234
235
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
235
- assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " ) ) )
236
+ assertFailure ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using : . utf8 ) ! ) ) )
236
237
}
237
238
238
239
func testMatchesAnyAlgorithm( ) {
239
240
let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w. "
240
- assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " ) , . hs256( " secret " ) ] ) )
241
+ assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " . data ( using : . utf8 ) ! ) , . hs256( " secret " . data ( using : . utf8 ) ! ) ] ) )
241
242
}
242
243
243
244
func testHS384Algorithm( ) {
244
245
let jwt = " eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4 "
245
- assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " ) ) ) { payload in
246
+ assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
246
247
XCTAssertEqual ( payload as NSDictionary , [ " some " : " payload " ] )
247
248
}
248
249
}
249
250
250
251
func testHS512Algorithm( ) {
251
252
let jwt = " eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA "
252
- assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " ) ) ) { payload in
253
+ assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " . data ( using : . utf8 ) ! ) ) ) { payload in
253
254
XCTAssertEqual ( payload as NSDictionary , [ " some " : " payload " ] )
254
255
}
255
256
}
0 commit comments