FIRStorageTokenAuthorizer.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <FirebaseCore/FIRAppInternal.h>
  15. #import "FIRStorageTokenAuthorizer.h"
  16. #import "FIRStorageConstants.h"
  17. #import "FIRStorageConstants_Private.h"
  18. #import "FIRStorageErrors.h"
  19. #import "FirebaseStorage.h"
  20. #import <FirebaseCore/FIRApp.h>
  21. #import <FirebaseCore/FIROptions.h>
  22. @implementation FIRStorageTokenAuthorizer {
  23. @private
  24. // Firebase App which vends tokens
  25. FIRApp *_app;
  26. }
  27. @synthesize fetcherService = _fetcherService;
  28. - (instancetype)initWithApp:(FIRApp *)app fetcherService:(GTMSessionFetcherService *)service {
  29. self = [super init];
  30. if (self) {
  31. _app = app;
  32. _fetcherService = service;
  33. }
  34. return self;
  35. }
  36. #pragma mark - GTMFetcherAuthorizationProtocol methods
  37. - (void)authorizeRequest:(NSMutableURLRequest *)request
  38. delegate:(id)delegate
  39. didFinishSelector:(SEL)sel {
  40. // Set version header on each request
  41. NSString *versionString = [NSString stringWithFormat:@"ios/%s", FIRStorageVersionString];
  42. [request setValue:versionString forHTTPHeaderField:@"x-firebase-storage-version"];
  43. // Set GMP ID on each request
  44. NSString *GMPAppId = _app.options.googleAppID;
  45. [request setValue:GMPAppId forHTTPHeaderField:@"x-firebase-gmpid"];
  46. if (delegate && sel) {
  47. id selfParam = self;
  48. NSMethodSignature *sig = [delegate methodSignatureForSelector:sel];
  49. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
  50. [invocation setSelector:sel];
  51. [invocation setTarget:delegate];
  52. [invocation setArgument:&selfParam atIndex:2];
  53. [invocation setArgument:&request atIndex:3];
  54. dispatch_queue_t callbackQueue = self.fetcherService.callbackQueue;
  55. if (!callbackQueue) {
  56. callbackQueue = dispatch_get_main_queue();
  57. }
  58. [invocation retainArguments];
  59. if (_app.getTokenImplementation) {
  60. [_app getTokenForcingRefresh:NO
  61. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  62. if (error) {
  63. NSMutableDictionary *errorDictionary =
  64. [NSMutableDictionary dictionaryWithDictionary:error.userInfo];
  65. errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
  66. errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
  67. NSError *tokenError =
  68. [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnauthenticated
  69. infoDictionary:errorDictionary];
  70. [invocation setArgument:&tokenError atIndex:4];
  71. } else if (token) {
  72. NSString *firebaseToken =
  73. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, token];
  74. [request setValue:firebaseToken forHTTPHeaderField:@"Authorization"];
  75. }
  76. dispatch_async(callbackQueue, ^{
  77. [invocation invoke];
  78. });
  79. }];
  80. } else {
  81. dispatch_async(callbackQueue, ^{
  82. [invocation invoke];
  83. });
  84. }
  85. }
  86. }
  87. // Note that stopAuthorization, isAuthorizingRequest, and userEmail
  88. // aren't relevant with the Firebase App/Auth implementation of tokens,
  89. // and thus aren't implemented. Token refresh is handled transparently
  90. // for us, and we don't allow the auth request to be stopped.
  91. // Auth is also not required so the world doesn't stop.
  92. - (void)stopAuthorization {
  93. // Noop
  94. }
  95. - (void)stopAuthorizationForRequest:(NSURLRequest *)request {
  96. // Noop
  97. }
  98. - (BOOL)isAuthorizingRequest:(NSURLRequest *)request {
  99. return NO;
  100. }
  101. - (BOOL)isAuthorizedRequest:(NSURLRequest *)request {
  102. NSString *authHeader = request.allHTTPHeaderFields[@"Authorization"];
  103. BOOL isFirebaseToken = [authHeader hasPrefix:@"Firebase"];
  104. return isFirebaseToken;
  105. }
  106. - (NSString *)userEmail {
  107. // Noop
  108. return nil;
  109. }
  110. @end