Skip to content

Commit

Permalink
Merge pull request #36 from shankari/add_generated_token
Browse files Browse the repository at this point in the history
Add an option to save programatically save local tokens
  • Loading branch information
shankari authored Apr 3, 2020
2 parents 3a79d52 + 6f83309 commit fe688ac
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 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.auth"
version="1.5.0">
version="1.6.0">

<name>JWTAuth</name>
<description>Get the user email and associated JWT tokens from both native
Expand Down
11 changes: 11 additions & 0 deletions src/android/JWTAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public void onResult(@NonNull AuthResult authResult) {
}
});
return true;
} else if (action.equals("setPromptedAuthToken")) {
if (tokenCreator.getClass() != PromptedAuth.class) {
callbackContext.error("Attempting to set programmatic token conflicts"
+ "with configured auth method");
}
String email = data.getString(0);
Log.d(cordova.getActivity(),TAG,
"Force setting the prompted auth token = "+email);
UserProfile.getInstance(cordova.getActivity()).setUserEmail(email);
callbackContext.success(email);
return true;
} else {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/android/PromptedAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AuthPendingResult uiSignIn(CordovaPlugin plugin) {
this.mAuthPending = new AuthPendingResult();
this.mPlugin = plugin;

final String devJSScript = "window.cordova.plugins.BEMJWTAuth.launchDevAuth('"+getPrompt()+"')";
final String devJSScript = "window.cordova.plugins.BEMJWTAuth.launchPromptedAuth('"+getPrompt()+"')";
Log.d(mCtxt, TAG, "About to execute script: "+devJSScript);
final CordovaPlugin finalPlugin = plugin;
plugin.cordova.getActivity().runOnUiThread(new Runnable() {
Expand Down
28 changes: 27 additions & 1 deletion src/ios/BEMJWTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "AuthTokenCreationFactory.h"
#import "AuthTokenCreator.h"
#import "BEMBuiltinUserCache.h"
#import "PromptedAuth.h"

@interface BEMJWTAuth ()
@property (nonatomic, retain) CDVInvokedUrlCommand* command;
Expand Down Expand Up @@ -102,6 +103,32 @@ - (void)getJWT:(CDVInvokedUrlCommand*)command
}
}

- (void)setPromptedAuthToken:(CDVInvokedUrlCommand*)command
{
@try {
_token_creator = [AuthTokenCreationFactory getInstance];
if ([_token_creator class] != [PromptedAuth class]) {
[self getCallbackForCommand:command]
(NULL, [NSError errorWithDomain:
@"Setting programmatic token conflicts with configured auth method"
code:100 userInfo:NULL]);
} else {
NSString* email = [[command arguments] objectAtIndex:0];
[PromptedAuth setStoredUserAuthEntry:email];
[self getCallbackForCommand:command](email, NULL);
}
}
@catch (NSException *exception) {
NSString* msg = [NSString stringWithFormat: @"While setting programmatic token, error %@", exception];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString:msg];
[self.commandDelegate sendPluginResult:result callbackId:[command callbackId]];
}
}




-(AuthResultCallback) getCallbackForCommand:(CDVInvokedUrlCommand*)command
{
return ^(NSString *resultStr, NSError *error) {
Expand All @@ -120,7 +147,6 @@ -(AuthResultCallback) getCallbackForCommand:(CDVInvokedUrlCommand*)command
};
}


- (void)applicationLaunchedWithUrl:(NSNotification*)notification
{
// Make this consistent with android to fix crashes if the user clicks on a URL
Expand Down
1 change: 1 addition & 0 deletions src/ios/PromptedAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

@interface PromptedAuth : NSObject <AuthTokenCreator>
+ (PromptedAuth*) sharedInstance;
+ (void) setStoredUserAuthEntry: (NSString*)token;
@end
9 changes: 7 additions & 2 deletions src/ios/PromptedAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ -(void)handleNotification:(NSNotification *)notification
// For the prompted-auth method name
if ([tokenParam.name isEqualToString:TOKEN_PARAM_KEY]) {
NSString* userName = tokenParam.value;
[PromptedAuth setStoredUserAuthEntry:userName];
[LocalNotificationManager addNotification:
[NSString stringWithFormat:@"in handleNotification, received userName %@",
userName]];
[[NSUserDefaults standardUserDefaults] setObject:userName forKey:STORAGE_KEY];
self.mResultCallback(userName, NULL);
} else {
[LocalNotificationManager addNotification:
Expand All @@ -85,6 +85,11 @@ - (NSString*) getStoredUserAuthEntry
return [[NSUserDefaults standardUserDefaults] objectForKey:STORAGE_KEY];
}

+ (void) setStoredUserAuthEntry: (NSString*)token
{
[[NSUserDefaults standardUserDefaults] setObject:token forKey:STORAGE_KEY];
}

- (void) getEmail:(AuthResultCallback) authResultCallback
{
authResultCallback([self getStoredUserAuthEntry], NULL);
Expand All @@ -104,7 +109,7 @@ - (void) getExpirationDate:(AuthResultCallback) authResultCallback
- (void) uiSignIn:(AuthResultCallback)authResultCallback withPlugin:(CDVPlugin *)plugin
{
self.mResultCallback = authResultCallback;
NSString* devJSScript = [NSString stringWithFormat:@"window.cordova.plugins.BEMJWTAuth.launchDevAuth('%@')", self.prompt];
NSString* devJSScript = [NSString stringWithFormat:@"window.cordova.plugins.BEMJWTAuth.launchPromptedAuth('%@')", self.prompt];
[LocalNotificationManager addNotification:@"About to execute script"];
[LocalNotificationManager addNotification:devJSScript];
[plugin.commandDelegate evalJs:devJSScript];
Expand Down
15 changes: 14 additions & 1 deletion www/jwtauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ var JWTAuth = {
});
},

launchDevAuth: function(promptMsg) {
/*
* Although we generally support auth through callbacks, this is special
* because we may want to call it from the client to programatically create
* a string or token and we don't want to jump through in app browser hoops
* to do so.
*/

setPromptedAuthToken(email) {
return new Promise(function(resolve, reject) {
exec(resolve, reject, "JWTAuth", "setPromptedAuthToken", [email]);
});
},

launchPromptedAuth: function(promptMsg) {
var email = window.prompt(promptMsg, '');
// window.alert('email = '+email);
var callbackURL = 'emission://auth?method=prompted-auth&token='+email;
Expand Down

0 comments on commit fe688ac

Please sign in to comment.