FIRStorageTokenAuthorizer.m 4.7 KB

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