reset auth-token if identifierForVendor changed

This commit is contained in:
Stephan Diederich
2013-09-07 19:01:17 +02:00
parent 41d7a0da6a
commit 2d97ac7cf7
2 changed files with 42 additions and 1 deletions

View File

@@ -14,6 +14,7 @@
#import "BITHockeyAppClient.h"
static NSString* const kBITAuthenticatorAuthTokenKey = @"BITAuthenticatorAuthTokenKey";
static NSString* const kBITAuthenticatorAuthTokenVendorIdentifierKey = @"BITAuthenticatorAuthTokenVendorIdentifierKey";
static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthenticatorLastAuthenticatedVersionKey";
@implementation BITAuthenticator {
@@ -459,6 +460,7 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe
- (void) cleanupInternalStorage {
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenKey];
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenVendorIdentifierKey];
[self setLastAuthenticatedVersion:nil];
}
@@ -486,15 +488,29 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe
[self willChangeValueForKey:@"installationIdentification"];
if(nil == authenticationToken) {
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenKey];
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenVendorIdentifierKey];
} else {
[self addStringValueToKeychain:authenticationToken forKey:kBITAuthenticatorAuthTokenKey];
NSString *identifierForVendor = self.currentDevice.identifierForVendor.UUIDString;
[self addStringValueToKeychain:identifierForVendor forKey:kBITAuthenticatorAuthTokenVendorIdentifierKey];
}
[self didChangeValueForKey:@"installationIdentification"];
}
}
- (NSString *)authenticationToken {
return [self stringValueFromKeychainForKey:kBITAuthenticatorAuthTokenKey];
NSString *authToken = [self stringValueFromKeychainForKey:kBITAuthenticatorAuthTokenKey];
if(nil == authToken) return nil;
//check if this was generated on the same device we're running now
NSString *currentVendorUUIDString = self.currentDevice.identifierForVendor.UUIDString;
if(![currentVendorUUIDString isEqualToString:[self stringValueFromKeychainForKey:kBITAuthenticatorAuthTokenVendorIdentifierKey]]) {
BITHockeyLog(@"Vendor identifier mismatch for stored auth-token. Resetting.");
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenVendorIdentifierKey];
[self removeKeyFromKeychain:kBITAuthenticatorAuthTokenKey];
return nil;
}
return authToken;
}
- (void)setLastAuthenticatedVersion:(NSString *)lastAuthenticatedVersion {

View File

@@ -27,6 +27,21 @@
- (NSString*) uniqueIdentifier {return @"reallyUnique";}
@end
@interface MyDeviceWithIdentifierForVendor : MyDevice
@property (copy) NSUUID *identifierForVendor;
@end
@implementation MyDeviceWithIdentifierForVendor
- (id)init {
self = [super init];
if( self ) {
_identifierForVendor = [NSUUID UUID];
}
return self;
}
@end
static void *kInstallationIdentification = &kInstallationIdentification;
@interface BITAuthenticatorTests : SenTestCase
@@ -325,4 +340,14 @@ static void *kInstallationIdentification = &kInstallationIdentification;
assertThat(error, nilValue());
}
- (void) testThatAuthTokenIsResettingWhenVendorIdentifierChanged {
MyDeviceWithIdentifierForVendor *device = [MyDeviceWithIdentifierForVendor new];
_sut.currentDevice = (id)device;
[_sut didAuthenticateWithToken:@"SuperToken"];
NSString *ident = [_sut installationIdentification];
assertThat(ident, equalTo(@"SuperToken"));
device.identifierForVendor = [NSUUID UUID];
ident = [_sut installationIdentification];
assertThat(ident, isNot(equalTo(@"SuperToken")));
}
@end