mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-01 16:06:59 +00:00
add authentication logic
This commit is contained in:
parent
9ae6fa060e
commit
a530df813d
@ -12,6 +12,15 @@
|
||||
|
||||
@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
|
||||
*
|
||||
|
@ -22,10 +22,16 @@
|
||||
|
||||
@implementation BITAuthenticationViewController
|
||||
|
||||
- (id)initWithStyle:(UITableViewStyle)style {
|
||||
self = [super initWithStyle:style];
|
||||
- (instancetype) initWithApplicationIdentifier:(NSString*) encodedApplicationIdentifier
|
||||
requirePassword:(BOOL) requiresPassword
|
||||
delegate:(id<BITAuthenticationViewControllerDelegate>) delegate {
|
||||
self = [super initWithStyle:UITableViewStyleGrouped];
|
||||
if (self) {
|
||||
self.title = BITHockeyLocalizedString(@"HockeyAuthenticatorViewControllerTitle");
|
||||
_encodedApplicationIdentifier = [encodedApplicationIdentifier copy];
|
||||
_requirePassword = requiresPassword;
|
||||
_delegate = delegate;
|
||||
_showsCancelButton = YES;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@ -231,38 +237,71 @@
|
||||
|
||||
- (void)saveAction:(id)sender {
|
||||
[self showLoginUI:YES];
|
||||
NSParameterAssert(self.encodedApplicationIdentifier);
|
||||
|
||||
NSString *authenticationEndpoint = @"authenticate";
|
||||
__weak typeof (self) weakSelf = self;
|
||||
|
||||
[self.authenticator getPath:authenticationEndpoint
|
||||
completion:^(BITHTTPOperation *operation, id response, NSError *error) {
|
||||
typeof (self) strongSelf = weakSelf;
|
||||
if(nil == response) {
|
||||
//TODO think about alertview messages
|
||||
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
|
||||
message:@"Failed to authenticate"
|
||||
delegate:nil
|
||||
cancelButtonTitle:BITHockeyLocalizedString(@"OK")
|
||||
otherButtonTitles:nil];
|
||||
[alert show];
|
||||
} else {
|
||||
NSError *authParseError = nil;
|
||||
NSString *authToken = [strongSelf.class authenticationTokenFromReponse:response
|
||||
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];
|
||||
}];
|
||||
NSString *authenticationPath = [self authenticationPath];
|
||||
NSDictionary *params = [self parametersForAuthentication];
|
||||
|
||||
__weak typeof (self) weakSelf = self;
|
||||
[self.authenticator postPath:authenticationPath
|
||||
parameters:params
|
||||
completion:^(BITHTTPOperation *operation, id response, NSError *error) {
|
||||
typeof (self) strongSelf = weakSelf;
|
||||
if(nil == response) {
|
||||
//TODO think about alertview messages
|
||||
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
|
||||
message:@"Failed to authenticate"
|
||||
delegate:nil
|
||||
cancelButtonTitle:BITHockeyLocalizedString(@"OK")
|
||||
otherButtonTitles:nil];
|
||||
[alert show];
|
||||
} else if(401 == operation.response.statusCode) {
|
||||
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
|
||||
message:@"Not authorized"
|
||||
delegate:nil
|
||||
cancelButtonTitle:BITHockeyLocalizedString(@"OK")
|
||||
otherButtonTitles:nil];
|
||||
[alert show];
|
||||
} else {
|
||||
NSError *authParseError = nil;
|
||||
NSString *authToken = [strongSelf.class authenticationTokenFromReponse:response
|
||||
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 {
|
||||
@ -283,7 +322,7 @@
|
||||
code:BITAuthenticatorAPIServerReturnedInvalidRespone
|
||||
userInfo:(jsonParseError ? @{NSUnderlyingErrorKey : jsonParseError} : nil)];
|
||||
}
|
||||
return NO;
|
||||
return nil;
|
||||
}
|
||||
if(![jsonObject isKindOfClass:[NSDictionary class]]) {
|
||||
if(error) {
|
||||
@ -291,10 +330,27 @@
|
||||
code:BITAuthenticatorAPIServerReturnedInvalidRespone
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user