-
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.
Merge pull request #294 from mohssenfathi/par
Implements PAR request to prefill user information
- Loading branch information
Showing
11 changed files
with
348 additions
and
51 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
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,60 @@ | ||
// | ||
// Copyright © Uber Technologies, Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
protocol NetworkProviding { | ||
func execute<R: Request>(request: R, completion: @escaping (Result<R.Response, UberAuthError>) -> ()) | ||
} | ||
|
||
final class NetworkProvider: NetworkProviding { | ||
|
||
private let baseUrl: String | ||
private let session: URLSession | ||
private let decoder = JSONDecoder() | ||
|
||
init(baseUrl: String) { | ||
self.baseUrl = baseUrl | ||
self.session = URLSession(configuration: .default) | ||
} | ||
|
||
func execute<R: Request>(request: R, completion: @escaping (Result<R.Response, UberAuthError>) -> ()) { | ||
guard let urlRequest = request.urlRequest(baseUrl: baseUrl) else { | ||
completion(.failure(UberAuthError.invalidRequest(""))) | ||
return | ||
} | ||
|
||
let dataTask = session | ||
.dataTask( | ||
with: urlRequest, | ||
completionHandler: { data, response, error in | ||
if let error { | ||
completion(.failure(.other(error))) | ||
return | ||
} | ||
|
||
guard let data, | ||
let response = response as? HTTPURLResponse else { | ||
completion(.failure(UberAuthError.oAuth(.unsupportedResponseType))) | ||
return | ||
} | ||
|
||
if let error = UberAuthError(response) { | ||
completion(.failure(error)) | ||
return | ||
} | ||
|
||
do { | ||
let decodedResponse = try self.decoder.decode(R.Response.self, from: data) | ||
completion(.success(decodedResponse)) | ||
} catch { | ||
completion(.failure(UberAuthError.serviceError)) | ||
} | ||
} | ||
) | ||
|
||
dataTask.resume() | ||
} | ||
} |
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,65 @@ | ||
// | ||
// Copyright © Uber Technologies, Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
struct ParRequest: Request { | ||
|
||
// MARK: Private Properties | ||
|
||
private let clientID: String | ||
|
||
private let prefill: [String: String] | ||
|
||
// MARK: Initializers | ||
|
||
init(clientID: String, | ||
prefill: [String: String]) { | ||
self.clientID = clientID | ||
self.prefill = prefill | ||
} | ||
|
||
// MARK: Request | ||
|
||
typealias Response = Par | ||
|
||
var body: [String: String]? { | ||
[ | ||
"client_id": clientID, | ||
"response_type": "code", | ||
"login_hint": loginHint | ||
] | ||
} | ||
|
||
var method: HTTPMethod = .post | ||
|
||
let path: String = "/oauth/v2/par" | ||
|
||
let contentType: String = "application/x-www-form-urlencoded" | ||
|
||
// MARK: Private | ||
|
||
private var loginHint: String { | ||
base64EncodedString(from: prefill) ?? "" | ||
} | ||
|
||
private func base64EncodedString(from dict: [String: String]) -> String? { | ||
(try? JSONSerialization.data(withJSONObject: dict))?.base64EncodedString() | ||
} | ||
} | ||
|
||
struct Par: Codable { | ||
|
||
/// An identifier used for profile sharing | ||
let requestURI: String? | ||
|
||
/// Lifetime of the request_uri | ||
let expiresIn: Date | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case requestURI = "request_uri" | ||
case expiresIn = "expires_in" | ||
} | ||
} |
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
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
Oops, something went wrong.