Skip to content
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

Rework errors handling #26

Merged
merged 7 commits into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Auth0

class ViewController: UIViewController {

var onAuth: (Result<Credentials, Authentication.Error> -> ())!
var onAuth: (Result<Credentials> -> ())!

@IBOutlet weak var oauth2: UIButton!

Expand Down
28 changes: 28 additions & 0 deletions Auth0.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
5FCCC31A1CF51BBD00901E2E /* NSError+Management.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCCC3181CF51BBD00901E2E /* NSError+Management.swift */; };
5FCCC31C1CF51DF300901E2E /* _ObjectiveManagementAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCCC31B1CF51DF300901E2E /* _ObjectiveManagementAPI.swift */; };
5FCCC31D1CF51DF300901E2E /* _ObjectiveManagementAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCCC31B1CF51DF300901E2E /* _ObjectiveManagementAPI.swift */; };
5FD255AE1D14A4A300387ECB /* AuthenticationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255AD1D14A4A300387ECB /* AuthenticationError.swift */; };
5FD255AF1D14A4A300387ECB /* AuthenticationError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255AD1D14A4A300387ECB /* AuthenticationError.swift */; };
5FD255B11D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B01D14A9E000387ECB /* AuthenticationErrorSpec.swift */; };
5FD255B21D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B01D14A9E000387ECB /* AuthenticationErrorSpec.swift */; };
5FD255B41D14DD2600387ECB /* ManagementError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B31D14DD2600387ECB /* ManagementError.swift */; };
5FD255B51D14DD2600387ECB /* ManagementError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B31D14DD2600387ECB /* ManagementError.swift */; };
5FD255B71D14F00900387ECB /* Auth0Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B61D14F00900387ECB /* Auth0Error.swift */; };
5FD255B81D14F00900387ECB /* Auth0Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B61D14F00900387ECB /* Auth0Error.swift */; };
5FD255BA1D14F70B00387ECB /* WebAuthError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B91D14F70B00387ECB /* WebAuthError.swift */; };
5FE2F8A61CCA9C17003628F4 /* CredentialsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */; };
5FE2F8A71CCA9C17003628F4 /* CredentialsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */; };
5FE2F8A91CCE54F1003628F4 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8A81CCE54F1003628F4 /* Result.swift */; };
Expand Down Expand Up @@ -231,6 +240,11 @@
5FCCC3141CF4DB4400901E2E /* NSError+Authentication.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSError+Authentication.swift"; sourceTree = "<group>"; };
5FCCC3181CF51BBD00901E2E /* NSError+Management.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSError+Management.swift"; sourceTree = "<group>"; };
5FCCC31B1CF51DF300901E2E /* _ObjectiveManagementAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _ObjectiveManagementAPI.swift; sourceTree = "<group>"; };
5FD255AD1D14A4A300387ECB /* AuthenticationError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationError.swift; sourceTree = "<group>"; };
5FD255B01D14A9E000387ECB /* AuthenticationErrorSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationErrorSpec.swift; sourceTree = "<group>"; };
5FD255B31D14DD2600387ECB /* ManagementError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManagementError.swift; sourceTree = "<group>"; };
5FD255B61D14F00900387ECB /* Auth0Error.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Auth0Error.swift; sourceTree = "<group>"; };
5FD255B91D14F70B00387ECB /* WebAuthError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebAuthError.swift; sourceTree = "<group>"; };
5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CredentialsSpec.swift; sourceTree = "<group>"; };
5FE2F8A81CCE54F1003628F4 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
5FE2F8B11CCEAED8003628F4 /* Requestable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Requestable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -328,6 +342,7 @@
5F049B6C1CB42C29006F6C05 /* Auth0.h */,
5F06DDC81CC66B710011842B /* Auth0.swift */,
5F049B6E1CB42C29006F6C05 /* Info.plist */,
5FD255B61D14F00900387ECB /* Auth0Error.swift */,
);
path = Auth0;
sourceTree = "<group>";
Expand Down Expand Up @@ -388,6 +403,7 @@
5F53F5CD1CFD157300476A46 /* OAuth2Session.swift */,
5FCAB1751D0900CF00331C84 /* SessionStorage.swift */,
5F3965C11CF67CF000CDE7C0 /* WebAuth.swift */,
5FD255B91D14F70B00387ECB /* WebAuthError.swift */,
);
path = WebAuth;
sourceTree = "<group>";
Expand Down Expand Up @@ -442,6 +458,7 @@
5FE2F8B41CCEB1C0003628F4 /* Handlers.swift */,
5F74CB3C1CEE8E9100226823 /* UserIdentity.swift */,
5FE2F8C21CD1498E003628F4 /* UserProfile.swift */,
5FD255AD1D14A4A300387ECB /* AuthenticationError.swift */,
);
path = Authentication;
sourceTree = "<group>";
Expand All @@ -453,6 +470,7 @@
5FE2F8A51CCA9C17003628F4 /* CredentialsSpec.swift */,
5F74CB421CEFDFB800226823 /* UserIdentitySpec.swift */,
5FE2F8C51CD1522F003628F4 /* UserProfileSpec.swift */,
5FD255B01D14A9E000387ECB /* AuthenticationErrorSpec.swift */,
);
path = Authentication;
sourceTree = "<group>";
Expand Down Expand Up @@ -550,6 +568,7 @@
5FF465BB1CE2AC4500F7ED8C /* Management.swift */,
5FADB60B1CED7E0800D4BB50 /* UserPatchAttributes.swift */,
5FADB6051CED27FB00D4BB50 /* Users.swift */,
5FD255B31D14DD2600387ECB /* ManagementError.swift */,
);
path = Management;
sourceTree = "<group>";
Expand Down Expand Up @@ -796,6 +815,9 @@
5FF465BC1CE2AC4500F7ED8C /* Management.swift in Sources */,
5F4A1F961D00AABC00C72242 /* OAuth2Grant.swift in Sources */,
5FCAB1731D09009600331C84 /* NSData+URLSafe.swift in Sources */,
5FD255BA1D14F70B00387ECB /* WebAuthError.swift in Sources */,
5FD255B71D14F00900387ECB /* Auth0Error.swift in Sources */,
5FD255AE1D14A4A300387ECB /* AuthenticationError.swift in Sources */,
5F2BD4041D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */,
5F06DDC91CC66B710011842B /* Auth0.swift in Sources */,
5FE2F8B21CCEAED8003628F4 /* Requestable.swift in Sources */,
Expand All @@ -805,6 +827,7 @@
5FCAB1791D09124D00331C84 /* NSURL+Auth0.swift in Sources */,
5F7989511CF13A20009E8A10 /* _ObjectiveAuthenticationAPI.swift in Sources */,
5F74CB401CEFD5E600226823 /* JSONObjectPayload.swift in Sources */,
5FD255B41D14DD2600387ECB /* ManagementError.swift in Sources */,
5FE2F8B81CD0E910003628F4 /* Request.swift in Sources */,
5FE2F8A91CCE54F1003628F4 /* Result.swift in Sources */,
5F3965C21CF67CF000CDE7C0 /* WebAuth.swift in Sources */,
Expand All @@ -827,13 +850,16 @@
5F6FAC641D09E98000D5B4EA /* Logger.swift in Sources */,
5FF465BD1CE2AC4500F7ED8C /* Management.swift in Sources */,
5F06DDCA1CC66B710011842B /* Auth0.swift in Sources */,
5FD255B81D14F00900387ECB /* Auth0Error.swift in Sources */,
5FD255B51D14DD2600387ECB /* ManagementError.swift in Sources */,
5FE2F8B31CCEAED8003628F4 /* Requestable.swift in Sources */,
5FCCC31A1CF51BBD00901E2E /* NSError+Management.swift in Sources */,
5FE2F8B61CCEB1C0003628F4 /* Handlers.swift in Sources */,
5FADB60D1CED7E0800D4BB50 /* UserPatchAttributes.swift in Sources */,
5F7989521CF13A20009E8A10 /* _ObjectiveAuthenticationAPI.swift in Sources */,
5F74CB411CEFD5E600226823 /* JSONObjectPayload.swift in Sources */,
5F2BD4051D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */,
5FD255AF1D14A4A300387ECB /* AuthenticationError.swift in Sources */,
5FE2F8B91CD0E910003628F4 /* Request.swift in Sources */,
5FE2F8AA1CCE54F1003628F4 /* Result.swift in Sources */,
5FE2F8C41CD1498E003628F4 /* UserProfile.swift in Sources */,
Expand Down Expand Up @@ -861,6 +887,7 @@
5FCAB16D1D07AC3500331C84 /* WebAuthSpec.swift in Sources */,
5FBBF0431CCA90300024D2AF /* AuthenticationSpec.swift in Sources */,
5FCAB16B1D07AC3500331C84 /* OAuth2GrantSpec.swift in Sources */,
5FD255B11D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */,
5FCAB16C1D07AC3500331C84 /* OAuth2SessionSpec.swift in Sources */,
5FE2F8C61CD1522F003628F4 /* UserProfileSpec.swift in Sources */,
5FE2F8BE1CD0EC52003628F4 /* ResponseSpec.swift in Sources */,
Expand All @@ -879,6 +906,7 @@
5FBBF0391CC964BC0024D2AF /* Matchers.swift in Sources */,
5F93BC0C1CC6B0DE0031519F /* Auth0Spec.swift in Sources */,
5FE2F8A71CCA9C17003628F4 /* CredentialsSpec.swift in Sources */,
5FD255B21D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */,
5FADB60A1CED500900D4BB50 /* ManagementSpec.swift in Sources */,
5FBBF0441CCA90300024D2AF /* AuthenticationSpec.swift in Sources */,
5FE2F8C71CD1522F003628F4 /* UserProfileSpec.swift in Sources */,
Expand Down
48 changes: 48 additions & 0 deletions Auth0/Auth0Error.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Auth0Error.swift
//
// Copyright (c) 2016 Auth0 (http://auth0.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

