1/* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19#import <objc/runtime.h> 20 21#import "GRPCCall+OAuth2.h" 22 23static NSString *const kAuthorizationHeader = @"authorization"; 24static NSString *const kBearerPrefix = @"Bearer "; 25static NSString *const kChallengeHeader = @"www-authenticate"; 26 27@implementation GRPCCall (OAuth2) 28@dynamic tokenProvider; 29 30- (NSString *)oauth2AccessToken { 31 NSString *headerValue = self.requestHeaders[kAuthorizationHeader]; 32 if ([headerValue hasPrefix:kBearerPrefix]) { 33 return [headerValue substringFromIndex:kBearerPrefix.length]; 34 } else { 35 return nil; 36 } 37} 38 39- (void)setOauth2AccessToken:(NSString *)token { 40 if (token) { 41 self.requestHeaders[kAuthorizationHeader] = [kBearerPrefix stringByAppendingString:token]; 42 } else { 43 [self.requestHeaders removeObjectForKey:kAuthorizationHeader]; 44 } 45} 46 47- (NSString *)oauth2ChallengeHeader { 48 return self.responseHeaders[kChallengeHeader]; 49} 50 51- (void)setTokenProvider:(id<GRPCAuthorizationProtocol>)tokenProvider { 52 objc_setAssociatedObject(self, @selector(tokenProvider), tokenProvider, OBJC_ASSOCIATION_RETAIN); 53} 54 55- (id<GRPCAuthorizationProtocol>)tokenProvider { 56 return objc_getAssociatedObject(self, @selector(tokenProvider)); 57} 58 59@end 60