mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 14:45:21 +00:00
add "webAuth" identificationType
This commit is contained in:
@@ -75,6 +75,18 @@ typedef NS_ENUM(NSUInteger, BITAuthenticatorIdentificationType) {
|
|||||||
* for further documentation on this.
|
* for further documentation on this.
|
||||||
*/
|
*/
|
||||||
BITAuthenticatorIdentificationTypeDevice,
|
BITAuthenticatorIdentificationTypeDevice,
|
||||||
|
/**
|
||||||
|
* Ask for the HockeyApp account email.
|
||||||
|
* <br/><br/>
|
||||||
|
* This will present a user interface requesting the user to start a Safari based
|
||||||
|
* flow to login to HockeyApp (if not already logged in) and to share the hockeyapp
|
||||||
|
* account's email.
|
||||||
|
* <br/><br/>
|
||||||
|
* If restrictApplicationUsage is enabled, the provided user account has to match a
|
||||||
|
* registered HockeyApp user who is a member or tester of the app.
|
||||||
|
* For identification purpose any HockeyApp user is allowed.
|
||||||
|
*/
|
||||||
|
BITAuthenticatorIdentificationTypeWebAuth,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -180,6 +180,11 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
viewController.showsLoginViaWebButton = YES;
|
viewController.showsLoginViaWebButton = YES;
|
||||||
viewController.tableViewTitle = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerWebUDIDLoginDescription");
|
viewController.tableViewTitle = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerWebUDIDLoginDescription");
|
||||||
break;
|
break;
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth:
|
||||||
|
viewController = [[BITAuthenticationViewController alloc] initWithDelegate:self];
|
||||||
|
viewController.requirePassword = NO;
|
||||||
|
viewController.showsLoginViaWebButton = YES;
|
||||||
|
viewController.tableViewTitle = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerWebAuthLoginDescription");
|
||||||
break;
|
break;
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
||||||
if(nil == self.authenticationSecret) {
|
if(nil == self.authenticationSecret) {
|
||||||
@@ -230,6 +235,7 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
//no break
|
//no break
|
||||||
case BITAuthenticatorIdentificationTypeDevice:
|
case BITAuthenticatorIdentificationTypeDevice:
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth:
|
||||||
if(nil == self.installationIdentifier) {
|
if(nil == self.installationIdentifier) {
|
||||||
error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
|
error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
|
||||||
code:BITAuthenticatorNotIdentified
|
code:BITAuthenticatorNotIdentified
|
||||||
@@ -483,7 +489,25 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (NSURL *)deviceAuthenticationURL {
|
- (NSURL *)deviceAuthenticationURL {
|
||||||
return [self.webpageURL URLByAppendingPathComponent:[NSString stringWithFormat:@"apps/%@/authorize", self.encodedAppIdentifier]];
|
NSString *whatParameter = nil;
|
||||||
|
switch (self.identificationType) {
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth:
|
||||||
|
whatParameter = @"email";
|
||||||
|
break;
|
||||||
|
case BITAuthenticatorIdentificationTypeDevice:
|
||||||
|
whatParameter = @"udid";
|
||||||
|
break;
|
||||||
|
case BITAuthenticatorIdentificationTypeAnonymous:
|
||||||
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
||||||
|
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
||||||
|
NSAssert(NO,@"Should not happen. Those identification types don't need an authentication URL");
|
||||||
|
return nil;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
NSURL *url = [self.webpageURL URLByAppendingPathComponent:[NSString stringWithFormat:@"apps/%@/authorize", self.encodedAppIdentifier]];
|
||||||
|
NSParameterAssert(whatParameter && url.absoluteString);
|
||||||
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?what=%@", url.absoluteString, whatParameter]];
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)authenticationViewControllerDidTapWebButton:(UIViewController *)viewController {
|
- (void)authenticationViewControllerDidTapWebButton:(UIViewController *)viewController {
|
||||||
@@ -496,19 +520,46 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
- (BOOL) handleOpenURL:(NSURL *) url
|
- (BOOL) handleOpenURL:(NSURL *) url
|
||||||
sourceApplication:(NSString *) sourceApplication
|
sourceApplication:(NSString *) sourceApplication
|
||||||
annotation:(id) annotation {
|
annotation:(id) annotation {
|
||||||
BOOL isValidURL = NO;
|
//check if this URL was meant for us, if not return NO so the user can
|
||||||
NSString *udid = [self UDIDFromOpenURL:url annotation:annotation isValidURL:&isValidURL];
|
//handle it
|
||||||
if(NO == isValidURL) {
|
NSString *const kAuthorizationHost = @"authorize";
|
||||||
//do nothing, was not for us
|
NSString *urlScheme = _urlScheme ? : [NSString stringWithFormat:@"ha%@", self.appIdentifier];
|
||||||
|
if(!([[url scheme] isEqualToString:urlScheme] && [[url host] isEqualToString:kAuthorizationHost])) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(udid){
|
NSString *installationIdentifier = nil;
|
||||||
|
NSString *localizedErrorDescription = nil;
|
||||||
|
switch (self.identificationType) {
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth: {
|
||||||
|
NSString *email = nil;
|
||||||
|
[self.class email:&email andIUID:&installationIdentifier fromOpenURL:url];
|
||||||
|
if(email) {
|
||||||
|
[self addStringValueToKeychain:email forKey:kBITAuthenticatorUserEmailKey];
|
||||||
|
} else {
|
||||||
|
BITHockeyLog(@"No email found in URL: %@", url);
|
||||||
|
}
|
||||||
|
localizedErrorDescription = @"Failed to retrieve parameters from URL.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BITAuthenticatorIdentificationTypeDevice: {
|
||||||
|
installationIdentifier = [self.class UDIDFromOpenURL:url annotation:annotation];
|
||||||
|
localizedErrorDescription = @"Failed to retrieve UDID from URL.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
||||||
|
case BITAuthenticatorIdentificationTypeAnonymous:
|
||||||
|
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
||||||
|
NSAssert(NO, @"Should only be called for Device and WebAuth identificationType");
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(installationIdentifier){
|
||||||
if(NO == self.restrictApplicationUsage) {
|
if(NO == self.restrictApplicationUsage) {
|
||||||
[_authenticationController dismissViewControllerAnimated:YES completion:nil];
|
[_authenticationController dismissViewControllerAnimated:YES completion:nil];
|
||||||
_authenticationController = nil;
|
_authenticationController = nil;
|
||||||
}
|
}
|
||||||
[self storeInstallationIdentifier:udid withType:BITAuthenticatorIdentificationTypeDevice];
|
[self storeInstallationIdentifier:installationIdentifier withType:self.identificationType];
|
||||||
self.identified = YES;
|
self.identified = YES;
|
||||||
if(self.identificationCompletion) {
|
if(self.identificationCompletion) {
|
||||||
self.identificationCompletion(YES, nil);
|
self.identificationCompletion(YES, nil);
|
||||||
@@ -516,12 +567,12 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//reset token
|
//reset token
|
||||||
[self storeInstallationIdentifier:nil withType:BITAuthenticatorIdentificationTypeDevice];
|
[self storeInstallationIdentifier:nil withType:self.identificationType];
|
||||||
self.identified = NO;
|
self.identified = NO;
|
||||||
if(self.identificationCompletion) {
|
if(self.identificationCompletion) {
|
||||||
NSError *error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
|
NSError *error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain
|
||||||
code:BITAuthenticatorErrorUnknown
|
code:BITAuthenticatorErrorUnknown
|
||||||
userInfo:@{NSLocalizedDescriptionKey : @"Failed to retrieve UDID from URL"}];
|
userInfo:@{NSLocalizedDescriptionKey : localizedErrorDescription}];
|
||||||
self.identificationCompletion(NO, error);
|
self.identificationCompletion(NO, error);
|
||||||
self.identificationCompletion = nil;
|
self.identificationCompletion = nil;
|
||||||
}
|
}
|
||||||
@@ -529,13 +580,7 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *) UDIDFromOpenURL:(NSURL *) url annotation:(id) annotation isValidURL:(BOOL*) isValid{
|
+ (NSString *) UDIDFromOpenURL:(NSURL *) url annotation:(id) annotation {
|
||||||
NSString *const kAuthorizationHost = @"authorize";
|
|
||||||
NSString *urlScheme = _urlScheme ? : [NSString stringWithFormat:@"ha%@", self.appIdentifier];
|
|
||||||
if([[url scheme] isEqualToString:urlScheme] && [[url host] isEqualToString:kAuthorizationHost]) {
|
|
||||||
if(isValid) {
|
|
||||||
*isValid = YES;
|
|
||||||
}
|
|
||||||
NSString *query = [url query];
|
NSString *query = [url query];
|
||||||
NSString *udid = nil;
|
NSString *udid = nil;
|
||||||
//there should actually only one
|
//there should actually only one
|
||||||
@@ -548,11 +593,20 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return udid;
|
return udid;
|
||||||
} else {
|
|
||||||
if(isValid) {
|
|
||||||
*isValid = NO;
|
|
||||||
}
|
}
|
||||||
return nil;
|
|
||||||
|
+ (void) email:(NSString**) email andIUID:(NSString**) iuid fromOpenURL:(NSURL *) url {
|
||||||
|
NSString *query = [url query];
|
||||||
|
//there should actually only one
|
||||||
|
static NSString * const EmailQuerySpecifier = @"email";
|
||||||
|
static NSString * const IUIDQuerySpecifier = @"iuid";
|
||||||
|
for(NSString *queryComponents in [query componentsSeparatedByString:@"&"]) {
|
||||||
|
NSArray *parameterComponents = [queryComponents componentsSeparatedByString:@"="];
|
||||||
|
if(email && 2 == parameterComponents.count && [parameterComponents[0] isEqualToString:EmailQuerySpecifier]) {
|
||||||
|
*email = parameterComponents[1];
|
||||||
|
} else if(iuid && 2 == parameterComponents.count && [parameterComponents[0] isEqualToString:IUIDQuerySpecifier]) {
|
||||||
|
*iuid = parameterComponents[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -642,6 +696,7 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
- (NSString *)installationIdentifierParameterString {
|
- (NSString *)installationIdentifierParameterString {
|
||||||
switch(self.identificationType) {
|
switch(self.identificationType) {
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth:
|
||||||
return @"iuid";
|
return @"iuid";
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppUser: return @"auid";
|
case BITAuthenticatorIdentificationTypeHockeyAppUser: return @"auid";
|
||||||
case BITAuthenticatorIdentificationTypeDevice: return @"udid";
|
case BITAuthenticatorIdentificationTypeDevice: return @"udid";
|
||||||
@@ -652,6 +707,7 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
+ (NSString *)stringForIdentificationType:(BITAuthenticatorIdentificationType) identificationType {
|
+ (NSString *)stringForIdentificationType:(BITAuthenticatorIdentificationType) identificationType {
|
||||||
switch(identificationType) {
|
switch(identificationType) {
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppEmail: return @"iuid";
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail: return @"iuid";
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth: return @"webAuth";
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppUser: return @"auid";
|
case BITAuthenticatorIdentificationTypeHockeyAppUser: return @"auid";
|
||||||
case BITAuthenticatorIdentificationTypeDevice: return @"udid";
|
case BITAuthenticatorIdentificationTypeDevice: return @"udid";
|
||||||
case BITAuthenticatorIdentificationTypeAnonymous: return @"uuid";
|
case BITAuthenticatorIdentificationTypeAnonymous: return @"uuid";
|
||||||
@@ -670,6 +726,7 @@ static NSString* const kBITAuthenticatorAuthTokenTypeKey = @"BITAuthenticatorAut
|
|||||||
switch (self.identificationType) {
|
switch (self.identificationType) {
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
case BITAuthenticatorIdentificationTypeHockeyAppEmail:
|
||||||
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
case BITAuthenticatorIdentificationTypeHockeyAppUser:
|
||||||
|
case BITAuthenticatorIdentificationTypeWebAuth:
|
||||||
return [self stringValueFromKeychainForKey:kBITAuthenticatorUserEmailKey];
|
return [self stringValueFromKeychainForKey:kBITAuthenticatorUserEmailKey];
|
||||||
case BITAuthenticatorIdentificationTypeAnonymous:
|
case BITAuthenticatorIdentificationTypeAnonymous:
|
||||||
case BITAuthenticatorIdentificationTypeDevice:
|
case BITAuthenticatorIdentificationTypeDevice:
|
||||||
|
|||||||
@@ -189,6 +189,16 @@ static void *kInstallationIdentification = &kInstallationIdentification;
|
|||||||
|
|
||||||
[verifyCount(delegateMock, times(1)) authenticator:_sut willShowAuthenticationController:(id)anything()];
|
[verifyCount(delegateMock, times(1)) authenticator:_sut willShowAuthenticationController:(id)anything()];
|
||||||
}
|
}
|
||||||
|
#pragma mark - Web auth identification type
|
||||||
|
- (void) testWebAuthIdentificationShowsViewController {
|
||||||
|
_sut.identificationType = BITAuthenticatorIdentificationTypeWebAuth;
|
||||||
|
id delegateMock = mockProtocol(@protocol(BITAuthenticatorDelegate));
|
||||||
|
_sut.delegate = delegateMock;
|
||||||
|
|
||||||
|
[_sut identifyWithCompletion:nil];
|
||||||
|
|
||||||
|
[verifyCount(delegateMock, times(1)) authenticator:_sut willShowAuthenticationController:(id)anything()];
|
||||||
|
}
|
||||||
|
|
||||||
#pragma mark - Email identification type
|
#pragma mark - Email identification type
|
||||||
- (void) testEmailIdentificationFailsWithMissingSecret {
|
- (void) testEmailIdentificationFailsWithMissingSecret {
|
||||||
|
|||||||
Reference in New Issue
Block a user