let UnknownError = "a0.sdk.internal_error.unknown"
let NonJSONError = "a0.sdk.internal_error.plain"
let EmptyBodyError = "a0.sdk.internal_error.empty"

/**
Generic representation of Auth0 API errors
- note: It's recommended to use either `AuthenticationError` or `ManagementError` for better error handling
*/
public protocol Auth0Error: ErrorType {

init(string: String?, statusCode: Int)
init(info: [String: AnyObject])

/// The code of the error as a String
var code: String { get }

}

internal protocol FoundationErrorConvertible {
static var FoundationDomain: String { get }
static var FoundationUserInfoKey: String { get }

func newFoundationError() -> NSError
}
27 changes: 14 additions & 13 deletions Auth0/Authentication/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct Authentication {
- returns: authentication request that will yield Auth0 User Credentials
- seeAlso: Credentials
*/
public func login(username: String, password: String, connection: String, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> Request<Credentials, Error> {
public func login(username: String, password: String, connection: String, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> Request<Credentials, AuthenticationError> {
let resourceOwner = NSURL(string: "/oauth/ro", relativeToURL: self.url)!
var payload: [String: AnyObject] = [
"username": username,
Expand Down Expand Up @@ -154,7 +154,7 @@ public struct Authentication {

- returns: request that will yield a created database user (just email, username and email verified flag)
*/
public func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]? = nil) -> Request<DatabaseUser, Error> {
public func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]? = nil) -> Request<DatabaseUser, AuthenticationError> {
var payload: [String: AnyObject] = [
"email": email,
"password": password,
Expand Down Expand Up @@ -188,7 +188,7 @@ public struct Authentication {

- returns: request to reset password
*/
public func resetPassword(email: String, connection: String) -> Request<Void, Error> {
public func resetPassword(email: String, connection: String) -> Request<Void, AuthenticationError> {
let payload = [
"email": email,
"connection": connection,
Expand Down Expand Up @@ -245,9 +245,10 @@ public struct Authentication {

- returns: an authentication request that will yield Auth0 user credentials after creating the user.
*/
public func signUp(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]? = nil, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> ConcatRequest<DatabaseUser, Credentials, Error> {
return createUser(email, username: username, password: password, connection: connection, userMetadata: userMetadata)
.concat(login(email, password: password, connection: connection, scope: scope, parameters: parameters))
public func signUp(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]? = nil, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> ConcatRequest<DatabaseUser, Credentials, AuthenticationError> {
let first = createUser(email, username: username, password: password, connection: connection, userMetadata: userMetadata)
let second = login(email, password: password, connection: connection, scope: scope, parameters: parameters)
return ConcatRequest(first: first, second: second)
}

/**
Expand Down Expand Up @@ -276,7 +277,7 @@ public struct Authentication {

- returns: a request
*/
public func startPasswordless(email email: String, type: PasswordlessType = .Code, connection: String = "email", parameters: [String: AnyObject] = [:]) -> Request<Void, Authentication.Error> {
public func startPasswordless(email email: String, type: PasswordlessType = .Code, connection: String = "email", parameters: [String: AnyObject] = [:]) -> Request<Void, AuthenticationError> {
var payload: [String: AnyObject] = [
"email": email,
"connection": connection,
Expand Down Expand Up @@ -316,7 +317,7 @@ public struct Authentication {

- returns: a request
*/
public func startPasswordless(phoneNumber phoneNumber: String, type: PasswordlessType = .Code, connection: String = "sms") -> Request<Void, Authentication.Error> {
public func startPasswordless(phoneNumber phoneNumber: String, type: PasswordlessType = .Code, connection: String = "sms") -> Request<Void, AuthenticationError> {
let payload: [String: AnyObject] = [
"phone_number": phoneNumber,
"connection": connection,
Expand All @@ -341,7 +342,7 @@ public struct Authentication {

- returns: a request that will yield token information
*/
public func tokenInfo(token: String) -> Request<UserProfile, Authentication.Error> {
public func tokenInfo(token: String) -> Request<UserProfile, AuthenticationError> {
let payload: [String: AnyObject] = ["id_token": token]
let tokenInfo = NSURL(string: "/tokeninfo", relativeToURL: self.url)!
return Request(session: session, url: tokenInfo, method: "POST", handle: authenticationObject, payload: payload)
Expand All @@ -361,7 +362,7 @@ public struct Authentication {

- returns: a request that will yield user information
*/
public func userInfo(token: String) -> Request<UserProfile, Authentication.Error> {
public func userInfo(token: String) -> Request<UserProfile, AuthenticationError> {
let userInfo = NSURL(string: "/userinfo", relativeToURL: self.url)!
return Request(session: session, url: userInfo, method: "GET", handle: authenticationObject, headers: ["Authorization": "Bearer \(token)"])
}
Expand Down Expand Up @@ -392,7 +393,7 @@ public struct Authentication {

- returns: a request that will yield Auth0 user's credentials
*/
public func loginSocial(token: String, connection: String, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> Request<Credentials, Error> {
public func loginSocial(token: String, connection: String, scope: String = "openid", parameters: [String: AnyObject] = [:]) -> Request<Credentials, AuthenticationError> {
var payload: [String: AnyObject] = [
"access_token": token,
"connection": connection,
Expand Down Expand Up @@ -420,7 +421,7 @@ public struct Authentication {
- returns: a request that will yield Auth0 user's credentials
- seeAlso: Authentication#exchangeCode(codeVerifier:redirectURI:) for PKCE
*/
public func token(parameters: [String: AnyObject]) -> Request<Credentials, Error> {
public func token(parameters: [String: AnyObject]) -> Request<Credentials, AuthenticationError> {
var payload: [String: AnyObject] = [
"client_id": self.clientId
]
Expand Down Expand Up @@ -448,7 +449,7 @@ public struct Authentication {
- returns: a request that will yield Auth0 user's credentials
- seeAlso: https://tools.ietf.org/html/rfc7636
*/
public func exchangeCode(code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, Error> {
public func exchangeCode(code: String, codeVerifier: String, redirectURI: String) -> Request<Credentials, AuthenticationError> {
return self.token([
"code": code,
"code_verifier": codeVerifier,
Expand Down
Loading