Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove listMissingScopes from implementations.
  • Loading branch information
emerssso committed Mar 19, 2020
commit d77cb0317bee428455cd46761a0e57dff512fd69
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,8 +47,8 @@ public class GoogleSignInPlugin implements MethodCallHandler {
private static final String METHOD_DISCONNECT = "disconnect";
private static final String METHOD_IS_SIGNED_IN = "isSignedIn";
private static final String METHOD_CLEAR_AUTH_CACHE = "clearAuthCache";
private static final String METHOD_HAS_GRANTED_SCOPE = "hasGrantedScope";
private static final String METHOD_REQUEST_SCOPE = "requestScope";
private static final String METHOD_LIST_MISSING_SCOPES = "listMissingScopes";
private static final String METHOD_REQUEST_SCOPES = "requestScopes";

private final IDelegate delegate;

Expand Down Expand Up @@ -102,13 +103,8 @@ public void onMethodCall(MethodCall call, Result result) {
delegate.isSignedIn(result);
break;

case METHOD_HAS_GRANTED_SCOPE:
String scope = call.argument("scope");
delegate.hasGrantedScope(result, scope);
break;

case METHOD_REQUEST_SCOPE:
List<String> scopes = call.argument("scope");
case METHOD_REQUEST_SCOPES:
List<String> scopes = call.argument("scopes");
delegate.requestScopes(result, scopes);
break;

Expand Down Expand Up @@ -166,9 +162,6 @@ public void init(
/** Checks if there is a signed in user. */
public void isSignedIn(Result result);

/** Checks to see if the passed Oauth scope has been granted by the user. */
public void hasGrantedScope(final Result result, final String scope);

/** Prompts the user to grant an additional Oauth scopes. */
public void requestScopes(final Result result, final List<String> scopes);
}
Expand Down Expand Up @@ -362,25 +355,33 @@ public void isSignedIn(final Result result) {
result.success(value);
}

@Override
public void hasGrantedScope(Result result, String scope) {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(registrar.context());
boolean value = account != null && GoogleSignIn.hasPermissions(account, new Scope(scope));
result.success(value);
}

@Override
public void requestScopes(Result result, List<String> scopes) {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(registrar.context());
if (account != null) {
Scope[] wrappedScopes = new Scope[scopes.size()];
for (int i = 0; i < scopes.size(); i++) {
wrappedScopes[i] = new Scope(scopes.get(i));
if (account == null) {
result.error(ERROR_REASON_SIGN_IN_REQUIRED, "No account to grant scopes.", null);
return;
}

List<Scope> wrappedScopes = new ArrayList<>();

for (String scope : scopes) {
Scope wrappedScope = new Scope(scope);
if (!GoogleSignIn.hasPermissions(account, wrappedScope)) {
wrappedScopes.add(wrappedScope);
}
}

GoogleSignIn.requestPermissions(
registrar.activity(), REQUEST_CODE_REQUEST_SCOPE, account, wrappedScopes);
if (wrappedScopes.isEmpty()) {
result.success(true);
return;
}

GoogleSignIn.requestPermissions(
registrar.activity(),
REQUEST_CODE_REQUEST_SCOPE,
account,
wrappedScopes.toArray(new Scope[0]));
}

private void onSignInResult(Task<GoogleSignInAccount> completedTask) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
// There's nothing to be done here on iOS since the expired/invalid
// tokens are refreshed automatically by getTokensWithHandler.
result(nil);
} else if ([call.method isEqualToString:@"hasGrantedScope"]) {
GIDGoogleUser *user = [GIDSignIn sharedInstance].currentUser;
NSString *scope = call.arguments[@"scope"];
bool success = [user.grantedScopes containsObject:scope];
result(@(success));
} else if ([call.method isEqualToString:@"requestScopes"]) {
NSArray *currentScopes = [GIDSignIn sharedInstance].scopes;
NSArray *scope = call.arguments[@"scopes"];
[GIDSignIn sharedInstance].scopes = [currentScopes arrayByAddingObjectsFromArray:scopes];
NSArray *scopes = call.arguments[@"scopes"];
NSArray *missingScopes = [scopes
filteredArrayUsingPredicate:[NSPredicate
predicateWithBlock:^BOOL(id scope, NSDictionary *bindings) {
return ![user.grantedScopes containsObject:scope];
}]];
[GIDSignIn sharedInstance].scopes = [currentScopes arrayByAddingObjectsFromArray:missingScopes];
[[GIDSignIn sharedInstance] signIn];
} else {
result(FlutterMethodNotImplemented);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,7 @@ class GoogleSignIn {
Future<GoogleSignInAccount> disconnect() =>
_addMethodCall(GoogleSignInPlatform.instance.disconnect);

/// Indicates whether this Oauth [scope] has been granted.
Future<bool> hasGrantedScope(String scope) async {
await _ensureInitialized();
return GoogleSignInPlatform.instance.hasGrantedScope(scope);
}

/// Requests the user grant an additional Oauth [scope].
/// Requests the user grants additional Oauth [scopes].
Future<bool> requestScopes(List<String> scopes) async {
await _ensureInitialized();
return GoogleSignInPlatform.instance.requestScopes(scopes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void main() {
'signOut': null,
'disconnect': null,
'isSignedIn': true,
'hasGrantedScope': true,
'requestScope': true,
'requestScopes': true,
'getTokens': <dynamic, dynamic>{
'idToken': '123',
'accessToken': '456',
Expand Down Expand Up @@ -382,28 +381,7 @@ void main() {
);
});

test('hasGrantedScope returns true if scope is granted', () async {
await googleSignIn.signIn();
final result = await googleSignIn.hasGrantedScope('testScope');

expect(result, isTrue);
expect(
log,
<Matcher>[
isMethodCall('init', arguments: <String, dynamic>{
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
}),
isMethodCall('signIn', arguments: null),
isMethodCall('hasGrantedScope', arguments: <String, dynamic>{
'scope': 'testScope',
}),
],
);
});

test('requestScope returns true once new scope is granted', () async {
test('requestScopes returns true once new scope is granted', () async {
await googleSignIn.signIn();
final result = await googleSignIn.requestScopes(['testScope']);

Expand All @@ -417,7 +395,7 @@ void main() {
'hostedDomain': null,
}),
isMethodCall('signIn', arguments: null),
isMethodCall('requestScope', arguments: <String, dynamic>{
isMethodCall('requestScopes', arguments: <String, dynamic>{
'scopes': ['testScope'],
}),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,21 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
}

@override
Future<bool> hasGrantedScope(String scope) async {
Future<bool> requestScopes(List<String> scopes) async {
await initialized;

return auth2
.getAuthInstance()
?.currentUser
?.get()
?.getGrantedScopes()
?.contains(scope) ??
false;
}
final currentUser = auth2.getAuthInstance()?.currentUser?.get();

@override
Future<bool> requestScopes(List<String> scopes) async {
await initialized;
if (currentUser == null) return false;

final grantedScopes = currentUser.getGrantedScopes();
final missingScopes =
scopes.where((scope) => !grantedScopes.contains(scope));

if (missingScopes.isEmpty) return true;

return auth2
.getAuthInstance()
?.currentUser
?.get()
?.grant(auth2.SigninOptions(scope: scopes.join(" "))) ??
return currentUser
.grant(auth2.SigninOptions(scope: missingScopes.join(" "))) ??
false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ void main() {
expect(actualToken, expectedTokenData);
});

test('hasGrantedScope', () async {
bool hasScope = await plugin.hasGrantedScope('scope');

expect(hasScope, isTrue);
});

test('requestScope', () async {
test('requestScopes', () async {
bool scopeGranted = await plugin.requestScopes(['newScope']);

expect(scopeGranted, isTrue);
Expand Down