Skip to content

Commit

Permalink
Merge pull request #16 from shankari/switch_to_new_auth
Browse files Browse the repository at this point in the history
Switch to new authentication library and sign in screen from google
  • Loading branch information
shankari authored Apr 20, 2017
2 parents cc28c1f + c340641 commit 7d8a03c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 49 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="edu.berkeley.eecs.emission.cordova.comm"
version="0.0.1">
version="1.0.0">

<name>ServerComm</name>
<description>Abstraction for communication settings, and for making both GET
Expand Down
2 changes: 1 addition & 1 deletion src/ios/BEMCommunicationHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <Foundation/Foundation.h>
#import "GTMSessionFetcherService.h"
#import <GTMSessionFetcher/GTMSessionFetcherService.h>

@interface CommunicationHelper : NSObject
// Wrappers for our specific functionality
Expand Down
54 changes: 7 additions & 47 deletions src/ios/BEMCommunicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//

#import "BEMCommunicationHelper.h"
#import "AuthCompletionHandler.h"
#import "BEMConnectionSettings.h"
#import "BEMConstants.h"
#import "LocalNotificationManager.h"
#import "GTMSessionFetcher.h"
#import <GTMSessionFetcher/GTMSessionFetcherService.h>
#import "AuthCompletionHandler.h"

// This is the base URL
// We need to append the username to it, and then we need to authenticate the user as well
Expand All @@ -34,10 +34,6 @@
return aBool? @"YES" : @"NO";
}

@interface CommunicationHelper() <AuthCompletionDelegate> {
}
@end

@implementation CommunicationHelper

+(void)getCustomSettings:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler {
Expand Down Expand Up @@ -143,18 +139,17 @@ -(void)execute {
return;
}

[[AuthCompletionHandler sharedInstance] getValidAuth:^(GTMOAuth2Authentication *auth,NSError* error) {
[[AuthCompletionHandler sharedInstance] getValidAuth:^(GIDGoogleUser *user,NSError* error) {
if (error != NULL) {
self.mCompletionHandler(jsonData, NULL, error);
} else {
// TODO: have postToHost take the auth token as input instead of re-reading it
[self postToHost];
[self postToHost:user.authentication.idToken];
}
} forceRefresh:FALSE];
}];
}


- (void)postToHost {
- (void)postToHost:(NSString*)idToken {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"postToHost called with url = %@", self.mUrl] showUI:FALSE];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
Expand All @@ -164,7 +159,7 @@ - (void)postToHost {
[request setValue:@"application/json"
forHTTPHeaderField:@"Content-Type"];

NSString *userToken = [AuthCompletionHandler sharedInstance].getIdToken;
NSString *userToken = idToken;
// At this point, we assume that all the authentication is done correctly
// Should I try to verify making a remote call or add that to a debug screen?
[self.mJsonDict setObject:userToken forKey:@"user"];
Expand Down Expand Up @@ -204,39 +199,4 @@ - (void)postToHost {
}
}

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error usingController:(UIViewController *)viewController {
NSLog(@"CommunicationHelper.finishedWithAuth called with auth = %@ and error = %@", auth, error);
if (error != NULL) {
NSLog(@"Got error %@ while authenticating", error);
self.mCompletionHandler(NULL, NULL, error);
// modify some kind of error count and notify that user needs to sign in again
} else {
[[AuthCompletionHandler sharedInstance] unregisterFinishDelegate:self];
[self postToHost];
}
}

- (void)finishRefreshSelector:(GTMOAuth2Authentication *)auth
request:(NSMutableURLRequest *)request
finishedWithError:(NSError *)error {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"CommunicationHelper.finishRefreshSelector called with auth = %@, request = %@, error = %@", auth, request, error] showUI:FALSE];

if (error != NULL) {
self.mCompletionHandler(NULL, NULL, error);
} else {
BOOL stillExpired = ([auth.expirationDate compare:[NSDate date]] == NSOrderedAscending);
if (stillExpired) {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"No more methods to try, I GIVE UP!"] showUI:TRUE];
} else {
// Check to see whether the sharedInstance auth token has been updated
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Auth token in the shared instance is %@, posting to host", [AuthCompletionHandler sharedInstance].currAuth] showUI:FALSE];
[self postToHost];
}
}

}

@end

0 comments on commit 7d8a03c

Please sign in to comment.