Skip to content

Commit

Permalink
Merge pull request #45 from shankari/native_upgrade_2021
Browse files Browse the repository at this point in the history
Native code updates (c) 2021
  • Loading branch information
shankari authored Oct 24, 2021
2 parents 1c5f341 + 52205fe commit 51c51da
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
10 changes: 9 additions & 1 deletion README.openid.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Cordova JWT Auth OpenID support
---

🚧 This functionality is currently untested in the most recent version (1.6.5) since I don't have a server to test against.
If you use OpenID Auth and find errors, please contact me with information about your server so I can fix it 🚧

🗑️ If this functionality is not currently used, I might deprecate it in the
next release and remove in 2 releases later. So if you do use this
functionality, please speak up! 🗑️

The plugin currently supports login with generic OpenID providers, while a few additional configuration needed to be
made in order for the plugin to function.

Expand Down Expand Up @@ -47,4 +55,4 @@ Developer's Note
---
1. For Android, `android support` conflict found in development. The current workaround is to force all android support
version to be the same. If some library needs higher android support version in the future, please bump up the version
in `src/android/openid-config.gradle`
in `src/android/openid-config.gradle`
25 changes: 13 additions & 12 deletions 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="cordova-plugin-em-jwt-auth"
version="1.6.4">
version="1.6.5">

<name>JWTAuth</name>
<description>Get the user email and associated JWT tokens from both native
Expand All @@ -14,6 +14,7 @@
<engines>
<engine name="cordova" version=">=3.6.0"/>
<engine name="cordova-android" version=">=7.0.0"/>
<engine name="cordova-ios" version=">=5.0.0"/>
</engines>

<asset src="www/jwtauth.js" target="js/jwtauth.js"/>
Expand All @@ -39,10 +40,10 @@
</config-file>

<framework src="com.google.android.gms:play-services-auth:$AUTH_VERSION"/>
<preference name="AUTH_VERSION" default="11.0.1"/>
<preference name="AUTH_VERSION" default="19.2.0"/>

<!-- Configuration of OpenID auth plugin start -->
<framework src="net.openid:appauth:0.7.0"/>
<framework src="net.openid:appauth:0.10.0"/>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="net.openid.appauth.RedirectUriReceiverActivity">
<intent-filter>
Expand All @@ -53,7 +54,7 @@
</intent-filter>
</activity>
</config-file>
<framework src="com.auth0.android:jwtdecode:1.1.1"/>
<framework src="com.auth0.android:jwtdecode:2.0.0"/>
<framework src="src/android/openid-config.gradle" custom="true" type="gradleReference" />
<!-- Configuration of OpenID auth plugin end -->

Expand Down Expand Up @@ -93,16 +94,16 @@

<framework src="SystemConfiguration.framework"/>
<framework src="Security.framework"/>
<!--
<podspec>
<pod name="GoogleSignIn" spec="~> 5.0.0" />
<pod name="AppAuth" spec="~> 1.2" />
<pod name="JWT" spec="~> 3.0.0-beta.12" />
<config>
<source url="https://cdn.cocoapods.org/"/>
</config>
<pods use-frameworks="true">
<pod name="GoogleSignIn" spec="~> 6.0.2" />
<pod name="AppAuth" spec="~> 1.4.0" />
<pod name="JWT" spec="~> 3.0.0-beta.14" />
</pods>
</podspec>
-->
<framework src="GoogleSignIn" type="podspec" spec="~> 5.0.0"/>
<framework src="AppAuth" type="podspec" spec="~> 1.2"/>
<framework src="JWT" type="podspec" spec="~> 3.0.0-beta.12"/>

<header-file src="src/ios/BEMJWTAuth.h"/>
<header-file src="src/ios/GoogleSigninAuth.h"/>
Expand Down
59 changes: 17 additions & 42 deletions src/ios/GoogleSigninAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
#import "BEMConstants.h"
#import "LocalNotificationManager.h"
#import <Cordova/CDV.h>
#import <GoogleSignIn/GoogleSignIn.h>
@import GoogleSignIn;


typedef void (^GoogleSigninCallback)(GIDGoogleUser *,NSError*);
typedef NSString* (^ProfileRetValue)(GIDGoogleUser *);

#define NOT_SIGNED_IN_CODE 1000

