Skip to content

Commit 1ee174f

Browse files
authored
Merge pull request #17803 from wordpress-mobile/issue/17794-facebook-publicize-auth
Publicize: Fix Facebook auth success detection
2 parents 83768bb + 1cfe665 commit 1ee174f

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* [*] Block editor: Highlight text: fix applying formatting for non-selected text [https://github.com/wordpress-mobile/gutenberg-mobile/pull/4471]
1818
* [**] Block editor: Fix Android handling of Hebrew and Indonesian translations [https://github.com/wordpress-mobile/gutenberg-mobile/pull/4397]
1919
* [***] Self-hosted sites: Fixed a crash when saving media and no Internet connection was available. [#17759]
20+
* [*] Publicize: Fixed an issue where a successful login was not automatically detected when connecting a Facebook account to Publicize. [#17803]
2021

2122
19.0
2223
-----

WordPress/Classes/ViewRelated/Blog/SharingAuthorizationWebViewController.swift

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,31 @@ class SharingAuthorizationWebViewController: WPWebViewController {
2525
}
2626

2727
private static let loginURL = "https://wordpress.com/wp-login.php"
28-
private static let authorizationPrefix = "https://public-api.wordpress.com/connect/"
29-
private static let requestActionParameter = "action=request"
30-
private static let verifyActionParameter = "action=verify"
31-
private static let denyActionParameter = "action=deny"
32-
33-
// Special handling for the inconsistent way that services respond to a user's choice to decline
34-
// oauth authorization.
35-
// Right now we have no clear way to know if Tumblr fails. This is something we should try
36-
// fixing moving forward.
37-
// Path does not set the action param or call the callback. It forwards to its own URL ending in /decline.
38-
private static let declinePath = "/decline"
39-
private static let userRefused = "oauth_problem=user_refused"
40-
private static let authorizationDenied = "denied="
41-
private static let accessDenied = "error=access_denied"
28+
29+
private enum AuthorizeURLComponents: String {
30+
case verifyActionParameter = "action=verify"
31+
case denyActionParameter = "action=deny"
32+
case requestActionParameter = "action=request"
33+
34+
case declinePath = "/decline"
35+
case authorizationPrefix = "https://public-api.wordpress.com/connect/"
36+
case accessDenied = "error=access_denied"
37+
38+
case state = "state"
39+
case code = "code"
40+
case error = "error"
41+
42+
// Special handling for the inconsistent way that services respond to a user's choice to decline
43+
// oauth authorization.
44+
// Right now we have no clear way to know if Tumblr fails. This is something we should try
45+
// fixing moving forward.
46+
// Path does not set the action param or call the callback. It forwards to its own URL ending in /decline.
47+
case userRefused = "oauth_problem=user_refused"
48+
49+
func containedIn(_ url: URL) -> Bool {
50+
url.absoluteString.contains(rawValue)
51+
}
52+
}
4253

4354
/// Verification loading -- dismiss on completion
4455
///
@@ -145,43 +156,51 @@ class SharingAuthorizationWebViewController: WPWebViewController {
145156
// MARK: - URL Interpretation
146157

147158
private func authorizeAction(from url: URL) -> AuthorizeAction {
148-
let requested = url.absoluteString
149-
150159
// Path oauth declines are handled by a redirect to a path.com URL, so check this first.
151-
if requested.range(of: SharingAuthorizationWebViewController.declinePath) != nil {
160+
if AuthorizeURLComponents.declinePath.containedIn(url) {
152161
return .deny
153162
}
154163

155-
if !requested.hasPrefix(SharingAuthorizationWebViewController.authorizationPrefix) {
164+
if !url.absoluteString.hasPrefix(AuthorizeURLComponents.authorizationPrefix.rawValue) {
156165
return .none
157166
}
158167

159-
if requested.range(of: SharingAuthorizationWebViewController.requestActionParameter) != nil {
168+
if AuthorizeURLComponents.requestActionParameter.containedIn(url) {
160169
return .request
161170
}
162171

163172
// Check the rest of the various decline ranges
164-
if requested.range(of: SharingAuthorizationWebViewController.denyActionParameter) != nil {
173+
if AuthorizeURLComponents.denyActionParameter.containedIn(url) {
165174
return .deny
166175
}
167176

168177
// LinkedIn
169-
if requested.range(of: SharingAuthorizationWebViewController.userRefused) != nil {
178+
if AuthorizeURLComponents.userRefused.containedIn(url) {
170179
return .deny
171180
}
172181

173182
// Facebook and Google+
174-
if requested.range(of: SharingAuthorizationWebViewController.accessDenied) != nil {
183+
if AuthorizeURLComponents.accessDenied.containedIn(url) {
175184
return .deny
176185
}
177186

178187
// If we've made it this far and verifyRange is found then we're *probably*
179188
// verifying the oauth request. There are edge cases ( :cough: tumblr :cough: )
180189
// where verification is declined and we get a false positive.
181-
if requested.range(of: SharingAuthorizationWebViewController.verifyActionParameter) != nil {
190+
if AuthorizeURLComponents.verifyActionParameter.containedIn(url) {
191+
return .verify
192+
}
193+
194+
// Facebook
195+
if AuthorizeURLComponents.state.containedIn(url) && AuthorizeURLComponents.code.containedIn(url) {
182196
return .verify
183197
}
184198

199+
// Facebook failure
200+
if AuthorizeURLComponents.state.containedIn(url) && AuthorizeURLComponents.error.containedIn(url) {
201+
return .unknown
202+
}
203+
185204
return .unknown
186205
}
187206
}

0 commit comments

Comments
 (0)