Skip to content

Commit 735c180

Browse files
authored
Merge pull request #305 from mohssenfathi/authorize-code-challenge
Limit code_challenge to shouldExchangeAuthCode only
2 parents 5d6cf94 + b8bb8f4 commit 735c180

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

Sources/UberAuth/Authorize/AuthorizationCodeAuthProvider.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
194194
let request = AuthorizeRequest(
195195
app: nil,
196196
clientID: clientID,
197-
codeChallenge: pkce.codeChallenge,
197+
codeChallenge: shouldExchangeAuthCode ? pkce.codeChallenge : nil,
198198
redirectURI: redirectURI,
199199
requestURI: requestURI,
200200
scopes: scopes
@@ -297,7 +297,7 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
297297
let request = AuthorizeRequest(
298298
app: app,
299299
clientID: clientID,
300-
codeChallenge: pkce.codeChallenge,
300+
codeChallenge: shouldExchangeAuthCode ? pkce.codeChallenge : nil,
301301
redirectURI: redirectURI,
302302
requestURI: requestURI,
303303
scopes: scopes

Sources/UberAuth/Authorize/AuthorizeRequest.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct AuthorizeRequest: NetworkRequest {
1515
// MARK: Private Properties
1616

1717
private let app: UberApp?
18-
private let codeChallenge: String
18+
private let codeChallenge: String?
1919
private let clientID: String
2020
private let redirectURI: String
2121
private let requestURI: String?
@@ -25,7 +25,7 @@ struct AuthorizeRequest: NetworkRequest {
2525

2626
init(app: UberApp?,
2727
clientID: String,
28-
codeChallenge: String,
28+
codeChallenge: String?,
2929
redirectURI: String,
3030
requestURI: String?,
3131
scopes: [String] = []) {
@@ -46,7 +46,7 @@ struct AuthorizeRequest: NetworkRequest {
4646
"response_type": "code",
4747
"client_id": clientID,
4848
"code_challenge": codeChallenge,
49-
"code_challenge_method": "S256",
49+
"code_challenge_method": codeChallenge != nil ? "S256" : nil,
5050
"redirect_uri": redirectURI,
5151
"request_uri": requestURI,
5252
"scope": scopes.joined(separator: " ")

examples/UberSDK/UberSDKTests/UberAuth/AuthorizationCodeAuthProviderTests.swift

+79
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ final class AuthorizationCodeAuthProviderTests: XCTestCase {
4848

4949
XCTAssertEqual(authSession.startCallCount, 0)
5050
}
51+
52+
func test_executeInAppLogin_noTokenExchange_doesNotIncludeCodeChallenge() {
53+
54+
configurationProvider.isInstalledHandler = { _, _ in
55+
true
56+
}
57+
58+
let applicationLauncher = ApplicationLaunchingMock()
59+
applicationLauncher.openHandler = { _, _, completion in
60+
completion?(true)
61+
}
62+
63+
var hasCalledAuthenticationSessionBuilder: Bool = false
64+
65+
let authenticationSessionBuilder: AuthorizationCodeAuthProvider.AuthenticationSessionBuilder = { _, _, url, _ in
66+
XCTAssertFalse(url.absoluteString.contains("code_challenge"))
67+
XCTAssertFalse(url.absoluteString.contains("code_challenge_method"))
68+
hasCalledAuthenticationSessionBuilder = true
69+
return AuthenticationSessioningMock()
70+
}
71+
72+
let provider = AuthorizationCodeAuthProvider(
73+
authenticationSessionBuilder: authenticationSessionBuilder,
74+
shouldExchangeAuthCode: false,
75+
configurationProvider: configurationProvider,
76+
applicationLauncher: applicationLauncher
77+
)
78+
79+
provider.execute(
80+
authDestination: .inApp,
81+
completion: { result in }
82+
)
83+
84+
let url = URL(string: "test://app?code=123")!
85+
_ = provider.handle(response: url)
86+
87+
XCTAssertTrue(hasCalledAuthenticationSessionBuilder)
88+
}
5189

5290
func test_execute_existingSession_returnsExistingAuthSessionError() {
5391
let provider = AuthorizationCodeAuthProvider(
@@ -257,6 +295,47 @@ final class AuthorizationCodeAuthProviderTests: XCTestCase {
257295
XCTAssertEqual(authenticationSession.startCallCount, 1)
258296
}
259297

298+
func test_executeNativeLogin_noTokenExchange_doesNotIncludeCodeChallenge() {
299+
300+
let applicationLauncher = ApplicationLaunchingMock()
301+
applicationLauncher.openHandler = { url, _, completion in
302+
XCTAssertFalse(url.absoluteString.contains("code_challenge"))
303+
XCTAssertFalse(url.absoluteString.contains("code_challenge_method"))
304+
completion?(false)
305+
}
306+
307+
configurationProvider.isInstalledHandler = { _, _ in
308+
true
309+
}
310+
311+
let expectation = XCTestExpectation()
312+
313+
let authenticationSession = AuthenticationSessioningMock()
314+
let authenticationSessionBuilder: AuthorizationCodeAuthProvider.AuthenticationSessionBuilder = { _, _, _, _ in
315+
expectation.fulfill()
316+
return authenticationSession
317+
}
318+
319+
let provider = AuthorizationCodeAuthProvider(
320+
authenticationSessionBuilder: authenticationSessionBuilder,
321+
shouldExchangeAuthCode: false,
322+
configurationProvider: configurationProvider,
323+
applicationLauncher: applicationLauncher
324+
)
325+
326+
XCTAssertEqual(applicationLauncher.openCallCount, 0)
327+
328+
provider.execute(
329+
authDestination: .native(appPriority: [.eats]),
330+
prefill: nil,
331+
completion: { _ in }
332+
)
333+
334+
wait(for: [expectation], timeout: 0.2)
335+
336+
XCTAssertEqual(applicationLauncher.openCallCount, 1)
337+
}
338+
260339
func test_handleResponse_true_callsResponseParser() {
261340

262341
let responseParser = AuthorizationCodeResponseParsingMock()

0 commit comments

Comments
 (0)