Skip to content

Commit

Permalink
Merge pull request #30 from auth0/chore-improve-pkce-disabled-error
Browse files Browse the repository at this point in the history
Improve PKCE not enabled error
  • Loading branch information
hzalaz authored Jun 21, 2016
2 parents 605bf37 + af36fc5 commit f23d943
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Auth0/WebAuth/OAuth2Grant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,17 @@ struct PKCE: OAuth2Grant {
let string = String(data: data, encoding: NSUTF8StringEncoding)
return callback(.Failure(error: AuthenticationError(string: string)))
}
let clientId = self.clientId
Authentication(clientId: clientId, url: url)
.tokenExchange(withCode: code, codeVerifier: verifier, redirectURI: redirectURL.absoluteString)
.start(callback)
.start { result in
// FIXME: Special case for PKCE when the correct method for token endpoint authentication is not set (it should be None)
if case .Failure(let cause as AuthenticationError) = result where cause.description == "Unauthorized" {
let error = WebAuthError.PKCENotAllowed("Please go to 'https://manage.auth0.com/#/applications/\(clientId)/settings' and set 'Token Endpoint Authentication Method' to 'None' to enable PKCE.")
callback(Result.Failure(error: error))
} else {
callback(result)
}
}
}
}
12 changes: 12 additions & 0 deletions Auth0/WebAuth/WebAuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import Foundation
- NoBundleIdentifierFound: Cannot get the App's Bundle Identifier to use for redirect_uri.
- CannotDismissWebAuthController: When trying to dismiss WebAuth controller, no presenter controller could be found.
- UserCancelled: User cancelled the web-based authentication, e.g. tapped the "Done" button in SFSafariViewController
- PKCENotAllowed: PKCE for the supplied Auth0 ClientId was not allowed. You need to set the `Token Endpoint Authentication Method` to `None` in your Auth0 Dashboard
*/
public enum WebAuthError: ErrorType {
case NoBundleIdentifierFound
case CannotDismissWebAuthController
case UserCancelled
case PKCENotAllowed(String)
}

extension WebAuthError: FoundationErrorConvertible {
Expand All @@ -52,6 +54,16 @@ extension WebAuthError: FoundationErrorConvertible {
]
)
}
if case .PKCENotAllowed(let message) = self {
return NSError(
domain: WebAuthError.FoundationDomain,
code: WebAuthError.GenericFoundationCode,
userInfo: [
NSLocalizedDescriptionKey: message,
WebAuthError.FoundationUserInfoKey: self as NSError
]
)
}
return NSError(
domain: WebAuthError.FoundationDomain,
code: WebAuthError.GenericFoundationCode,
Expand Down

0 comments on commit f23d943

Please sign in to comment.