GIDSignInCallbackSchemes.m 2.8 KB

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