diff --git a/AmiKoDesitin/HINClient/MLHINADSwissAuthHandle.m b/AmiKoDesitin/HINClient/MLHINADSwissAuthHandle.m index fa11261..0ba0a8a 100644 --- a/AmiKoDesitin/HINClient/MLHINADSwissAuthHandle.m +++ b/AmiKoDesitin/HINClient/MLHINADSwissAuthHandle.m @@ -8,6 +8,12 @@ #import "MLHINADSwissAuthHandle.h" +@interface MLHINADSwissAuthHandle () + +@property (nonatomic, strong) NSString *sourceForDebug; + +@end + @implementation MLHINADSwissAuthHandle - (instancetype)initWithToken:(NSString *)token { @@ -15,6 +21,7 @@ - (instancetype)initWithToken:(NSString *)token { self.token = token; self.expiresAt = [[NSDate date] dateByAddingTimeInterval:12*60*60]; self.lastUsedAt = [NSDate date]; + self.sourceForDebug = [MLHINADSwissAuthHandle buildEnvironment]; } return self; } @@ -24,6 +31,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict { self.token = dict[@"token"]; self.lastUsedAt = dict[@"lastUsedAt"]; self.expiresAt = dict[@"expiresAt"]; + self.sourceForDebug = dict[@"source"]; } return self; } @@ -33,6 +41,7 @@ - (NSDictionary *)dictionaryRepresentation { @"token": self.token, @"lastUsedAt": self.lastUsedAt, @"expiresAt": self.expiresAt, + @"source": self.sourceForDebug ?: @"", }; } @@ -45,4 +54,19 @@ - (void)updateLastUsedAt { self.lastUsedAt = [NSDate date]; } +- (NSString *)token { + if (self.sourceForDebug.length && ![self.sourceForDebug isEqual:[MLHINADSwissAuthHandle buildEnvironment]]) { + NSLog(@"WARNING: An auth handle generated in %@ environment is being used in %@ environment", self.sourceForDebug, [MLHINADSwissAuthHandle buildEnvironment]); + } + return _token; +} + ++ (NSString *)buildEnvironment { +#ifdef DEBUG + return @"debug"; +#else + return @"release"; +#endif +} + @end diff --git a/AmiKoDesitin/HINClient/MLHINClient.m b/AmiKoDesitin/HINClient/MLHINClient.m index e845f0b..3833a14 100644 --- a/AmiKoDesitin/HINClient/MLHINClient.m +++ b/AmiKoDesitin/HINClient/MLHINClient.m @@ -304,6 +304,7 @@ - (void)fetchADSwissSAMLWithToken:(MLHINTokens *)token completion:(void (^_Nonnu @"Authorization": [NSString stringWithFormat:@"Bearer %@", token.accessToken], }]; [request setHTTPMethod:@"POST"]; + NSLog(@"Fetching SAML from: %@", request.URL); [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error != nil) { callback(error, nil); @@ -350,6 +351,7 @@ - (void)fetchADSwissAuthHandleWithToken:(MLHINTokens *)token return; } [request setHTTPMethod:@"POST"]; + NSLog(@"Fetching Auth Handle from: %@", request.URL); [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error != nil) { callback(error, nil); diff --git a/AmiKoDesitin/HINClient/MLHINTokens.m b/AmiKoDesitin/HINClient/MLHINTokens.m index 2ed7112..6c8f6fe 100644 --- a/AmiKoDesitin/HINClient/MLHINTokens.m +++ b/AmiKoDesitin/HINClient/MLHINTokens.m @@ -15,6 +15,9 @@ @interface MLHINTokens () @property (nonatomic, strong) NSDate *expiresAt; @property (nonatomic, strong) NSString *hinId; @property (nonatomic, strong) NSString *tokenType; +// We want to track where does this token comes from, +// so we can probably show logs and error message better +@property (nonatomic, strong) NSString *sourceForDebug; @end @@ -33,10 +36,19 @@ - (instancetype)initWithResponseJSON:(NSDictionary *)dict { self.expiresAt = [[NSDate date] dateByAddingTimeInterval:[expiresIn doubleValue]]; self.hinId = dict[@"hin_id"]; self.tokenType = dict[@"token_type"]; + self.sourceForDebug = [MLHINTokens buildEnvironment]; } return self; } ++ (NSString *)buildEnvironment { +#ifdef DEBUG + return @"debug"; +#else + return @"release"; +#endif +} + - (instancetype)initWithDictionary:(NSDictionary *)dict { if (self = [super init]) { self.accessToken = dict[@"accessToken"]; @@ -45,10 +57,25 @@ - (instancetype)initWithDictionary:(NSDictionary *)dict { self.hinId = dict[@"hinId"]; self.tokenType = dict[@"tokenType"]; self.application = dict[@"application"] ? [dict[@"application"] unsignedIntegerValue] : MLHINTokensApplicationSDS; + self.sourceForDebug = dict[@"source"]; } return self; } +- (NSString *)accessToken { + if (self.sourceForDebug.length && ![self.sourceForDebug isEqual:[MLHINTokens buildEnvironment]]) { + NSLog(@"WARNING: A token generated in %@ environment is being used in %@ environment", self.sourceForDebug, [MLHINTokens buildEnvironment]); + } + return _accessToken; +} + +- (NSString *)refreshToken { + if (self.sourceForDebug.length && ![self.sourceForDebug isEqual:[MLHINTokens buildEnvironment]]) { + NSLog(@"WARNING: A token generated in %@ environment is being used in %@ environment", self.sourceForDebug, [MLHINTokens buildEnvironment]); + } + return _refreshToken; +} + - (NSDictionary *)dictionaryRepresentation { return @{ @"accessToken": self.accessToken, @@ -57,6 +84,7 @@ - (NSDictionary *)dictionaryRepresentation { @"hinId": self.hinId, @"tokenType": self.tokenType, @"application": [NSNumber numberWithUnsignedInteger:self.application], + @"source": self.sourceForDebug ?: @"", }; } diff --git a/AmiKoDesitin/MLSettingViewController.m b/AmiKoDesitin/MLSettingViewController.m index 51699e9..6e89972 100644 --- a/AmiKoDesitin/MLSettingViewController.m +++ b/AmiKoDesitin/MLSettingViewController.m @@ -227,6 +227,7 @@ - (IBAction)loginwithHINADSwissClicked:(id)sender { if (!tokens) { typeof(self) __weak _self = self; NSURL *url = [[MLHINClient shared] authURLForADSwiss]; + NSLog(@"Authenticating with URL: %@", url); ASWebAuthenticationSession *session = [[ASWebAuthenticationSession alloc] initWithURL:url callbackURLScheme:[[MLHINClient shared] oauthCallbackScheme] completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) { diff --git a/AmiKoDesitin/PrescriptionViewController.m b/AmiKoDesitin/PrescriptionViewController.m index 82f325c..a0ffa6f 100644 --- a/AmiKoDesitin/PrescriptionViewController.m +++ b/AmiKoDesitin/PrescriptionViewController.m @@ -2280,17 +2280,18 @@ - (void)sharePrescription:(NSURL *)urlAttachment withEPrescription:(BOOL)useEPre [self presentViewController:alert animated:YES completion:nil]; [self getEPrescriptionQRCodeWithCallback:^(NSError * _Nullable error, UIImage * _Nullable qrCode) { dispatch_async(dispatch_get_main_queue(), ^{ - [alert dismissViewControllerAnimated:YES completion:nil]; - if (error) { - UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", @"") - message:error.localizedDescription - preferredStyle:UIAlertControllerStyleAlert]; - [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"") style:UIAlertActionStyleDefault handler:nil]]; - [_self presentViewController:alert animated:YES completion:nil]; - return; - } - NSMutableData *pdfData = [self renderPdfForPrintingWithEPrescriptionQRCode:qrCode]; - [_self sharePrescription:urlAttachment withPdfData:pdfData]; + [alert dismissViewControllerAnimated:YES completion:^{ + if (error) { + UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", @"") + message:error.localizedDescription + preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"") style:UIAlertActionStyleDefault handler:nil]]; + [_self presentViewController:alert animated:YES completion:nil]; + return; + } + NSMutableData *pdfData = [self renderPdfForPrintingWithEPrescriptionQRCode:qrCode]; + [_self sharePrescription:urlAttachment withPdfData:pdfData]; + }]; }); }]; } else {