@@ -3,75 +3,68 @@ import XCTest
33@testable import JWT
44
55class 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-
136 func testDecodingValidJWT( ) throws {
147 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
158
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) !) )
1710 XCTAssertEqual ( claims [ " name " ] as? String , " Kyle " )
1811 }
1912
2013 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 " )
2215 }
2316
2417 // MARK: Disable verify
2518
2619 func testDisablingVerify( ) throws {
2720 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 " )
2922 }
3023
3124 // MARK: Issuer claim
3225
3326 func testSuccessfulIssuerValidation( ) throws {
3427 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ "
3528
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) !) )
3730 XCTAssertEqual ( claims. issuer, " fuller.li " )
3831 }
3932
4033 func testIncorrectIssuerValidation( ) {
4134 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 " ) )
4336 }
4437
4538 func testMissingIssuerValidation( ) {
4639 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 " ) )
4841 }
4942
5043 // MARK: Expiration claim
5144
5245 func testExpiredClaim( ) {
5346 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) !) ) )
5548 }
5649
5750 func testInvalidExpiaryClaim( ) {
5851 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) !) ) )
6053 }
6154
6255 func testUnexpiredClaim( ) throws {
6356 // If this just started failing, hello 2024!
6457 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgxODg0OTF9.EW7k-8Mvnv0GpvOKJalFRLoCB3a3xGG3i7hAZZXNAz0 "
6558
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) !) )
6760 XCTAssertEqual ( claims. expiration? . timeIntervalSince1970, 1728188491 )
6861 }
6962
7063 func testUnexpiredClaimString( ) throws {
7164 // If this just started failing, hello 2024!
7265 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk "
7366
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) !) )
7568 XCTAssertEqual ( claims. expiration? . timeIntervalSince1970, 1728188491 )
7669 }
7770
@@ -80,48 +73,48 @@ class DecodeTests: XCTestCase {
8073 func testNotBeforeClaim( ) throws {
8174 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0MjgxODk3MjB9.jFT0nXAJvEwyG6R7CMJlzNJb7FtZGv30QRZpYam5cvs "
8275
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) !) )
8477 XCTAssertEqual ( claims. notBefore? . timeIntervalSince1970, 1428189720 )
8578 }
8679
8780 func testNotBeforeClaimString( ) throws {
8881 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI "
8982
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) !) )
9184 XCTAssertEqual ( claims. notBefore? . timeIntervalSince1970, 1428189720 )
9285 }
9386
9487 func testInvalidNotBeforeClaim( ) {
9588 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 " )
9790 }
9891
9992 func testUnmetNotBeforeClaim( ) {
10093 // If this just started failing, hello 2024!
10194 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) !) ) )
10396 }
10497
10598 // MARK: Issued at claim
10699
107100 func testIssuedAtClaimInThePast( ) throws {
108101 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0MjgxODk3MjB9.I_5qjRcCUZVQdABLwG82CSuu2relSdIyJOyvXWUAJh4 "
109102
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) !) )
111104 XCTAssertEqual ( claims. issuedAt? . timeIntervalSince1970, 1428189720 )
112105 }
113106
114107 func testIssuedAtClaimInThePastString( ) throws {
115108 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNDI4MTg5NzIwIn0.M8veWtsY52oBwi7LRKzvNnzhjK0QBS8Su1r0atlns2k "
116109
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) !) )
118111 XCTAssertEqual ( claims. issuedAt? . timeIntervalSince1970, 1428189720 )
119112 }
120113
121114 func testIssuedAtClaimInTheFuture( ) {
122115 // If this just started failing, hello 2024!
123116 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) !) ) )
125118 }
126119
127120 func testInvalidIssuedAtClaim( ) {
@@ -134,58 +127,58 @@ class DecodeTests: XCTestCase {
134127
135128 func testAudiencesClaim( ) {
136129 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
138131 XCTAssertEqual ( payload. count, 1 )
139132 XCTAssertEqual ( payload [ " aud " ] as! [ String ] , [ " maxine " , " katie " ] )
140133 }
141134 }
142135
143136 func testAudienceClaim( ) {
144137 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
146139 XCTAssertEqual ( payload as! [ String : String ] , [ " aud " : " kyle " ] )
147140 }
148141 }
149142
150143 func testMismatchAudienceClaim( ) {
151144 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 " ) )
153146 }
154147
155148 func testMissingAudienceClaim( ) {
156149 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 " ) )
158151 }
159152
160153 // MARK: Signature verification
161154
162155 func testNoneAlgorithm( ) {
163156 let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
164- assertSuccess ( try decode ( jwt, algorithm: . none) as ClaimSet ) { payload in
157+ assertSuccess ( try decode ( jwt, algorithm: . none) ) { payload in
165158 XCTAssertEqual ( payload as! [ String : String ] , [ " test " : " ing " ] )
166159 }
167160 }
168161
169162 func testNoneFailsWithSecretAlgorithm( ) {
170163 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) !) ) )
172165 }
173166
174167 func testMatchesAnyAlgorithm( ) {
175168 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) !) ] ) )
177170 }
178171
179172 func testHS384Algorithm( ) {
180173 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
182175 XCTAssertEqual ( payload as! [ String : String ] , [ " some " : " payload " ] )
183176 }
184177 }
185178
186179 func testHS512Algorithm( ) {
187180 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
189182 XCTAssertEqual ( claims as! [ String : String ] , [ " some " : " payload " ] )
190183 }
191184 }
0 commit comments