Skip to content

Commit

Permalink
{Authentication,Management}Error available in ObjC
Browse files Browse the repository at this point in the history
  • Loading branch information
hzalaz committed Jun 18, 2016
1 parent 30135f9 commit 287f821
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 35 deletions.
3 changes: 0 additions & 3 deletions Auth0/Auth0Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public protocol Auth0Error: ErrorType {
/// The code of the error as a String
var code: String { get }

/// A basic description of the error.
var description: String { get }

}

internal protocol FoundationErrorConvertible {
Expand Down
8 changes: 4 additions & 4 deletions Auth0/Authentication/AuthenticationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Foundation
/**
* Represents an error during a request to Auth0 Authentication API
*/
public class AuthenticationError: Auth0Error, CustomStringConvertible {
public class AuthenticationError: NSObject, Auth0Error {

/**
Additional information about the error
Expand Down Expand Up @@ -72,7 +72,7 @@ public class AuthenticationError: Auth0Error, CustomStringConvertible {
Description of the error
- important: You should avoid displaying description to the user, it's meant for debugging only.
*/
public var description: String {
public override var description: String {
let description = self.info["description"] ?? self.info["error_description"]
if let string = description as? String {
return string
Expand Down Expand Up @@ -134,8 +134,8 @@ public class AuthenticationError: Auth0Error, CustomStringConvertible {
}

extension AuthenticationError: FoundationErrorConvertible {
static let FoundationDomain = "com.auth0.authentication"
static let FoundationUserInfoKey = "com.auth0.authentication.error.info"
@nonobjc static let FoundationDomain = "com.auth0.authentication"
@nonobjc static let FoundationUserInfoKey = "com.auth0.authentication.error.info"

public func newFoundationError() -> NSError {
return NSError(domain: AuthenticationError.FoundationDomain, code: 1, userInfo: [
Expand Down
20 changes: 13 additions & 7 deletions Auth0/Authentication/ObjectiveC/NSError+Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ import Foundation

public extension NSError {

func a0_authenticationError() -> Bool {
/**
Check if the NSError was created from an AuthenticationError

- returns: if it's an authentication error from Auth0
*/
func a0_isAuthenticationError() -> Bool {
return self.domain == AuthenticationError.FoundationDomain
}

func a0_authenticationErrorWithCode(code: String) -> Bool {
return self.a0_authenticationError() && a0_authenticationErrorCode() == code
}
/**
Returns the Auth0 Authentication Error

func a0_authenticationErrorCode() -> String? {
guard let error = self.userInfo[AuthenticationError.FoundationUserInfoKey] as? AuthenticationError else { return nil }
return error.code
- returns: authentication error
- seeAlso: AuthenticationError
*/
func a0_authenticationError() -> AuthenticationError? {
return self.userInfo[AuthenticationError.FoundationUserInfoKey] as? AuthenticationError
}

}
8 changes: 4 additions & 4 deletions Auth0/Management/ManagementError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Foundation
/**
* Represents an error during a request to Auth0 Management API
*/
public class ManagementError: Auth0Error {
public class ManagementError: NSObject, Auth0Error {

/**
Additional information about the error
Expand Down Expand Up @@ -69,7 +69,7 @@ public class ManagementError: Auth0Error {
Description of the error
- important: You should avoid displaying description to the user, it's meant for debugging only.
*/
public var description: String {
public override var description: String {
if let string = self.info["description"] as? String {
return string
}
Expand All @@ -79,8 +79,8 @@ public class ManagementError: Auth0Error {
}

extension ManagementError: FoundationErrorConvertible {
static let FoundationDomain = "com.auth0.management"
static let FoundationUserInfoKey = "com.auth0.management.error.info"
@nonobjc static let FoundationDomain = "com.auth0.management"
@nonobjc static let FoundationUserInfoKey = "com.auth0.management.error.info"

func newFoundationError() -> NSError {
return NSError(domain: ManagementError.FoundationDomain, code: 1, userInfo: [
Expand Down
24 changes: 15 additions & 9 deletions Auth0/Management/ObjectiveC/NSError+Management.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ import Foundation

public extension NSError {

func a0_managementError() -> Bool {
return self.domain == domain
}
/**
Check if the NSError was created from an ManagementError

func a0_managementErrorWithCode(code: String) -> Bool {
return self.a0_authenticationError() && a0_managementErrorCode() == code
- returns: if it's an management error from Auth0
*/
func a0_isManagementError() -> Bool {
return self.domain == ManagementError.FoundationDomain
}

func a0_managementErrorCode() -> String? {
guard let error = self.userInfo[ManagementError.FoundationUserInfoKey] as? ManagementError else { return nil }
return error.info["code"] as? String
}
/**
Returns the Auth0 Managenent Error

- returns: management error
- seeAlso: ManagementError
*/
func a0_managementError() -> ManagementError? {
return self.userInfo[ManagementError.FoundationUserInfoKey] as? ManagementError
}

}
24 changes: 16 additions & 8 deletions Auth0Tests/ObjectiveC/AuthenticationAPISpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:domain.host] && [request.URL.path isEqualToString:@"/oauth/ro"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_username_password", @"error_description": @"invalid password"}
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_user_password", @"error_description": @"invalid password"}
statusCode:401
headers:@{@"Content-Type":@"application/json"}];
}];
Expand All @@ -112,8 +112,10 @@
expect(credentials).to(beNil());
expect(error).toNot(beNil());
expect(error.domain).to(equal(@"com.auth0.authentication"));
expect(error.a0_authenticationErrorCode).to(equal(@"invalid_username_password"));
expect(@(error.a0_isManagementError)).to(beFalse());
expect(@(error.a0_isAuthenticationError)).to(beTrue());
expect(error.localizedDescription).to(equal(@"invalid password"));
expect(@([error.a0_authenticationError isInvalidCredentials])).to(beTrue());
done();
}];
});
Expand Down Expand Up @@ -156,7 +158,7 @@
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:domain.host] && [request.URL.path isEqualToString:@"/dbconnections/signup"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_username_password", @"error_description": @"invalid password"}
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_user_password", @"error_description": @"invalid password"}
statusCode:401
headers:@{@"Content-Type":@"application/json"}];
}];
Expand All @@ -171,8 +173,10 @@
expect(databaseUser).to(beNil());
expect(error).toNot(beNil());
expect(error.domain).to(equal(@"com.auth0.authentication"));
expect(error.a0_authenticationErrorCode).to(equal(@"invalid_username_password"));
expect(@(error.a0_isManagementError)).to(beFalse());
expect(@(error.a0_isAuthenticationError)).to(beTrue());
expect(error.localizedDescription).to(equal(@"invalid password"));
expect(@([error.a0_authenticationError isInvalidCredentials])).to(beTrue());
done();
}];
});
Expand Down Expand Up @@ -211,7 +215,7 @@
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:domain.host] && [request.URL.path isEqualToString:@"/dbconnections/change_password"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_username_password", @"error_description": @"invalid password"}
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_user_password", @"error_description": @"invalid password"}
statusCode:401
headers:@{@"Content-Type":@"application/json"}];
}];
Expand All @@ -222,8 +226,10 @@
callback:^(NSError * _Nullable error) {
expect(error).toNot(beNil());
expect(error.domain).to(equal(@"com.auth0.authentication"));
expect(error.a0_authenticationErrorCode).to(equal(@"invalid_username_password"));
expect(@(error.a0_isManagementError)).to(beFalse());
expect(@(error.a0_isAuthenticationError)).to(beTrue());
expect(error.localizedDescription).to(equal(@"invalid password"));
expect(@([error.a0_authenticationError isInvalidCredentials])).to(beTrue());
done();
}];
});
Expand Down Expand Up @@ -276,7 +282,7 @@
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:domain.host] && [request.URL.path isEqualToString:@"/dbconnections/signup"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
return [OHHTTPStubsResponse responseWithJSONObject:@{@"error": @"invalid_username_password", @"error_description": @"invalid password"}
return [OHHTTPStubsResponse responseWithJSONObject:@{@"code": @"invalid_password", @"description": @"invalid password", @"name": @"PasswordStrengthError"}
statusCode:401
headers:@{@"Content-Type":@"application/json"}];
}];
Expand All @@ -293,8 +299,10 @@
expect(credentials).to(beNil());
expect(error).toNot(beNil());
expect(error.domain).to(equal(@"com.auth0.authentication"));
expect(error.a0_authenticationErrorCode).to(equal(@"invalid_username_password"));
expect(@(error.a0_isManagementError)).to(beFalse());
expect(@(error.a0_isAuthenticationError)).to(beTrue());
expect(error.localizedDescription).to(equal(@"invalid password"));
expect(@([error.a0_authenticationError isPasswordNotStrongEnough])).to(beTrue());
done();
}];
});
Expand Down

0 comments on commit 287f821

Please sign in to comment.