Skip to content

Commit

Permalink
fix(authentication): explicitly request Google auth token with provid…
Browse files Browse the repository at this point in the history
…ed scopes on Android (#726)

* Add scopes when requesting access token

* Add changeset

* Fix lint

* Refactor to private function

* Update selfish-steaks-enjoy.md

---------

Co-authored-by: Robin Genz <[email protected]>
  • Loading branch information
nkalupahana and robingenz authored Sep 24, 2024
1 parent 38fd57c commit 5729328
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-steaks-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': patch
---

fix(android): explicitly request Google auth token with provided scopes
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin;
import io.capawesome.capacitorjs.plugins.firebase.authentication.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;

Expand Down Expand Up @@ -63,9 +64,17 @@ public void handleOnActivityResult(@NonNull final PluginCall call, @NonNull Acti
new Thread(
() -> {
String accessToken = null;
List<String> scopes = new ArrayList<>();
scopes.add("oauth2:email");
scopes.addAll(getScopesAsList(call));

try {
accessToken =
GoogleAuthUtil.getToken(mGoogleSignInClient.getApplicationContext(), account.getAccount(), "oauth2:email");
GoogleAuthUtil.getToken(
mGoogleSignInClient.getApplicationContext(),
account.getAccount(),
String.join(" ", scopes)
);
// Clears local cache after every login attempt
// to ensure permissions changes elsewhere are reflected in future tokens
GoogleAuthUtil.clearToken(mGoogleSignInClient.getApplicationContext(), accessToken);
Expand Down Expand Up @@ -105,19 +114,26 @@ private GoogleSignInClient buildGoogleSignInClient(@Nullable PluginCall call) {
.requestEmail();

if (call != null) {
JSArray scopes = call.getArray("scopes");
if (scopes != null) {
try {
List<String> scopeList = scopes.toList();
for (String scope : scopeList) {
googleSignInOptionsBuilder = googleSignInOptionsBuilder.requestScopes(new Scope(scope));
}
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "buildGoogleSignInClient failed.", exception);
}
List<String> scopeList = getScopesAsList(call);
for (String scope : scopeList) {
googleSignInOptionsBuilder = googleSignInOptionsBuilder.requestScopes(new Scope(scope));
}
}

return GoogleSignIn.getClient(pluginImplementation.getPlugin().getActivity(), googleSignInOptionsBuilder.build());
}

private List<String> getScopesAsList(@NonNull PluginCall call) {
List<String> scopeList = new ArrayList<>();
JSArray scopes = call.getArray("scopes");
if (scopes != null) {
try {
scopeList = scopes.toList();
} catch (JSONException exception) {
Log.e(FirebaseAuthenticationPlugin.TAG, "getScopesAsList failed.", exception);
}
}

return scopeList;
}
}

0 comments on commit 5729328

Please sign in to comment.