Skip to content

Commit

Permalink
Custom scopes for different requirements (Android and iOS) (#269)
Browse files Browse the repository at this point in the history
* Moved google auth initialize methods from the load function to the initialize function for users to add scopes on the fly

* custom scopes for ios

* update readme

* update readme
  • Loading branch information
AlvinTCH authored May 1, 2024
1 parent f613415 commit f5fa8d9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,15 @@ public class GoogleAuth extends Plugin {

private GoogleSignInClient googleSignInClient;

@Override
public void load() {
String clientId = getConfig().getString("androidClientId",
getConfig().getString("clientId",
this.getContext().getString(R.string.server_client_id)));

boolean forceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false);

public void loadSignInClient (String clientId, boolean forceCodeForRefreshToken, String[] scopeArray) {
GoogleSignInOptions.Builder googleSignInBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(clientId)
.requestEmail();
.requestIdToken(clientId)
.requestEmail();

if (forceCodeForRefreshToken) {
googleSignInBuilder.requestServerAuthCode(clientId, true);
}

String[] scopeArray = getConfig().getArray("scopes", new String[] {});
Scope[] scopes = new Scope[scopeArray.length - 1];
Scope firstScope = new Scope(scopeArray[0]);
for (int i = 1; i < scopeArray.length; i++) {
Expand All @@ -81,6 +73,9 @@ public void load() {
googleSignInClient = GoogleSignIn.getClient(this.getContext(), googleSignInOptions);
}

@Override
public void load() {}

@PluginMethod()
public void signIn(PluginCall call) {
Intent signInIntent = googleSignInClient.getSignInIntent();
Expand Down Expand Up @@ -177,6 +172,31 @@ public void onFailure(Exception e) {

@PluginMethod()
public void initialize(final PluginCall call) {
// get data from config
String configClientId = getConfig().getString("androidClientId",
getConfig().getString("clientId",
this.getContext().getString(R.string.server_client_id)));
boolean configForceCodeForRefreshToken = getConfig().getBoolean("forceCodeForRefreshToken", false);
// need to get this as string so as to standardize with data from plugin call
String configScopeArray = getConfig().getString("scopes", new String());

// get client id from plugin call, fallback to be client id from config
String clientId = call.getData().getString("clientId", configClientId);
// get forceCodeForRefreshToken from call, fallback to be from config
boolean forceCodeForRefreshToken = call.getData().getBoolean("grantOfflineAccess", configForceCodeForRefreshToken);
// get scopes from call, fallback to be from config
String scopesStr = call.getData().getString("scopes", configScopeArray);
// replace all the symbols from parsing array as string
// leaving only scopes delimited by commas
String replacedScopesStr = scopesStr
.replaceAll("[\"\\[\\] ]", "")
// this is for scopes that are in the form of a url
.replace("\\", "");

// scope to be in the form of an array
String[] scopeArray = replacedScopesStr.split(",");

loadSignInClient(clientId, forceCodeForRefreshToken, scopeArray);
call.resolve();
}

Expand Down
44 changes: 33 additions & 11 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,56 @@ public class GoogleAuth: CAPPlugin {
var forceAuthCode: Bool = false;
var additionalScopes: [String]!;


public override func load() {
func loadSignInClient (
customClientId: String,
customScopes: [String]
) {
googleSignIn = GIDSignIn.sharedInstance;

let serverClientId = getServerClientIdValue();

guard let clientId = getClientIdValue() else {
NSLog("no client id found in config")
return;
}

googleSignInConfiguration = GIDConfiguration.init(clientID: clientId, serverClientID: serverClientId)
googleSignInConfiguration = GIDConfiguration.init(clientID: customClientId, serverClientID: serverClientId)

// these are scopes granted by default by the signIn method
let defaultGrantedScopes = ["email", "profile", "openid"];

// these are scopes we will need to request after sign in
additionalScopes = (getConfig().getArray("scopes") as? [String] ?? []).filter {
additionalScopes = customScopes.filter {
return !defaultGrantedScopes.contains($0);
};

forceAuthCode = getConfig().getBoolean("forceCodeForRefreshToken", false)

NotificationCenter.default.addObserver(self, selector: #selector(handleOpenUrl(_ :)), name: Notification.Name(Notification.Name.capacitorOpenURL.rawValue), object: nil);
}


public override func load() {
}

@objc
func initialize(_ call: CAPPluginCall) {
// get client id from initialize, with client id from config file as fallback
guard let clientId = call.getString("clientId") ?? getClientIdValue() as? String else {
NSLog("no client id found in config");
call.resolve();
return;
}

// get scopes from initialize, with scopes from config file as fallback
let customScopes = call.getArray("scopes", String.self) ?? (
getConfigValue("scopes") as? [String] ?? []
);

// get force auth code from initialize, with config from config file as fallback
forceAuthCode = call.getBool("grantOfflineAccess") ?? (
getConfigValue("forceCodeForRefreshToken") as? Bool ?? false
);

// load client
self.loadSignInClient(
customClientId: clientId,
customScopes: customScopes
)
call.resolve();
}

Expand Down

0 comments on commit f5fa8d9

Please sign in to comment.