-
Notifications
You must be signed in to change notification settings - Fork 121
REST API: Update AlamofireNetwork to support authentication with application passwords
#8402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
7895ade
Inject siteID from `DefaultStoresManager` to `AlamofireNetwork`
itsmeichigo 0099895
Add new request type for REST API requests
itsmeichigo 3efefba
Configure application password use case in AlamofireNetwork
itsmeichigo f4e1519
Add new method to update headers for REST requests
itsmeichigo 823607a
Update AlamofireNetwork to handle application password
itsmeichigo 583d756
Use Task only for fetching application password
itsmeichigo 763ba7f
Add a fallback Jetpack request to REST request to trigger when applic…
itsmeichigo 168a6ac
Fix line limit violation
itsmeichigo 9b0d019
Update comment for RESTRequest initializer
itsmeichigo 17cae6a
Inject the complete use case to AlamofireNetwork from AuthenticatedState
itsmeichigo 5e07911
Update comments for AlamofireNetwork
itsmeichigo 85c31f8
Revert "Inject the complete use case to AlamofireNetwork from Authent…
itsmeichigo 9740cbb
Separate authentication logic to a new class
itsmeichigo cc87a50
Make ApplicationPasswordUseCase injectable
itsmeichigo ad69556
Add unit tests for RequestAuthenticator
itsmeichigo 4b5423e
Update comments for RESTRequest
itsmeichigo 3ede699
Fix incorrect username and password when authenticating rest request
itsmeichigo d1ea84c
Reformat code for RequestAuthenticator
itsmeichigo 139348b
Remove fallback request and header from
itsmeichigo b7942a5
Update JetpackRequest to be able to converted to a REST request
itsmeichigo 63a76f6
Update RequestAuthenticator to return error if application password c…
itsmeichigo 012d6c8
Revert changes to AuthenticatedState and DefaultStoresManager
itsmeichigo f779ff2
Fix line length violation
itsmeichigo c87f274
Keep `network` local in AuthenticatedState intializer
itsmeichigo 1ef0e5b
Merge branch 'trunk' into feat/8389-network-update-for-rest-api
itsmeichigo fdf9566
Update unit tests for RequestAuthenticator
itsmeichigo 498cfe8
Merge branch 'trunk' into feat/8389-network-update-for-rest-api
itsmeichigo ab3a53b
Update RequestAuthenticator to set up application password usecase in…
itsmeichigo 55d4fc5
Merge branch 'trunk' into feat/8389-network-update-for-rest-api
itsmeichigo f3fa7aa
Create application password use case in RequestAuthenticator
itsmeichigo aa6d8e9
Return completion when self is not found
itsmeichigo 67b9316
Simplify condition checks for enum case
itsmeichigo e66619a
Rename createRESTRequest to asRESTRequest
itsmeichigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
| import Alamofire | ||
| import Foundation | ||
|
|
||
| /// Helper class to update requests with authorization header if possible. | ||
| /// | ||
| final class RequestAuthenticator { | ||
| /// WordPress.com Credentials. | ||
| /// | ||
| private let credentials: Credentials? | ||
|
|
||
| /// The use case to handle authentication with application passwords. | ||
| /// | ||
| private let applicationPasswordUseCase: ApplicationPasswordUseCase? | ||
|
|
||
| /// Sets up the authenticator with optional credentials and application password use case. | ||
| /// `applicationPasswordUseCase` can be injected for unit tests. | ||
| /// | ||
| init(credentials: Credentials?, applicationPasswordUseCase: ApplicationPasswordUseCase? = nil) { | ||
| self.credentials = credentials | ||
| let useCase: ApplicationPasswordUseCase? = { | ||
| if let applicationPasswordUseCase { | ||
| return applicationPasswordUseCase | ||
| } else if case let .wporg(username, password, siteAddress) = credentials { | ||
| return try? DefaultApplicationPasswordUseCase(username: username, | ||
| password: password, | ||
| siteAddress: siteAddress) | ||
| } else { | ||
| return nil | ||
| } | ||
| }() | ||
| self.applicationPasswordUseCase = useCase | ||
| } | ||
|
|
||
| /// Updates a request with application password or WPCOM token if possible. | ||
| /// | ||
| func authenticateRequest(_ request: URLRequestConvertible, completion: @escaping (Swift.Result<URLRequestConvertible, Error>) -> Void) { | ||
| guard let jetpackRequest = request as? JetpackRequest, | ||
| let useCase = applicationPasswordUseCase, | ||
| case let .wporg(_, _, siteAddress) = credentials, | ||
| let restRequest = jetpackRequest.asRESTRequest(with: siteAddress) else { | ||
| // Handle non-REST requests as before | ||
| return completion(.success(authenticateUsingWPCOMTokenIfPossible(request))) | ||
| } | ||
|
|
||
| Task(priority: .medium) { | ||
| let result: Swift.Result<URLRequestConvertible, Error> | ||
| do { | ||
| let authenticatedRequest = try await authenticateUsingApplicationPassword(restRequest, useCase: useCase) | ||
| result = .success(authenticatedRequest) | ||
| } catch { | ||
| result = .failure(error) | ||
| } | ||
| await MainActor.run { | ||
| completion(result) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Attempts authenticating a request with application password. | ||
| /// | ||
| private func authenticateUsingApplicationPassword(_ restRequest: RESTRequest, useCase: ApplicationPasswordUseCase) async throws -> URLRequestConvertible { | ||
| let applicationPassword: ApplicationPassword = try await { | ||
| if let password = useCase.applicationPassword { | ||
| return password | ||
| } | ||
| return try await useCase.generateNewPassword() | ||
| }() | ||
| return try await MainActor.run { | ||
| return try restRequest.authenticateRequest(with: applicationPassword) | ||
| } | ||
| } | ||
|
|
||
| /// Attempts creating a request with WPCOM token if possible. | ||
| /// | ||
| private func authenticateUsingWPCOMTokenIfPossible(_ request: URLRequestConvertible) -> URLRequestConvertible { | ||
| credentials.map { AuthenticatedRequest(credentials: $0, request: request) } ?? | ||
| UnauthenticatedRequest(request: request) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever work extracting this into a separate class. 👏