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 2 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
4 changes: 2 additions & 2 deletions GoogleSignIn/Sources/GIDRestrictedScopesRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@

/// Retrieves a dictionary mapping restricted scopes to their handling classes within a given set of scopes.
///
/// @param scopes A set of scopes.
/// @param scopes A set of scopes to lookup their handling class.
/// @return A dictionary where restricted scopes found in the input set are mapped to their corresponding handling classes.
/// If no restricted scopes are found, an empty dictionary is returned.
- (NSDictionary<NSString *, Class> *)restrictedScopeToClassMappingInSet:(NSSet<NSString *> *)scopes;
- (NSDictionary<NSString *, Class> *)restrictedScopesToClassMappingInSet:(NSSet<NSString *> *)scopes;

@end

Expand Down
2 changes: 1 addition & 1 deletion GoogleSignIn/Sources/GIDRestrictedScopesRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (BOOL)isScopeRestricted:(NSString *)scope {
return [self.restrictedScopes containsObject:scope];
}

- (NSDictionary<NSString *, Class> *)restrictedScopeToClassMappingInSet:(NSSet<NSString *> *)scopes {
- (NSDictionary<NSString *, Class> *)restrictedScopesToClassMappingInSet:(NSSet<NSString *> *)scopes {
NSMutableDictionary<NSString *, Class> *mapping = [NSMutableDictionary dictionary];
for (NSString *scope in scopes) {
if ([self isScopeRestricted:scope]) {
Expand Down
15 changes: 9 additions & 6 deletions GoogleSignIn/Sources/GIDSignIn.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ @implementation GIDSignIn {
GIDTimedLoader *_timedLoader;
// Flag indicating developer's intent to use App Check.
BOOL _configureAppCheckCalled;
// The class used to manage restricted scopes and their associated handling classes.
GIDRestrictedScopesRegistry *_registry;
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
}

Expand Down Expand Up @@ -998,18 +1000,19 @@ - (void)assertValidPresentingViewController {

#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
- (void)assertValidScopes:(NSArray<NSString *> *)scopes {
GIDRestrictedScopesRegistry *registry = [[GIDRestrictedScopesRegistry alloc] init];
NSDictionary<NSString *, Class> *restrictedScopesMapping = [registry restrictedScopeToClassMappingInSet:[NSSet setWithArray:scopes]];
_registry = [[GIDRestrictedScopesRegistry alloc] init];
brnnmrls marked this conversation as resolved.
Show resolved Hide resolved
NSDictionary<NSString *, Class> *restrictedScopesMapping =
[_registry restrictedScopesToClassMappingInSet:[NSSet setWithArray:scopes]];

if (restrictedScopesMapping.count > 0) {
NSMutableString *errorMessage =
[NSMutableString stringWithString:@"The following scopes are not supported in the 'addScopes' flow. "
"Please use the appropriate classes to handle these:\n"];
for (NSString *restrictedScope in restrictedScopesMapping) {
Class handlingClass = restrictedScopesMapping[restrictedScope];
[restrictedScopesMapping enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull restrictedScope,
Class _Nonnull handlingClass,
BOOL * _Nonnull stop) {
[errorMessage appendFormat:@"%@ -> %@\n", restrictedScope, NSStringFromClass(handlingClass)];
}

}];
// NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
[NSException raise:NSInvalidArgumentException
format:@"%@", errorMessage];
Expand Down
22 changes: 12 additions & 10 deletions GoogleSignIn/Tests/Unit/GIDRestrictedScopesRegistryTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ @interface GIDRestrictedScopesRegistryTest : XCTestCase
@end

@implementation GIDRestrictedScopesRegistryTest

- (void)testIsScopeRestricted {
GIDRestrictedScopesRegistry *registry = [[GIDRestrictedScopesRegistry alloc] init];
BOOL isRestricted = [registry isScopeRestricted:kAccountDetailTypeAgeOver18Scope];
XCTAssertTrue(isRestricted);
GIDRestrictedScopesRegistry *registry = [[GIDRestrictedScopesRegistry alloc] init];
BOOL isRestricted = [registry isScopeRestricted:kAccountDetailTypeAgeOver18Scope];
XCTAssertTrue(isRestricted);
}

- (void)testRestrictedScopeToClassMappingInSet {
GIDRestrictedScopesRegistry *registry = [[GIDRestrictedScopesRegistry alloc] init];
NSSet<NSString *> *scopes = [NSSet setWithObjects:kAccountDetailTypeAgeOver18Scope, @"some_other_scope", nil];
NSDictionary<NSString *, Class> *mapping = [registry restrictedScopeToClassMappingInSet:scopes];

XCTAssertEqual(mapping.count, 1);
XCTAssertEqualObjects(mapping[kAccountDetailTypeAgeOver18Scope], [GIDVerifyAccountDetail class]);
- (void)testRestrictedScopesToClassMappingInSet {
GIDRestrictedScopesRegistry *registry = [[GIDRestrictedScopesRegistry alloc] init];
NSSet<NSString *> *scopes = [NSSet setWithObjects:kAccountDetailTypeAgeOver18Scope, @"some_other_scope", nil];
NSDictionary<NSString *, Class> *mapping = [registry restrictedScopesToClassMappingInSet:scopes];

XCTAssertEqual(mapping.count, 1);
XCTAssertEqualObjects(mapping[kAccountDetailTypeAgeOver18Scope], [GIDVerifyAccountDetail class]);
XCTAssertNil(mapping[@"some_other_scope"]);
}

@end
Expand Down
Loading