You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3 KiB

6 years ago
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRAuthWebUtils.h"
  17. #import "FIRAuthBackend.h"
  18. #import "FIRAuthErrorUtils.h"
  19. #import "FIRGetProjectConfigRequest.h"
  20. #import "FIRGetProjectConfigResponse.h"
  21. /** @var kAuthDomainSuffix
  22. @brief The suffix of the auth domain pertiaining to a given Firebase project.
  23. */
  24. static NSString *const kAuthDomainSuffix = @"firebaseapp.com";
  25. @implementation FIRAuthWebUtils
  26. + (NSString *)randomStringWithLength:(NSUInteger)length {
  27. NSMutableString *randomString = [[NSMutableString alloc] init];
  28. for (int i=0; i < length; i++) {
  29. [randomString appendString:
  30. [NSString stringWithFormat:@"%c", 'a' + arc4random_uniform('z' - 'a' + 1)]];
  31. }
  32. return randomString;
  33. }
  34. + (BOOL)isCallbackSchemeRegisteredForCustomURLScheme:(NSString *)URLScheme {
  35. NSString *expectedCustomScheme = [URLScheme lowercaseString];
  36. NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
  37. for (NSDictionary *urlType in urlTypes) {
  38. NSArray *urlTypeSchemes = urlType[@"CFBundleURLSchemes"];
  39. for (NSString *urlTypeScheme in urlTypeSchemes) {
  40. if ([urlTypeScheme.lowercaseString isEqualToString:expectedCustomScheme]) {
  41. return YES;
  42. }
  43. }
  44. }
  45. return NO;
  46. }
  47. + (void)fetchAuthDomainWithRequestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  48. completion:(FIRFetchAuthDomainCallback)completion {
  49. FIRGetProjectConfigRequest *request =
  50. [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration];
  51. [FIRAuthBackend getProjectConfig:request
  52. callback:^(FIRGetProjectConfigResponse *_Nullable response,
  53. NSError *_Nullable error) {
  54. if (error) {
  55. completion(nil, error);
  56. return;
  57. }
  58. NSString *authDomain;
  59. for (NSString *domain in response.authorizedDomains) {
  60. NSInteger index = domain.length - kAuthDomainSuffix.length;
  61. if (index >= 2) {
  62. if ([domain hasSuffix:kAuthDomainSuffix] && domain.length >= kAuthDomainSuffix.length + 2) {
  63. authDomain = domain;
  64. break;
  65. }
  66. }
  67. }
  68. if (!authDomain.length) {
  69. completion(nil, [FIRAuthErrorUtils unexpectedErrorResponseWithDeserializedResponse:response]);
  70. return;
  71. }
  72. completion(authDomain, nil);
  73. }];
  74. }
  75. /** @fn queryItemValue:from:
  76. @brief Utility function to get a value from a NSURLQueryItem array.
  77. @param name The key.
  78. @param queryList The NSURLQueryItem array.
  79. @return The value for the key.
  80. */
  81. + (NSString *)queryItemValue:(NSString *)name from:(NSArray<NSURLQueryItem *> *)queryList {
  82. for (NSURLQueryItem *item in queryList) {
  83. if ([item.name isEqualToString:name]) {
  84. return item.value;
  85. }
  86. }
  87. return nil;
  88. }
  89. @end