FIRStorageTokenAuthorizer.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2017 Google
  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 "FirebaseStorage/Sources/FIRStorageTokenAuthorizer.h"
  15. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorage.h"
  16. #import "FirebaseStorage/Sources/Public/FirebaseStorage/FIRStorageConstants.h"
  17. #import "FirebaseStorage/Sources/FIRStorageConstants_Private.h"
  18. #import "FirebaseStorage/Sources/FIRStorageErrors.h"
  19. #import "FirebaseStorage/Sources/FIRStorageLogger.h"
  20. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  21. #import "FirebaseAppCheck/Interop/FIRAppCheckInterop.h"
  22. #import "FirebaseAppCheck/Interop/FIRAppCheckTokenResultInterop.h"
  23. #import "FirebaseAuth/Interop/FIRAuthInterop.h"
  24. static NSString *const kAppCheckTokenHeader = @"X-Firebase-AppCheck";
  25. static NSString *const kAuthHeader = @"Authorization";
  26. @implementation FIRStorageTokenAuthorizer {
  27. @private
  28. /// Google App ID to pass along with each request.
  29. NSString *_googleAppID;
  30. /// Auth provider.
  31. id<FIRAuthInterop> _auth;
  32. id<FIRAppCheckInterop> _appCheck;
  33. }
  34. @synthesize fetcherService = _fetcherService;
  35. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID
  36. fetcherService:(GTMSessionFetcherService *)service
  37. authProvider:(nullable id<FIRAuthInterop>)auth
  38. appCheck:(nullable id<FIRAppCheckInterop>)appCheck {
  39. self = [super init];
  40. if (self) {
  41. _googleAppID = googleAppID;
  42. _fetcherService = service;
  43. _auth = auth;
  44. _appCheck = appCheck;
  45. }
  46. return self;
  47. }
  48. #pragma mark - GTMFetcherAuthorizationProtocol methods
  49. - (void)authorizeRequest:(NSMutableURLRequest *)request
  50. delegate:(id)delegate
  51. didFinishSelector:(SEL)sel {
  52. // Set version header on each request
  53. NSString *versionString = [NSString stringWithFormat:@"ios/%@", FIRFirebaseVersion()];
  54. [request setValue:versionString forHTTPHeaderField:@"x-firebase-storage-version"];
  55. // Set GMP ID on each request
  56. [request setValue:_googleAppID forHTTPHeaderField:@"x-firebase-gmpid"];
  57. if (delegate && sel) {
  58. id selfParam = self;
  59. NSMethodSignature *sig = [delegate methodSignatureForSelector:sel];
  60. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  61. [invocation setSelector:sel];
  62. [invocation setTarget:delegate];
  63. [invocation setArgument:&selfParam atIndex:2];
  64. [invocation setArgument:&request atIndex:3];
  65. dispatch_queue_t callbackQueue = self.fetcherService.callbackQueue;
  66. if (!callbackQueue) {
  67. callbackQueue = dispatch_get_main_queue();
  68. }
  69. [invocation retainArguments];
  70. dispatch_group_t fetchTokenGroup = dispatch_group_create();
  71. if (_auth) {
  72. dispatch_group_enter(fetchTokenGroup);
  73. [_auth getTokenForcingRefresh:NO
  74. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  75. if (error) {
  76. NSMutableDictionary *errorDictionary =
  77. [NSMutableDictionary dictionaryWithDictionary:error.userInfo];
  78. errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
  79. errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
  80. NSError *tokenError = [FIRStorageErrors
  81. errorWithCode:FIRIMPLStorageErrorCodeUnauthenticated
  82. infoDictionary:errorDictionary];
  83. [invocation setArgument:&tokenError atIndex:4];
  84. } else if (token) {
  85. NSString *firebaseToken =
  86. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, token];
  87. [request setValue:firebaseToken forHTTPHeaderField:kAuthHeader];
  88. }
  89. dispatch_group_leave(fetchTokenGroup);
  90. }];
  91. }
  92. if (_appCheck) {
  93. dispatch_group_enter(fetchTokenGroup);
  94. [_appCheck getTokenForcingRefresh:NO
  95. completion:^(id<FIRAppCheckTokenResultInterop> tokenResult) {
  96. [request setValue:tokenResult.token
  97. forHTTPHeaderField:kAppCheckTokenHeader];
  98. if (tokenResult.error) {
  99. FIRLogDebug(kFIRLoggerStorage, kFIRStorageMessageCodeAppCheckError,
  100. @"Failed to fetch AppCheck token. Error: %@",
  101. tokenResult.error);
  102. }
  103. dispatch_group_leave(fetchTokenGroup);
  104. }];
  105. }
  106. dispatch_group_notify(fetchTokenGroup, callbackQueue, ^{
  107. [invocation invoke];
  108. });
  109. }
  110. }
  111. // Note that stopAuthorization, isAuthorizingRequest, and userEmail
  112. // aren't relevant with the Firebase App/Auth implementation of tokens,
  113. // and thus aren't implemented. Token refresh is handled transparently
  114. // for us, and we don't allow the auth request to be stopped.
  115. // Auth is also not required so the world doesn't stop.
  116. - (void)stopAuthorization {
  117. // Noop
  118. }
  119. - (void)stopAuthorizationForRequest:(NSURLRequest *)request {
  120. // Noop
  121. }
  122. - (BOOL)isAuthorizingRequest:(NSURLRequest *)request {
  123. return NO;
  124. }
  125. - (BOOL)isAuthorizedRequest:(NSURLRequest *)request {
  126. NSString *authHeader = request.allHTTPHeaderFields[@"Authorization"];
  127. BOOL isFirebaseToken = [authHeader hasPrefix:@"Firebase"];
  128. return isFirebaseToken;
  129. }
  130. - (NSString *)userEmail {
  131. // Noop
  132. return nil;
  133. }
  134. @end