aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Storage/FIRStorageTokenAuthorizer.m
diff options
context:
space:
mode:
authorGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
committerGravatar Paul Beusterien <paulbeusterien@google.com>2017-05-15 12:27:07 -0700
commit98ba64449a632518bd2b86fe8d927f4a960d3ddc (patch)
tree131d9c4272fa6179fcda6c5a33fcb3b1bd57ad2e /Firebase/Storage/FIRStorageTokenAuthorizer.m
parent32461366c9e204a527ca05e6e9b9404a2454ac51 (diff)
Initial
Diffstat (limited to 'Firebase/Storage/FIRStorageTokenAuthorizer.m')
-rw-r--r--Firebase/Storage/FIRStorageTokenAuthorizer.m131
1 files changed, 131 insertions, 0 deletions
diff --git a/Firebase/Storage/FIRStorageTokenAuthorizer.m b/Firebase/Storage/FIRStorageTokenAuthorizer.m
new file mode 100644
index 0000000..36b94a9
--- /dev/null
+++ b/Firebase/Storage/FIRStorageTokenAuthorizer.m
@@ -0,0 +1,131 @@
+// Copyright 2017 Google
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#import "../../Firebase/Core/Private/FIRAppInternal.h"
+
+#import "FIRStorageTokenAuthorizer.h"
+
+#import "FIRStorageConstants.h"
+#import "FIRStorageConstants_Private.h"
+#import "FIRStorageErrors.h"
+
+#import "FirebaseStorage.h"
+
+#import "FIRApp.h"
+#import "FIROptions.h"
+
+@implementation FIRStorageTokenAuthorizer {
+ @private
+ // Firebase App which vends tokens
+ FIRApp *_app;
+}
+
+@synthesize fetcherService = _fetcherService;
+
+- (instancetype)initWithApp:(FIRApp *)app fetcherService:(GTMSessionFetcherService *)service {
+ self = [super init];
+ if (self) {
+ _app = app;
+ _fetcherService = service;
+ }
+ return self;
+}
+
+#pragma mark - GTMFetcherAuthorizationProtocol methods
+
+- (void)authorizeRequest:(NSMutableURLRequest *)request
+ delegate:(id)delegate
+ didFinishSelector:(SEL)sel {
+ // Set version header on each request
+ NSString *versionString = [NSString stringWithFormat:@"ios/%s", FIRStorageVersionString];
+ [request setValue:versionString forHTTPHeaderField:@"x-firebase-storage-version"];
+
+ // Set GMP ID on each request
+ NSString *GMPAppId = _app.options.googleAppID;
+ [request setValue:GMPAppId forHTTPHeaderField:@"x-firebase-gmpid"];
+
+ if (delegate && sel) {
+ id selfParam = self;
+ NSMethodSignature *sig = [delegate methodSignatureForSelector:sel];
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
+ [invocation setSelector:sel];
+ [invocation setTarget:delegate];
+ [invocation setArgument:&selfParam atIndex:2];
+ [invocation setArgument:&request atIndex:3];
+
+ dispatch_queue_t callbackQueue = self.fetcherService.callbackQueue;
+ if (!callbackQueue) {
+ callbackQueue = dispatch_get_main_queue();
+ }
+
+ [invocation retainArguments];
+ if (_app.getTokenImplementation) {
+ [_app getTokenForcingRefresh:NO
+ withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
+ if (error) {
+ NSMutableDictionary *errorDictionary =
+ [NSMutableDictionary dictionaryWithDictionary:error.userInfo];
+ errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
+ errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
+
+ NSError *tokenError =
+ [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnauthenticated
+ infoDictionary:errorDictionary];
+ [invocation setArgument:&tokenError atIndex:4];
+ } else if (token) {
+ NSString *firebaseToken =
+ [NSString stringWithFormat:kFIRStorageAuthTokenFormat, token];
+ [request setValue:firebaseToken forHTTPHeaderField:@"Authorization"];
+ }
+ dispatch_async(callbackQueue, ^{
+ [invocation invoke];
+ });
+ }];
+ } else {
+ dispatch_async(callbackQueue, ^{
+ [invocation invoke];
+ });
+ }
+ }
+}
+
+// Note that stopAuthorization, isAuthorizingRequest, and userEmail
+// aren't relevant with the Firebase App/Auth implementation of tokens,
+// and thus aren't implemented. Token refresh is handled transparently
+// for us, and we don't allow the auth request to be stopped.
+// Auth is also not required so the world doesn't stop.
+- (void)stopAuthorization {
+ // Noop
+}
+
+- (void)stopAuthorizationForRequest:(NSURLRequest *)request {
+ // Noop
+}
+
+- (BOOL)isAuthorizingRequest:(NSURLRequest *)request {
+ return NO;
+}
+
+- (BOOL)isAuthorizedRequest:(NSURLRequest *)request {
+ NSString *authHeader = request.allHTTPHeaderFields[@"Authorization"];
+ BOOL isFirebaseToken = [authHeader hasPrefix:@"Firebase"];
+ return isFirebaseToken;
+}
+
+- (NSString *)userEmail {
+ // Noop
+ return nil;
+}
+
+@end