-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Replace native WP.com sign in with web-based sign in #23675
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
crazytonyli
merged 9 commits into
trunk
from
task/wpcom-web-based-authentication-rollout
Nov 3, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2212307
Consolidate tracking of web-based WP.com login
crazytonyli 89816af
Add a clousre to overwrite handling of the WP.com login button
crazytonyli 3660f1c
Replace native WP.com sign in with web-based sign in
crazytonyli 319f7e3
Add a release note
crazytonyli 43409de
Extract error alert into a function
crazytonyli 37d8246
Handle a scenario where users deny access on web login
crazytonyli 7e582a0
Disable web log for UI tests
crazytonyli a218717
Merge branch 'trunk' into task/wpcom-web-based-authentication-rollout
crazytonyli 7389522
Merge branch 'trunk' into task/wpcom-web-based-authentication-rollout
crazytonyli 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| 25.5 | ||
| ----- | ||
|
|
||
| * [***] Use web-based sign-in flow (hidden under a feature flag) [#23675] | ||
|
|
||
| 25.4 | ||
| ----- | ||
|
|
||
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import AuthenticationServices | ||
| import Foundation | ||
| import UIKit | ||
| import WordPressAuthenticator | ||
|
|
||
| import Alamofire | ||
|
|
||
|
|
@@ -10,14 +11,84 @@ import Alamofire | |
| struct WordPressDotComAuthenticator { | ||
| enum Error: Swift.Error { | ||
| case invalidCallbackURL | ||
| case loginDenied(message: String) | ||
| case obtainAccessToken | ||
| case urlError(URLError) | ||
| case parsing(DecodingError) | ||
| case cancelled | ||
| case unknown(Swift.Error) | ||
| } | ||
|
|
||
| @MainActor | ||
| func signIn(from viewController: UINavigationController) async { | ||
| let token: String | ||
| do { | ||
| token = try await authenticate(from: viewController) | ||
| } catch { | ||
| if let error = error as? WordPressDotComAuthenticator.Error { | ||
| presentSignInError(error, from: viewController) | ||
| } else { | ||
| wpAssertionFailure("WP.com web login failed", userInfo: ["error": "\(error)"]) | ||
| } | ||
| return | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) Extract the first part into a separate method so that there is no
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 43409de |
||
| } | ||
|
|
||
| let delegate = WordPressAuthenticator.shared.delegate! | ||
| let credentials = AuthenticatorCredentials(wpcom: WordPressComCredentials(authToken: token, isJetpackLogin: false, multifactor: false)) | ||
| SVProgressHUD.show() | ||
| delegate.sync(credentials: credentials) { | ||
| SVProgressHUD.dismiss() | ||
|
|
||
| delegate.presentLoginEpilogue( | ||
| in: viewController, | ||
| for: credentials, | ||
| source: .custom(source: "web-login"), | ||
| onDismiss: { /* Do nothing */ } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private func presentSignInError(_ error: WordPressDotComAuthenticator.Error, from viewController: UIViewController) { | ||
| // Show an alert for non-cancellation errors. | ||
| let alertMessage: String | ||
| switch error { | ||
| case .cancelled: | ||
| // `.cancelled` error is thrown when user taps the cancel button in the presented Safari view controller. | ||
| // No need to show an alert for this error. | ||
| return | ||
| case let .loginDenied(message): | ||
| alertMessage = message | ||
| case let .urlError(error): | ||
| alertMessage = error.localizedDescription | ||
| case .invalidCallbackURL, .obtainAccessToken, .parsing, .unknown: | ||
| // These errors are unexpected. | ||
| wpAssertionFailure("WP.com web login failed", userInfo: ["error": "\(error)"]) | ||
| alertMessage = SharedStrings.Error.generic | ||
| } | ||
|
|
||
| let alert = UIAlertController( | ||
| title: NSLocalizedString("generic.error.title", value: "Error", comment: "A generic title for an error"), | ||
| message: alertMessage, | ||
| preferredStyle: .alert | ||
| ) | ||
| alert.addAction(UIAlertAction(title: SharedStrings.Button.close, style: .cancel, handler: nil)) | ||
| viewController.present(alert, animated: true) | ||
| } | ||
|
|
||
| func authenticate(from viewController: UIViewController) async throws -> String { | ||
| WPAnalytics.track(.wpcomWebSignIn, properties: ["stage": "start"]) | ||
|
|
||
| do { | ||
| let value = try await _authenticate(from: viewController) | ||
| WPAnalytics.track(.wpcomWebSignIn, properties: ["stage": "success"]) | ||
| return value | ||
| } catch { | ||
| WPAnalytics.track(.wpcomWebSignIn, properties: ["stage": "error", "error": "\(error)"]) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| private func _authenticate(from viewController: UIViewController) async throws -> String { | ||
| let clientId = ApiCredentials.client | ||
| let clientSecret = ApiCredentials.secret | ||
| let redirectURI = "x-wordpress-app://oauth2-callback" | ||
|
|
@@ -55,8 +126,17 @@ struct WordPressDotComAuthenticator { | |
| clientSecret: String, | ||
| redirectURI: String | ||
| ) async throws -> String { | ||
| guard let query = URLComponents(url: url, resolvingAgainstBaseURL: true)?.queryItems, | ||
| let code = query.first(where: { $0.name == "code" })?.value else { | ||
| guard let query = URLComponents(url: url, resolvingAgainstBaseURL: true)?.queryItems else { | ||
| throw Error.invalidCallbackURL | ||
| } | ||
|
|
||
| let queryMap: [String: String] = query.reduce(into: [:]) { $0[$1.name] = $1.value } | ||
|
|
||
| guard let code = queryMap["code"] else { | ||
| if queryMap["error"] == "access_denied" { | ||
| let message = NSLocalizedString("wpComLogin.error.accessDenied", value: "Access denied. You need to approve to log in to WordPress.com", comment: "Error message when user denies access to WordPress.com") | ||
| throw Error.loginDenied(message: message) | ||
| } | ||
| throw Error.invalidCallbackURL | ||
| } | ||
|
|
||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The animation at the top is distracting.
Untitled.mp4
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.
I'm not sure why it looks like this for me. It's different on your recording. I think there is a bug with the first welcome page.
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.
Can we present this login screen as a pushed navigation item instead of a popover?
That'd be more consistent with the current setup
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.
🤔 I don't think there is an API for that. https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession
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.
Hmm I thought we had the ability to do it in the SwiftUI setup for self-hosted sites, but it's ok – we can dig into that further once we're not relying on a bunch of WPAuthenticator stuff anymore.
I have a few suggestions about this in #23702
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.
I just checked. The application password flow for self-hosted site also presents Safari view as a modal.