Skip to content

Commit

Permalink
Merge pull request #173 from b123400/log-more-source
Browse files Browse the repository at this point in the history
Log more warning and sources
  • Loading branch information
zdavatz authored Aug 18, 2023
2 parents 65395b0 + 997d22b commit 773f5b4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
24 changes: 24 additions & 0 deletions AmiKoDesitin/HINClient/MLHINADSwissAuthHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

#import "MLHINADSwissAuthHandle.h"

@interface MLHINADSwissAuthHandle ()

@property (nonatomic, strong) NSString *sourceForDebug;

@end

@implementation MLHINADSwissAuthHandle

- (instancetype)initWithToken:(NSString *)token {
if (self = [super init]) {
self.token = token;
self.expiresAt = [[NSDate date] dateByAddingTimeInterval:12*60*60];
self.lastUsedAt = [NSDate date];
self.sourceForDebug = [MLHINADSwissAuthHandle buildEnvironment];
}
return self;
}
Expand All @@ -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;
}
Expand All @@ -33,6 +41,7 @@ - (NSDictionary *)dictionaryRepresentation {
@"token": self.token,
@"lastUsedAt": self.lastUsedAt,
@"expiresAt": self.expiresAt,
@"source": self.sourceForDebug ?: @"",
};
}

Expand All @@ -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
2 changes: 2 additions & 0 deletions AmiKoDesitin/HINClient/MLHINClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions AmiKoDesitin/HINClient/MLHINTokens.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"];
Expand All @@ -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,
Expand All @@ -57,6 +84,7 @@ - (NSDictionary *)dictionaryRepresentation {
@"hinId": self.hinId,
@"tokenType": self.tokenType,
@"application": [NSNumber numberWithUnsignedInteger:self.application],
@"source": self.sourceForDebug ?: @"",
};
}

Expand Down
1 change: 1 addition & 0 deletions AmiKoDesitin/MLSettingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions AmiKoDesitin/PrescriptionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 773f5b4

Please sign in to comment.