Skip to content

Commit

Permalink
Add scope support, fix TokenRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
mohssenfathi committed May 24, 2024
1 parent 8f927ed commit e714397
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 33 deletions.
62 changes: 40 additions & 22 deletions Sources/UberAuth/Authorize/AuthorizationCodeAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
public let redirectURI: String

public typealias Completion = (Result<Client, UberAuthError>) -> Void

public static let defaultScopes = ["profile"]

// MARK: Internal Properties

var currentSession: AuthenticationSessioning?

typealias AuthenticationSessionBuilder = (ASPresentationAnchor, String, URL, AuthCompletion) -> (AuthenticationSessioning)

// MARK: Private Properties

private let applicationLauncher: ApplicationLaunching

private let authenticationSessionBuilder: AuthenticationSessionBuilder?

private var completion: Completion?

private let configurationProvider: ConfigurationProviding

var currentSession: AuthenticationSessioning?

private let pkce = PKCE()

private let presentationAnchor: ASPresentationAnchor
Expand All @@ -40,6 +46,8 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {

private let tokenManager: TokenManaging

private let scopes: [String]

// MARK: Initializers

public init(presentationAnchor: ASPresentationAnchor = .init(),
Expand All @@ -56,16 +64,19 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
}

self.applicationLauncher = UIApplication.shared
self.authenticationSessionBuilder = nil
self.clientID = clientID
self.presentationAnchor = presentationAnchor
self.redirectURI = redirectURI
self.responseParser = AuthorizationCodeResponseParser()
self.shouldExchangeAuthCode = shouldExchangeAuthCode
self.networkProvider = NetworkProvider(baseUrl: Constants.baseUrl)
self.tokenManager = TokenManager()
self.scopes = scopes
}

init(presentationAnchor: ASPresentationAnchor = .init(),
authenticationSessionBuilder: AuthenticationSessionBuilder? = nil,
scopes: [String] = AuthorizationCodeAuthProvider.defaultScopes,
shouldExchangeAuthCode: Bool = false,
configurationProvider: ConfigurationProviding = DefaultConfigurationProvider(),
Expand All @@ -83,6 +94,7 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
}

self.applicationLauncher = applicationLauncher
self.authenticationSessionBuilder = authenticationSessionBuilder
self.clientID = clientID
self.configurationProvider = configurationProvider
self.presentationAnchor = presentationAnchor
Expand All @@ -91,6 +103,7 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
self.shouldExchangeAuthCode = shouldExchangeAuthCode
self.networkProvider = networkProvider
self.tokenManager = tokenManager
self.scopes = scopes
}

// MARK: AuthProviding
Expand Down Expand Up @@ -183,7 +196,8 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
clientID: clientID,
codeChallenge: pkce.codeChallenge,
redirectURI: redirectURI,
requestURI: requestURI
requestURI: requestURI,
scopes: scopes
)

guard let url = request.url(baseUrl: Constants.baseUrl) else {
Expand All @@ -197,16 +211,17 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
return
}

currentSession = AuthenticationSession(
anchor: presentationAnchor,
callbackURLScheme: callbackURLScheme,
url: url,
completion: { [weak self] result in
guard let self else { return }
completion(result)
currentSession = nil
}
)
currentSession = authenticationSessionBuilder?(ASPresentationAnchor(), callbackURLScheme, url, completion) ??
AuthenticationSession(
anchor: presentationAnchor,
callbackURLScheme: callbackURLScheme,
url: url,
completion: { [weak self] result in
guard let self else { return }
completion(result)
currentSession = nil
}
)

currentSession?.start()
}
Expand Down Expand Up @@ -284,21 +299,24 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
clientID: clientID,
codeChallenge: pkce.codeChallenge,
redirectURI: redirectURI,
requestURI: requestURI
requestURI: requestURI,
scopes: scopes
)

guard let url = request.url(baseUrl: Constants.baseUrl) else {
completion?(false)
return
}

applicationLauncher.open(
url,
options: [:],
completionHandler: { opened in
completion?(opened)
}
)
DispatchQueue.main.async {
self.applicationLauncher.open(
url,
options: [:],
completionHandler: { opened in
completion?(opened)
}
)
}
}

private func executePar(prefill: Prefill?,
Expand Down
8 changes: 6 additions & 2 deletions Sources/UberAuth/Authorize/AuthorizeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ struct AuthorizeRequest: NetworkRequest {
private let clientID: String
private let redirectURI: String
private let requestURI: String?
private let scopes: [String]

// MARK: Initializers

init(app: UberApp?,
clientID: String,
codeChallenge: String,
redirectURI: String,
requestURI: String?) {
requestURI: String?,
scopes: [String] = []) {
self.app = app
self.clientID = clientID
self.codeChallenge = codeChallenge
self.redirectURI = redirectURI
self.requestURI = requestURI
self.scopes = scopes
}

// MARK: Request
Expand All @@ -45,7 +48,8 @@ struct AuthorizeRequest: NetworkRequest {
"code_challenge": codeChallenge,
"code_challenge_method": "S256",
"redirect_uri": redirectURI,
"request_uri": requestURI
"request_uri": requestURI,
"scope": scopes.joined(separator: " ")
]
.compactMapValues { $0 }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/UberAuth/Token/TokenRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct TokenRequest: NetworkRequest {

typealias Response = AccessToken

var parameters: [String : String]? {
var body: [String : String]? {
[
"code": authorizationCode,
"client_id": clientID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ final class AuthorizationCodeAuthProviderTests: XCTestCase {
}

func test_executeNativeLogin_triggersApplicationLauncher() {


let expectation = XCTestExpectation()

let applicationLauncher = ApplicationLaunchingMock()
applicationLauncher.openHandler = { _, _, _ in }
applicationLauncher.openHandler = { _, _, completion in
expectation.fulfill()
completion?(true)
}

configurationProvider.isInstalledHandler = { _, _ in
true
Expand All @@ -180,15 +185,13 @@ final class AuthorizationCodeAuthProviderTests: XCTestCase {
applicationLauncher: applicationLauncher
)

XCTAssertEqual(applicationLauncher.openCallCount, 0)

provider.execute(
authDestination: .native(appPriority: UberApp.allCases),
prefill: nil,
completion: { _ in }
)

XCTAssertEqual(applicationLauncher.openCallCount, 1)
wait(for: [expectation], timeout: 0.2)
}

func test_executeNativeLogin_noDestinations_triggersInAppLogin() {
Expand Down Expand Up @@ -226,21 +229,32 @@ final class AuthorizationCodeAuthProviderTests: XCTestCase {
configurationProvider.isInstalledHandler = { _, _ in
true
}

let expectation = XCTestExpectation()

let authenticationSession = AuthenticationSessioningMock()
let authenticationSessionBuilder: AuthorizationCodeAuthProvider.AuthenticationSessionBuilder = { _, _, _, _ in
expectation.fulfill()
return authenticationSession
}

let provider = AuthorizationCodeAuthProvider(
authenticationSessionBuilder: authenticationSessionBuilder,
configurationProvider: configurationProvider,
applicationLauncher: applicationLauncher
)

XCTAssertNil(provider.currentSession)
XCTAssertEqual(authenticationSession.startCallCount, 0)

provider.execute(
authDestination: .native(appPriority: UberApp.allCases),
prefill: nil,
completion: { _ in }
)

XCTAssertNotNil(provider.currentSession)
wait(for: [expectation], timeout: 0.2)

XCTAssertEqual(authenticationSession.startCallCount, 1)
}

func test_handleResponse_true_callsResponseParser() {
Expand Down

0 comments on commit e714397

Please sign in to comment.