GIDScopes.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/GIDScopes.h"
  15. NS_ASSUME_NONNULL_BEGIN
  16. static NSString *const kEmailScope = @"email";
  17. static NSString *const kOldEmailScope = @"https://www.googleapis.com/auth/userinfo.email";
  18. static NSString *const kProfileScope = @"profile";
  19. static NSString *const kOldProfileScope = @"https://www.googleapis.com/auth/userinfo.profile";
  20. static BOOL hasProfile(NSString *scope) {
  21. return [scope isEqualToString:kProfileScope] || [scope isEqualToString:kOldProfileScope];
  22. }
  23. static BOOL hasEmail(NSString *scope) {
  24. return [scope isEqualToString:kEmailScope] || [scope isEqualToString:kOldEmailScope];
  25. }
  26. // Checks whether |scopes| contains or implies a particular scope, using
  27. // |hasScope| as the predicate.
  28. static BOOL hasScopeInArray(NSArray *scopes, BOOL (*hasScope)(NSString *)) {
  29. for (NSString *scope in scopes) {
  30. if (hasScope(scope)) {
  31. return YES;
  32. }
  33. }
  34. return NO;
  35. }
  36. // Adds |scopeToAdd| to |originalScopes| if it is not already contained
  37. // or implied, using |hasScope| as the predicate.
  38. static NSArray *addScopeTo(NSArray *originalScopes,
  39. BOOL (*hasScope)(NSString *),
  40. NSString *scopeToAdd) {
  41. if (hasScopeInArray(originalScopes, hasScope)) {
  42. return originalScopes;
  43. }
  44. NSMutableArray *result = [NSMutableArray arrayWithArray:originalScopes];
  45. [result addObject:scopeToAdd];
  46. return result;
  47. }
  48. @implementation GIDScopes
  49. + (NSArray *)scopesWithBasicProfile:(NSArray *)scopes {
  50. scopes = addScopeTo(scopes, hasEmail, kEmailScope);
  51. return addScopeTo(scopes, hasProfile, kProfileScope);
  52. }
  53. @end
  54. NS_ASSUME_NONNULL_END