add authentication logic

This commit is contained in:
Stephan Diederich 2013-08-15 22:27:20 +02:00
parent 9ae6fa060e
commit a530df813d
2 changed files with 102 additions and 37 deletions

View File

@ -12,6 +12,15 @@
@interface BITAuthenticationViewController : UITableViewController @interface BITAuthenticationViewController : UITableViewController
- (instancetype) initWithApplicationIdentifier:(NSString*) encodedApplicationIdentifier
requirePassword:(BOOL) requiresPassword
delegate:(id<BITAuthenticationViewControllerDelegate>) delegate;
/**
* The application's id to identifiy it in the backend
*/
@property (nonatomic, copy) NSString *encodedApplicationIdentifier;
/** /**
* can be set to YES to also require the users password * can be set to YES to also require the users password
* *

View File

@ -22,10 +22,16 @@
@implementation BITAuthenticationViewController @implementation BITAuthenticationViewController
- (id)initWithStyle:(UITableViewStyle)style { - (instancetype) initWithApplicationIdentifier:(NSString*) encodedApplicationIdentifier
self = [super initWithStyle:style]; requirePassword:(BOOL) requiresPassword
delegate:(id<BITAuthenticationViewControllerDelegate>) delegate {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) { if (self) {
self.title = BITHockeyLocalizedString(@"HockeyAuthenticatorViewControllerTitle"); self.title = BITHockeyLocalizedString(@"HockeyAuthenticatorViewControllerTitle");
_encodedApplicationIdentifier = [encodedApplicationIdentifier copy];
_requirePassword = requiresPassword;
_delegate = delegate;
_showsCancelButton = YES;
} }
return self; return self;
} }
@ -231,38 +237,71 @@
- (void)saveAction:(id)sender { - (void)saveAction:(id)sender {
[self showLoginUI:YES]; [self showLoginUI:YES];
NSParameterAssert(self.encodedApplicationIdentifier);
NSString *authenticationEndpoint = @"authenticate";
__weak typeof (self) weakSelf = self;
[self.authenticator getPath:authenticationEndpoint NSString *authenticationPath = [self authenticationPath];
completion:^(BITHTTPOperation *operation, id response, NSError *error) { NSDictionary *params = [self parametersForAuthentication];
typeof (self) strongSelf = weakSelf;
if(nil == response) { __weak typeof (self) weakSelf = self;
//TODO think about alertview messages [self.authenticator postPath:authenticationPath
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil parameters:params
message:@"Failed to authenticate" completion:^(BITHTTPOperation *operation, id response, NSError *error) {
delegate:nil typeof (self) strongSelf = weakSelf;
cancelButtonTitle:BITHockeyLocalizedString(@"OK") if(nil == response) {
otherButtonTitles:nil]; //TODO think about alertview messages
[alert show]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
} else { message:@"Failed to authenticate"
NSError *authParseError = nil; delegate:nil
NSString *authToken = [strongSelf.class authenticationTokenFromReponse:response cancelButtonTitle:BITHockeyLocalizedString(@"OK")
error:&authParseError]; otherButtonTitles:nil];
if(nil == authToken) { [alert show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil } else if(401 == operation.response.statusCode) {
message:@"Failed to authenticate" UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
delegate:nil message:@"Not authorized"
cancelButtonTitle:BITHockeyLocalizedString(@"OK") delegate:nil
otherButtonTitles:nil]; cancelButtonTitle:BITHockeyLocalizedString(@"OK")
[alert show]; otherButtonTitles:nil];
} else { [alert show];
[strongSelf.delegate authenticationViewController:strongSelf authenticatedWithToken:authToken]; } else {
} NSError *authParseError = nil;
} NSString *authToken = [strongSelf.class authenticationTokenFromReponse:response
[self showLoginUI:NO]; error:&authParseError];
}]; if(nil == authToken) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"Failed to authenticate"
delegate:nil
cancelButtonTitle:BITHockeyLocalizedString(@"OK")
otherButtonTitles:nil];
[alert show];
} else {
[strongSelf.delegate authenticationViewController:strongSelf authenticatedWithToken:authToken];
}
}
[self showLoginUI:NO];
}];
}
- (NSDictionary *) parametersForAuthentication {
if(self.requirePassword) {
return @{ @"user" : [NSString stringWithFormat:@"%@:%@", self.email, self.password] };
} else {
NSString *authCode = BITHockeyMD5([NSString stringWithFormat:@"%@%@",
self.authenticator.authenticationSecret ? : @"",
self.email ? : @""]);
return @{
@"email" : self.email,
@"authcode" : authCode.lowercaseString,
};
}
}
- (NSString *) authenticationPath {
if(self.requirePassword) {
return [NSString stringWithFormat:@"api/3/apps/%@/identity/authorize", self.encodedApplicationIdentifier];
} else {
return [NSString stringWithFormat:@"api/3/apps/%@/identity/check", self.encodedApplicationIdentifier];
}
} }
- (void) showLoginUI:(BOOL) enableLoginUI { - (void) showLoginUI:(BOOL) enableLoginUI {
@ -283,7 +322,7 @@
code:BITAuthenticatorAPIServerReturnedInvalidRespone code:BITAuthenticatorAPIServerReturnedInvalidRespone
userInfo:(jsonParseError ? @{NSUnderlyingErrorKey : jsonParseError} : nil)]; userInfo:(jsonParseError ? @{NSUnderlyingErrorKey : jsonParseError} : nil)];
} }
return NO; return nil;
} }
if(![jsonObject isKindOfClass:[NSDictionary class]]) { if(![jsonObject isKindOfClass:[NSDictionary class]]) {
if(error) { if(error) {
@ -291,10 +330,27 @@
code:BITAuthenticatorAPIServerReturnedInvalidRespone code:BITAuthenticatorAPIServerReturnedInvalidRespone
userInfo:nil]; userInfo:nil];
} }
return NO; return nil;
}
NSString *status = jsonObject[@"status"];
if(nil == status) {
if(error) {
*error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
code:BITAuthenticatorAPIServerReturnedInvalidRespone
userInfo:nil];
}
return nil;
} else if([status isEqualToString:@"identified"]) {
return jsonObject[@"iuid"];
} else if([status isEqualToString:@"authorized"]) {
return jsonObject[@"auid"];
} else {
if(error) {
*error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
code:BITAuthenticatorNotAuthorized
userInfo:nil];
}
return nil;
} }
//TODO: add proper validation
return jsonObject[@"authToken"];
} }
@end @end