GIDSignInCallbackSchemes.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/GIDSignInCallbackSchemes.h"
  15. NS_ASSUME_NONNULL_BEGIN
  16. @implementation GIDSignInCallbackSchemes {
  17. NSString *_clientIdentifier;
  18. }
  19. /**
  20. * @fn relevantURLSchemes
  21. * @brief Extracts CFBundleURLSchemes from the host app's info.plist.
  22. * @return An array of lowercase NSString *'s representing the URL schemes the host app has declared
  23. * support for.
  24. * @remarks Branched from google3/googlemac/iPhone/Firebase/Source/GGLBundleUtil.m
  25. */
  26. + (NSArray *)relevantURLSchemes {
  27. NSMutableArray *result = [NSMutableArray array];
  28. NSBundle *bundle = [NSBundle mainBundle];
  29. NSArray *urlTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  30. for (NSDictionary *urlType in urlTypes) {
  31. NSArray *urlTypeSchemes = urlType[@"CFBundleURLSchemes"];
  32. for (NSString *urlTypeScheme in urlTypeSchemes) {
  33. [result addObject:urlTypeScheme.lowercaseString];
  34. }
  35. }
  36. return result;
  37. }
  38. - (instancetype)initWithClientIdentifier:(NSString *)clientIdentifier {
  39. self = [super init];
  40. if (self) {
  41. _clientIdentifier = [clientIdentifier copy];
  42. }
  43. return self;
  44. }
  45. - (NSString *)clientIdentifierScheme {
  46. NSArray *clientIdentifierParts = [_clientIdentifier componentsSeparatedByString:@"."];
  47. NSString *reversedClientIdentifier =
  48. [[clientIdentifierParts reverseObjectEnumerator].allObjects componentsJoinedByString:@"."];
  49. return reversedClientIdentifier.lowercaseString;
  50. }
  51. - (NSArray *)allSchemes {
  52. NSMutableArray *schemes = [NSMutableArray array];
  53. NSString *clientIdentifierScheme = [self clientIdentifierScheme];
  54. if (clientIdentifierScheme) {
  55. [schemes addObject:clientIdentifierScheme];
  56. }
  57. return schemes;
  58. }
  59. - (NSMutableArray *)unsupportedSchemes {
  60. NSMutableArray *unsupportedSchemes = [NSMutableArray arrayWithArray:[self allSchemes]];
  61. NSArray *supportedSchemes = [[self class] relevantURLSchemes];
  62. [unsupportedSchemes removeObjectsInArray:supportedSchemes];
  63. return unsupportedSchemes;
  64. }
  65. - (BOOL)URLSchemeIsCallbackScheme:(NSURL *)URL {
  66. NSString *incomingURLScheme = URL.scheme.lowercaseString;
  67. for (NSString *scheme in [self allSchemes]) {
  68. if ([incomingURLScheme isEqual:scheme]) {
  69. return YES;
  70. }
  71. }
  72. return NO;
  73. }
  74. @end
  75. NS_ASSUME_NONNULL_END