Skip to content
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

Throw an exception if verified age scope is passed in sign-in request through the add scopes flow. #473

Merged
merged 13 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
32 changes: 32 additions & 0 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInResult.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiableAccountDetail.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifyAccountDetail.h"

#import "GoogleSignIn/Sources/GIDAuthorizationResponse/GIDAuthorizationResponseHelper.h"
#import "GoogleSignIn/Sources/GIDAuthorizationResponse/Implementations/GIDAuthorizationResponseHandler.h"
Expand Down Expand Up @@ -256,6 +258,10 @@ - (void)addScopes:(NSArray<NSString *> *)scopes
loginHint:self.currentUser.profile.email
addScopesFlow:YES
completion:completion];
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
// Explicitly throw an exception for invalid or restricted scopes in the request.
[self assertValidScopes:scopes];
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST

NSSet<NSString *> *requestedScopes = [NSSet setWithArray:scopes];
NSMutableSet<NSString *> *grantedScopes =
Expand Down Expand Up @@ -989,6 +995,32 @@ - (void)assertValidPresentingViewController {
}
}

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
// Asserts the requested scopes are valid.
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
- (void)assertValidScopes:(NSArray<NSString *> *)scopes {
NSDictionary *scopeToClassMapping = @{
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
kAccountDetailTypeAgeOver18Scope : [GIDVerifyAccountDetail class],
};
NSMutableString *errorMessage =
[NSMutableString stringWithString:@"The following scopes are not supported in the 'addScopes' flow. "
"Please use the appropriate classes to handle these:\n"];
Boolean unsupportedScopeFound = NO;

for (NSString *scope in scopes) {
Class scopeClass = scopeToClassMapping[scope];
if (scopeClass) {
unsupportedScopeFound = YES;
[errorMessage appendFormat:@"%@ -> %@\n", scope, NSStringFromClass(scopeClass)];
}
}
if (unsupportedScopeFound) {
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"%@", errorMessage];
}
}
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST

// Checks whether or not this is the first time the app runs.
- (BOOL)isFreshInstall {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
Expand Down
23 changes: 23 additions & 0 deletions GoogleSignIn/Tests/Unit/GIDSignInTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,29 @@ - (void)testTokenEndpointEMMError {
XCTAssertEqualObjects(_authError.domain, kGIDSignInErrorDomain);
XCTAssertEqual(_authError.code, kGIDSignInErrorCodeEMM);
XCTAssertNil(_signIn.currentUser, @"should not have current user");
}

- (void)testValidScopesException {
NSString *requestedScope = @"https://www.googleapis.com/auth/verified.age.over18.standard";
NSString *expectedException =
[NSString stringWithFormat:@"The following scopes are not supported in the 'addScopes' flow. "
"Please use the appropriate classes to handle these:\n%@ -> %@\n",
requestedScope, NSStringFromClass([GIDVerifyAccountDetail class])];
BOOL threw = NO;
@try {
[_signIn addScopes:@[requestedScope]
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
presentingViewController:_presentingViewController
#elif TARGET_OS_OSX
presentingWindow:_presentingWindow
#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
completion:_completion];
} @catch (NSException *exception) {
threw = YES;
XCTAssertEqualObjects(exception.description, expectedException);
} @finally {
}
XCTAssert(threw);

// TODO: Keep mocks from carrying forward to subsequent tests. (#410)
[_authState stopMocking];
Expand Down
Loading