@@ -25,20 +25,31 @@ class SharingAuthorizationWebViewController: WPWebViewController {
25
25
}
26
26
27
27
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
+ }
42
53
43
54
/// Verification loading -- dismiss on completion
44
55
///
@@ -145,43 +156,51 @@ class SharingAuthorizationWebViewController: WPWebViewController {
145
156
// MARK: - URL Interpretation
146
157
147
158
private func authorizeAction( from url: URL ) -> AuthorizeAction {
148
- let requested = url. absoluteString
149
-
150
159
// 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 ) {
152
161
return . deny
153
162
}
154
163
155
- if !requested . hasPrefix ( SharingAuthorizationWebViewController . authorizationPrefix) {
164
+ if !url . absoluteString . hasPrefix ( AuthorizeURLComponents . authorizationPrefix. rawValue ) {
156
165
return . none
157
166
}
158
167
159
- if requested . range ( of : SharingAuthorizationWebViewController . requestActionParameter) != nil {
168
+ if AuthorizeURLComponents . requestActionParameter. containedIn ( url ) {
160
169
return . request
161
170
}
162
171
163
172
// Check the rest of the various decline ranges
164
- if requested . range ( of : SharingAuthorizationWebViewController . denyActionParameter) != nil {
173
+ if AuthorizeURLComponents . denyActionParameter. containedIn ( url ) {
165
174
return . deny
166
175
}
167
176
168
177
// LinkedIn
169
- if requested . range ( of : SharingAuthorizationWebViewController . userRefused) != nil {
178
+ if AuthorizeURLComponents . userRefused. containedIn ( url ) {
170
179
return . deny
171
180
}
172
181
173
182
// Facebook and Google+
174
- if requested . range ( of : SharingAuthorizationWebViewController . accessDenied) != nil {
183
+ if AuthorizeURLComponents . accessDenied. containedIn ( url ) {
175
184
return . deny
176
185
}
177
186
178
187
// If we've made it this far and verifyRange is found then we're *probably*
179
188
// verifying the oauth request. There are edge cases ( :cough: tumblr :cough: )
180
189
// 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) {
182
196
return . verify
183
197
}
184
198
199
+ // Facebook failure
200
+ if AuthorizeURLComponents . state. containedIn ( url) && AuthorizeURLComponents . error. containedIn ( url) {
201
+ return . unknown
202
+ }
203
+
185
204
return . unknown
186
205
}
187
206
}
0 commit comments