@interface GoogleSigninAuth () <GIDSignInDelegate>
@interface GoogleSigninAuth ()
@property (atomic, retain) CDVPlugin* mPlugin;
@property (atomic, retain) GIDConfiguration* signInConfig;
@end

@implementation GoogleSigninAuth
Expand All @@ -40,13 +41,14 @@ + (GoogleSigninAuth*)sharedInstance
NSLog(@"creating new GoogleSigninAuth sharedInstance");
sharedInstance = [GoogleSigninAuth new];

GIDSignIn* signIn = [GIDSignIn sharedInstance];
signIn.clientID = [[ConnectionSettings sharedInstance] authValueForKey:@"clientID"];
NSString* iOSClientID = [[ConnectionSettings sharedInstance] authValueForKey:@"clientID"];
sharedInstance.signInConfig = [[GIDConfiguration alloc] initWithClientID:iOSClientID];


// client secret is no longer required for this client
// signIn.serverClientID = [[ConnectionSettings sharedInstance] getGoogleiOSClientSecret];
signIn.delegate = sharedInstance;
[LocalNotificationManager addNotification:[NSString stringWithFormat:@"Finished setting clientId = %@ and serverClientID = %@", signIn.clientID, signIn.serverClientID]];
[LocalNotificationManager addNotification:[NSString stringWithFormat:@"Finished setting delegate = %@", signIn.delegate]];
// signIn.delegate = sharedInstance;
[LocalNotificationManager addNotification:[NSString stringWithFormat:@"Finished setting clientId = %@", iOSClientID]];
}
return sharedInstance;
}
Expand All @@ -59,38 +61,9 @@ + (GoogleSigninAuth*)sharedInstance

- (void) getValidAuth:(GoogleSigninCallback) authCompletionCallback
{
[self registerCallback:authCompletionCallback];
[[GIDSignIn sharedInstance] restorePreviousSignIn];
}

- (void) registerCallback:(GoogleSigninCallback)authCompletionCallback
{
// pattern from `addObserverForName` docs
// https://developer.apple.com/reference/foundation/nsnotificationcenter/1411723-addobserverforname
NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [[NSNotificationCenter defaultCenter] addObserverForName:BEMJWTAuthComplete
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
if([note.userInfo[STATUS_KEY] isEqual:@YES]) {
authCompletionCallback(note.object, NULL);
} else {
authCompletionCallback(NULL, note.object);
}
[center removeObserver:token];
}];
}

-(void)signIn:(GIDSignIn*)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error
{
if (error == NULL) {
[[NSNotificationCenter defaultCenter] postNotificationName:BEMJWTAuthComplete
object:user userInfo:@{STATUS_KEY: @YES}];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:BEMJWTAuthComplete
object:error userInfo:@{STATUS_KEY: @NO}];
}
[[GIDSignIn sharedInstance] restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user, NSError * _Nullable error) {
authCompletionCallback(user, error);
}];
}

-(void)handleNotification:(NSNotification *)notification
Expand Down Expand Up @@ -150,11 +123,13 @@ -(GoogleSigninCallback) getRedirectedCallback:(AuthResultCallback)redirCallback
- (void) uiSignIn:(AuthResultCallback)authResultCallback withPlugin:(CDVPlugin*) plugin
{
self.mPlugin = plugin;
[GIDSignIn sharedInstance].presentingViewController = self.mPlugin.viewController;
[self registerCallback:[self getRedirectedCallback:authResultCallback
GoogleSigninCallback currCallback = [self getRedirectedCallback:authResultCallback
withRetValue:^NSString *(GIDGoogleUser *user) {
return user.profile.email;
}]];
}];
[GIDSignIn.sharedInstance signInWithConfiguration:self.signInConfig
presentingViewController:self.mPlugin.viewController
callback:currCallback];
}
// END: UI interaction

Expand Down
4 changes: 2 additions & 2 deletions src/ios/OpenIDAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

#import "OpenIDAuth.h"
#import "AppAuth.h"
#import <JWT/JWT.h>
@import AppAuth;
@import JWT;
#import <Cordova/CDV.h>
#import "BEMConnectionSettings.h"
#import "LocalNotificationManager.h"
Expand Down

0 comments on commit 51c51da

Please sign in to comment.