-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTokenTests.swift
More file actions
296 lines (255 loc) · 13.4 KB
/
Copy pathTokenTests.swift
File metadata and controls
296 lines (255 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//
import XCTest
#if !COCOAPODS
import JSON
#endif
@testable import AuthFoundation
@testable import TestCommon
fileprivate struct MockTokenRequest: OAuth2TokenRequest, IDTokenValidatorContext {
var nonce: String?
var maxAge: TimeInterval?
let context: (any AuthenticationContext)? = nil
let openIdConfiguration: OpenIdConfiguration
let clientConfiguration: OAuth2Client.Configuration
let url: URL
let category = OAuth2APIRequestCategory.token
var tokenValidatorContext: any IDTokenValidatorContext { self }
var bodyParameters: [String: any APIRequestArgument]?
}
final class TokenTests: XCTestCase {
var openIdConfiguration: OpenIdConfiguration!
let configuration = OAuth2Client.Configuration(issuerURL: URL(string: "https://example.com")!,
clientId: "clientid",
scope: "openid")
override func setUpWithError() throws {
JWK.validator = MockJWKValidator()
Token.idTokenValidator = MockIDTokenValidator()
Token.accessTokenValidator = MockTokenHashValidator()
openIdConfiguration = try OpenIdConfiguration.jsonDecoder.decode(
OpenIdConfiguration.self,
from: try data(from: .module,
for: "openid-configuration",
in: "MockResponses"))
}
override func tearDownWithError() throws {
JWK.resetToDefault()
Token.resetToDefault()
}
func testTokenContextNilSettings() throws {
let context = Token.Context(configuration: configuration, clientSettings: nil)
XCTAssertEqual(context.configuration, configuration)
let data = try JSONEncoder().encode(context)
let decodedContext = try JSONDecoder().decode(Token.Context.self, from: data)
XCTAssertEqual(context, decodedContext)
}
func testTokenContextStringSettings() throws {
let context = Token.Context(configuration: configuration,
clientSettings: ["foo": "bar"])
XCTAssertEqual(context.clientSettings, ["foo": "bar"])
let data = try JSONEncoder().encode(context)
let decodedContext = try JSONDecoder().decode(Token.Context.self, from: data)
XCTAssertEqual(context, decodedContext)
}
func testTokenContextCodingUserInfoKeySettings() throws {
let context = Token.Context(configuration: configuration,
clientSettings: [
CodingUserInfoKey.apiClientConfiguration: "bar",
])
XCTAssertEqual(context.clientSettings, ["apiClientConfiguration": "bar"])
let data = try JSONEncoder().encode(context)
let decodedContext = try JSONDecoder().decode(Token.Context.self, from: data)
XCTAssertEqual(context, decodedContext)
}
func testNilScope() throws {
let data = data(for: """
{
"token_type": "Bearer",
"expires_in": 3600,
"access_token": "\(JWT.mockAccessToken)"
}
""")
let decoder = defaultJSONDecoder
decoder.userInfo = [
.apiClientConfiguration: configuration,
]
let token = try decoder.decode(Token.self, from: data)
XCTAssertNil(token.scope)
}
func testToken() throws {
let token = try! Token(id: "TokenId",
issuedAt: Date(),
tokenType: "Bearer",
expiresIn: 3600,
accessToken: "the_access_token",
scope: "openid profile offline_access",
refreshToken: "the_refresh_token",
idToken: nil,
deviceSecret: "the_device_secret",
context: Token.Context(configuration: configuration,
clientSettings: [:]))
XCTAssertEqual(token.token(of: .accessToken), token.accessToken)
XCTAssertEqual(token.token(of: .refreshToken), token.refreshToken)
XCTAssertEqual(token.token(of: .deviceSecret), token.deviceSecret)
let data = try JSONEncoder().encode(token)
let decodedToken = try JSONDecoder().decode(Token.self, from: data)
XCTAssertEqual(token, decodedToken)
}
func testTokenNilContext() throws {
let decoder = defaultJSONDecoder
decoder.userInfo = [:]
XCTAssertThrowsError(try decoder.decode(Token.self,
from: try data(from: .module,
for: "token",
in: "MockResponses")))
}
func testMFAAttestationToken() throws {
let decoder = defaultJSONDecoder
decoder.userInfo = [
.apiClientConfiguration: configuration,
.clientSettings: [
"acr_values": "urn:okta:app:mfa:attestation"
]
]
let token = try decoder.decode(Token.self,
from: try data(from: .module,
for: "token-mfa_attestation",
in: "MockResponses"))
XCTAssertTrue(token.accessToken.isEmpty)
}
func testMFAAttestationTokenFailed() throws {
let decoder = defaultJSONDecoder
decoder.userInfo = [
.apiClientConfiguration: configuration,
]
XCTAssertThrowsError(try decoder.decode(Token.self,
from: try data(from: .module,
for: "token-no_access_token",
in: "MockResponses")))
}
func testTokenEquality() throws {
var token1 = Token.mockToken()
var token2 = Token.mockToken()
XCTAssertEqual(token1, token2)
token2 = Token.mockToken(refreshToken: "SomethingDifferent")
XCTAssertNotEqual(token1, token2)
token1 = Token.mockToken(deviceSecret: "First")
token2 = Token.mockToken(deviceSecret: "Second")
XCTAssertNotEqual(token1, token2)
}
func testTokenFromRefreshToken() async throws {
let client = try mockClient()
nonisolated(unsafe) var tokenResult: Token?
let wait = expectation(description: "Token exchange")
Token.from(refreshToken: "the_refresh_token", using: client) { result in
switch result {
case .success(let success):
tokenResult = success
case .failure(let failure):
XCTAssertNil(failure)
}
wait.fulfill()
}
await fulfillment(of: [wait], timeout: .standard)
let token = try XCTUnwrap(tokenResult)
XCTAssertEqual(token.token(of: .accessToken), String.mockAccessToken)
XCTAssertNotEqual(token.id, Token.RefreshRequest.placeholderId)
}
func testTokenFromV1Data() throws {
// Note: The following is a redacted version of the raw payload saved to
// the keychain from a version of the SDK where the V1 coding keys
// were used. This is to ensure mimgration works as expected.
let storedData = """
{"scope":"profile offline_access openid","context":{"configuration":{"scopes":"openid profile offline_access","baseURL":"https://example.com/oauth2/default","clientId":"0oatheclientid","authentication":{"none":{}},"discoveryURL":"https://example.com/oauth2/default/.well-known/openid-configuration"},"clientSettings":{"client_id":"0oatheclientid","scope":"openid profile offline_access","redirect_uri":"com.example:/callback"}},"accessToken":"\(JWT.mockAccessToken)","tokenType":"Bearer","idToken":"\(JWT.mockIDToken)","id":"1834AF8D-BC97-4CCE-876F-300314784D5B","expiresIn":3600,"refreshToken":"refresh-kl2QWaYgyHaLkCdc6exjsowP9KUTW1ilAWC","deviceSecret":"device_lh4nMHgcUWLJIVgkcbQwnnSI2F8JMwNshLoa","issuedAt":744576826.0011461}
"""
let data = try XCTUnwrap(storedData.data(using: .utf8))
let token = try JSONDecoder().decode(Token.self, from: data)
XCTAssertEqual(token.id, "1834AF8D-BC97-4CCE-876F-300314784D5B")
XCTAssertEqual(token.accessToken, JWT.mockAccessToken)
XCTAssertEqual(token.idToken?.rawValue, JWT.mockIDToken)
XCTAssertEqual(token.scope, ["profile", "offline_access", "openid"])
XCTAssertEqual(token.expiresIn, 3600)
XCTAssertEqual(token.refreshToken, "refresh-kl2QWaYgyHaLkCdc6exjsowP9KUTW1ilAWC")
XCTAssertEqual(token.deviceSecret, "device_lh4nMHgcUWLJIVgkcbQwnnSI2F8JMwNshLoa")
XCTAssertEqual(token.issuedAt?.timeIntervalSinceReferenceDate, 744576826.0011461)
XCTAssertEqual(token.context.configuration.scope, ["openid", "profile", "offline_access"])
XCTAssertEqual(token.context.configuration.redirectUri?.absoluteString, "com.example:/callback")
XCTAssertEqual(token.context, .init(configuration: .init(issuerURL: try XCTUnwrap(URL(string: "https://example.com/oauth2/default")),
clientId: "0oatheclientid",
scope: ["openid", "profile", "offline_access"],
redirectUri: URL(string: "com.example:/callback"),
authentication: .none),
clientSettings: nil))
XCTAssertEqual(token.jsonPayload.jsonValue, try JSON([
"scope": "profile offline_access openid",
"access_token": JWT.mockAccessToken,
"token_type": "Bearer",
"id_token": JWT.mockIDToken,
"expires_in": 3600,
"refresh_token": "refresh-kl2QWaYgyHaLkCdc6exjsowP9KUTW1ilAWC",
"device_secret":"device_lh4nMHgcUWLJIVgkcbQwnnSI2F8JMwNshLoa",
]))
}
func testTokenClaims() throws {
var token: Token!
token = try! Token(id: "TokenId",
issuedAt: Date(),
tokenType: "Bearer",
expiresIn: 3600,
accessToken: "the_access_token",
scope: "openid profile offline_access",
refreshToken: "the_refresh_token",
idToken: nil,
deviceSecret: "the_device_secret",
context: Token.Context(configuration: configuration,
clientSettings: [:]))
XCTAssertEqual(token.allClaims.sorted(), [
"expires_in",
"token_type",
"access_token",
"scope",
"refresh_token",
"device_secret",
].sorted())
XCTAssertEqual(token[.accessToken], "the_access_token")
}
func testTokenFromRefreshTokenAsync() async throws {
let client = try mockClient()
let token = try await Token.from(refreshToken: "the_refresh_token", using: client)
XCTAssertEqual(token.token(of: .accessToken), String.mockAccessToken)
XCTAssertNotEqual(token.id, Token.RefreshRequest.placeholderId)
}
func mockClient() throws -> OAuth2Client {
let urlSession = URLSessionMock()
urlSession.expect("https://example.com/.well-known/openid-configuration",
data: try data(from: .module, for: "openid-configuration", in: "MockResponses"),
contentType: "application/json")
urlSession.expect("https://example.com/oauth2/v1/keys?client_id=clientId",
data: try data(from: .module, for: "keys", in: "MockResponses"),
contentType: "application/json")
urlSession.expect("https://example.com/oauth2/v1/token",
data: data(for: """
{
"token_type": "Bearer",
"expires_in": 3000,
"access_token": "\(String.mockAccessToken)",
"scope": "openid profile offline_access",
"refresh_token": "therefreshtoken",
"id_token": "\(String.mockIdToken)"
}
"""))
return OAuth2Client(issuerURL: URL(string: "https://example.com/")!,
clientId: "clientId",
scope: "openid profile offline_access",
session: urlSession)
}
}