-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
026087d
commit 03833c1
Showing
18 changed files
with
875 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
Sources/UberAuth/Authorize/AuthorizationCodeResponseParser.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Copyright © Uber Technologies, Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
/// @mockable | ||
public protocol AuthorizationCodeResponseParsing { | ||
func isValidResponse(url: URL, matching redirectURI: String) -> Bool | ||
func callAsFunction(url: URL) -> Result<Client, UberAuthError> | ||
} | ||
|
||
/// | ||
/// A struct that validates and extracts values from a url containing an authorization code response | ||
/// | ||
public struct AuthorizationCodeResponseParser: AuthorizationCodeResponseParsing { | ||
|
||
public init() {} | ||
|
||
/// Determines whether the provided url corresponds to an authorization code response | ||
/// by verifying that the url matches the expected redirect URI | ||
/// | ||
/// - Parameters: | ||
/// - url: The url to parse | ||
/// - redirectURI: The expected redirect url | ||
/// - Returns: A boolean indicating whether or not the URLs match | ||
public func isValidResponse(url: URL, matching redirectURI: String) -> Bool { | ||
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), | ||
let expectedComponents = URLComponents(string: redirectURI) else { | ||
return false | ||
} | ||
|
||
// Verify incoming scheme matches redirect_uri scheme | ||
guard let scheme = components.scheme?.lowercased(), | ||
let expectedScheme = expectedComponents.scheme?.lowercased(), | ||
scheme == expectedScheme else { | ||
return false | ||
} | ||
|
||
// Verify incoming host matches redirect_uri host | ||
guard let scheme = components.host?.lowercased(), | ||
let expectedScheme = expectedComponents.host?.lowercased(), | ||
scheme == expectedScheme else { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
/// Parses the provided url and attempts to pull an authorization code or an error | ||
/// from the query parameters | ||
/// | ||
/// - Parameter url: The url to parse | ||
/// - Returns: A Result containing a client object built from the parsed values | ||
public func callAsFunction(url: URL) -> Result<Client, UberAuthError> { | ||
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { | ||
return .failure(.invalidResponse) | ||
} | ||
|
||
if let authorizationCode = components.queryItems?.first(where: { | ||
$0.name == "code" | ||
})?.value { | ||
return .success( | ||
Client(authorizationCode: authorizationCode) | ||
) | ||
} | ||
|
||
let error: UberAuthError | ||
if let errorString = components.queryItems?.first(where: { $0.name == "error" })?.value, | ||
let oAuthError = OAuthError(rawValue: errorString) { | ||
error = .oAuth(oAuthError) | ||
} else { | ||
error = .invalidAuthCode | ||
} | ||
|
||
return .failure(error) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Copyright © Uber Technologies, Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// @mockable | ||
public protocol ApplicationLaunching { | ||
|
||
func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey: Any], completionHandler: ((Bool) -> Void)?) | ||
} | ||
|
||
extension UIApplication: ApplicationLaunching {} |
Oops, something went wrong.