GIDSignInCallbackSchemes.m 3.2 KB

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