Skip to content

Commit

Permalink
Consolidate UberRides and UberAuth configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
mohssenfathi committed Aug 8, 2024
1 parent 455f578 commit b39135a
Show file tree
Hide file tree
Showing 26 changed files with 267 additions and 500 deletions.
36 changes: 1 addition & 35 deletions Sources/UberAuth/AuthDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import Foundation
import UberCore

/// An enum that describes where login should occur
public enum AuthDestination {
Expand All @@ -19,38 +20,3 @@ public enum AuthDestination {
appPriority: [UberApp] = [.rides, .eats, .driver]
)
}

/// An enum corresponding to each Uber client application
public enum UberApp: CaseIterable {

// Uber Eats
case eats

// Uber Driver
case driver

// Uber
case rides

var deeplinkScheme: String {
switch self {
case .eats:
return "ubereats"
case .driver:
return "uberdriver"
case .rides:
return "uber"
}
}

var urlIdentifier: String {
switch self {
case .eats:
return "eats"
case .driver:
return "drivers"
case .rides:
return "riders"
}
}
}
29 changes: 6 additions & 23 deletions Sources/UberAuth/Authorize/AuthorizationCodeAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,12 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
scopes: [String] = AuthorizationCodeAuthProvider.defaultScopes,
shouldExchangeAuthCode: Bool = false,
prompt: Prompt? = nil) {
self.configurationProvider = DefaultConfigurationProvider()

guard let clientID: String = configurationProvider.clientID else {
preconditionFailure("No clientID specified in Info.plist")
}

guard let redirectURI: String = configurationProvider.redirectURI else {
preconditionFailure("No redirectURI specified in Info.plist")
}

self.configurationProvider = ConfigurationProvider()
self.applicationLauncher = UIApplication.shared
self.authenticationSessionBuilder = nil
self.clientID = clientID
self.clientID = configurationProvider.clientID
self.presentationAnchor = presentationAnchor
self.redirectURI = redirectURI
self.redirectURI = configurationProvider.redirectURI
self.responseParser = AuthorizationCodeResponseParser()
self.shouldExchangeAuthCode = shouldExchangeAuthCode
self.networkProvider = NetworkProvider(baseUrl: Constants.baseUrl)
Expand All @@ -84,26 +75,18 @@ public final class AuthorizationCodeAuthProvider: AuthProviding {
scopes: [String] = AuthorizationCodeAuthProvider.defaultScopes,
prompt: Prompt? = nil,
shouldExchangeAuthCode: Bool = false,
configurationProvider: ConfigurationProviding = DefaultConfigurationProvider(),
configurationProvider: ConfigurationProviding = ConfigurationProvider(),
applicationLauncher: ApplicationLaunching = UIApplication.shared,
responseParser: AuthorizationCodeResponseParsing = AuthorizationCodeResponseParser(),
networkProvider: NetworkProviding = NetworkProvider(baseUrl: Constants.baseUrl),
tokenManager: TokenManaging = TokenManager()) {

guard let clientID: String = configurationProvider.clientID else {
preconditionFailure("No clientID specified in Info.plist")
}

guard let redirectURI: String = configurationProvider.redirectURI else {
preconditionFailure("No redirectURI specified in Info.plist")
}

self.applicationLauncher = applicationLauncher
self.authenticationSessionBuilder = authenticationSessionBuilder
self.clientID = clientID
self.clientID = configurationProvider.clientID
self.configurationProvider = configurationProvider
self.presentationAnchor = presentationAnchor
self.redirectURI = redirectURI
self.redirectURI = configurationProvider.redirectURI
self.responseParser = responseParser
self.shouldExchangeAuthCode = shouldExchangeAuthCode
self.networkProvider = networkProvider
Expand Down
1 change: 1 addition & 0 deletions Sources/UberAuth/Authorize/AuthorizeRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import Foundation
import UberCore

///
/// Defines a network request conforming to the OAuth 2.0 standard authorization request
Expand Down
54 changes: 0 additions & 54 deletions Sources/UberAuth/ConfigurationProvider.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/UberAuth/Errors/UberAuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import AuthenticationServices
import UberCore
import Foundation

public enum UberAuthError: Error {
Expand Down
4 changes: 3 additions & 1 deletion Sources/UberAuth/Token/TokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public extension TokenManaging {

public final class TokenManager: TokenManaging {

public static let defaultAccessTokenIdentifier = "UberAccessTokenKey"
public static let defaultAccessTokenIdentifier: String = "UberAccessTokenKey"

public static let defaultKeychainAccessGroup: String = ""

private let keychainUtility: KeychainUtilityProtocol

Expand Down
96 changes: 96 additions & 0 deletions Sources/UberCore/ConfigurationProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright © Uber Technologies, Inc. All rights reserved.
//


import Foundation
import UIKit
import UberCore

/// @mockable
public protocol ConfigurationProviding {
var clientID: String { get }
var redirectURI: String { get }
var sdkVersion: String { get }
var serverToken: String? { get }
static var isSandbox: Bool { get }

func isInstalled(app: UberApp, defaultIfUnregistered: Bool) -> Bool
}

public struct ConfigurationProvider: ConfigurationProviding {

// MARK: Private Properties

private let parser: PlistParser

// MARK: Initializers

public init() {
let parser = PlistParser(plistName: "Info")
self.parser = parser

guard let contents: [String: String] = parser[ConfigurationKey.base] else {
preconditionFailure("Configuration item not found: \(ConfigurationKey.base)")
}

guard let clientID = contents[ConfigurationKey.clientID] as? String else {
preconditionFailure("Configuration item not found: \(ConfigurationKey.base)/\(ConfigurationKey.clientID)")
}

guard let redirectURI = contents[ConfigurationKey.redirectURI] as? String else {
preconditionFailure("Configuration item not found: \(ConfigurationKey.base)/\(ConfigurationKey.clientID)")
}

self.clientID = clientID
self.redirectURI = redirectURI
Self.isSandbox = Bool(contents[ConfigurationKey.sandbox] ?? "") ?? false
self.serverToken = contents[ConfigurationKey.serverToken]
}

// MARK: ConfigurationProviding

public let clientID: String

public let redirectURI: String

public let serverToken: String?

public static var isSandbox: Bool = false

/// Attempts to determine if the provided `UberApp` is installed on the current device.
/// First checks the Info.plist to see if the required url schemes are registered. If not registered,
/// returns `defaultIfUnregistered`.
/// If registered, returns whether the scheme can be opened, indicating if the app is installed.
///
/// - Parameters:
/// - app: The Uber application to check
/// - defaultIfUnregistered: The boolean value to return if the app's url scheme is not registered in the Info.plist
/// - Returns: A boolean indicating if the app is installed
public func isInstalled(app: UberApp, defaultIfUnregistered: Bool) -> Bool {
guard let registeredSchemes: [String] = parser["LSApplicationQueriesSchemes"],
registeredSchemes.contains(where: { $0 == app.deeplinkScheme }),
let url = URL(string: "\(app.deeplinkScheme)://") else {
return defaultIfUnregistered
}
return UIApplication.shared.canOpenURL(url)
}

/// The current version of the SDK as a string
public var sdkVersion: String {
guard let version = Bundle(for: UberButton.self).object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else {
return "Unknown"
}
return version
}

// MARK: Constants

private enum ConfigurationKey {
static let base = "Uber"
static let clientID = "ClientID"
static let redirectURI = "RedirectURI"
static let serverToken = "RedirectURI"
static let sandbox = "Sandbox"
}
}
4 changes: 2 additions & 2 deletions Sources/UberCore/PlistParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct PlistParser {
/// - name: The name of the plist to access
/// - bundle: The bundle the plist is contained in
public init(plistName: String,
bundle: Bundle = .main) {
bundle: Bundle = .main) {
guard let plistUrl = bundle.url(forResource: plistName, withExtension: "plist") else {
self.contents = [:]
return
Expand All @@ -39,7 +39,7 @@ public struct PlistParser {
/// - name: The name of the plist to access
/// - bundle: The bundle the plist is contained in
public init(name: String,
bundle: Bundle = .main) throws {
bundle: Bundle = .main) throws {
guard let plistUrl = bundle.url(forResource: name, withExtension: "plist") else {
throw ParserError.noResourceFound
}
Expand Down
41 changes: 41 additions & 0 deletions Sources/UberCore/UberApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright © Uber Technologies, Inc. All rights reserved.
//


import Foundation

/// An enum corresponding to each Uber client application
public enum UberApp: CaseIterable {

// Uber Eats
case eats

// Uber Driver
case driver

// Uber
case rides

public var deeplinkScheme: String {
switch self {
case .eats:
return "ubereats"
case .driver:
return "uberdriver"
case .rides:
return "uber"
}
}

public var urlIdentifier: String {
switch self {
case .eats:
return "eats"
case .driver:
return "drivers"
case .rides:
return "riders"
}
}
}
13 changes: 11 additions & 2 deletions Sources/UberRides/RequestDeeplink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import CoreLocation
import UIKit
import UberAuth
import UberCore

/**
Expand All @@ -35,6 +36,7 @@ public class RequestDeeplink: BaseDeeplink {

private let rideParameters: RideParameters
private let fallbackType: DeeplinkFallbackType
private let configurationProvider: ConfigurationProviding

/**
Initialize a ride request deeplink. If the Uber app is not installed, fallback to the App Store.
Expand All @@ -46,10 +48,13 @@ public class RequestDeeplink: BaseDeeplink {
/**
Initialize a ride request deeplink.
*/
public init(rideParameters: RideParameters, fallbackType: DeeplinkFallbackType) {
public init(rideParameters: RideParameters,
fallbackType: DeeplinkFallbackType,
configurationProvider: ConfigurationProviding = ConfigurationProvider()) {
self.rideParameters = rideParameters
self.fallbackType = fallbackType
self.rideParameters.source = rideParameters.source ?? RequestDeeplink.sourceString
self.configurationProvider = configurationProvider

let queryItems = RequestURLUtil.buildRequestQueryParameters(rideParameters)
let scheme = "uber"
Expand All @@ -75,7 +80,11 @@ public class RequestDeeplink: BaseDeeplink {
return urlComponents.url
}
case .appStore:
let deeplink = RidesAppStoreDeeplink(userAgent: rideParameters.userAgent)
let deeplink = RidesAppStoreDeeplink(
userAgent: rideParameters.userAgent,
clientID: configurationProvider.clientID ?? "",
sdkVersion: configurationProvider.sdkVersion
)
return deeplink.url
default:
break
Expand Down
Loading

0 comments on commit b39135a

Please sign in to comment.