Skip to content

Custom scopes for different requirements (Android) #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,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 @@ -79,6 +71,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 @@ -146,6 +141,31 @@ public void signOut(final PluginCall call) {

@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