Move OAuth2 provider to GRPCCall+OAuth2

pull/11832/head
Muxi Yan 7 years ago
parent 7dc61e04c9
commit c95110a721
  1. 15
      src/objective-c/GRPCClient/GRPCCall+OAuth2.h
  2. 11
      src/objective-c/GRPCClient/GRPCCall+OAuth2.m
  3. 15
      src/objective-c/GRPCClient/GRPCCall.h
  4. 2
      src/objective-c/GRPCClient/GRPCCall.m

@ -18,6 +18,13 @@
#import "GRPCCall.h" #import "GRPCCall.h"
/**
* The protocol of an OAuth2 token object from which GRPCCall can acquire a token.
*/
@protocol GRPCAuthorizationProtocol
- (void)getTokenWithHandler:(void (^)(NSString *token))hander;
@end
/** Helpers for setting and reading headers compatible with OAuth2. */ /** Helpers for setting and reading headers compatible with OAuth2. */
@interface GRPCCall (OAuth2) @interface GRPCCall (OAuth2)
@ -33,4 +40,12 @@
/** Returns the value (if any) of the "www-authenticate" response header (the challenge header). */ /** Returns the value (if any) of the "www-authenticate" response header (the challenge header). */
@property(atomic, readonly) NSString *oauth2ChallengeHeader; @property(atomic, readonly) NSString *oauth2ChallengeHeader;
/**
* The authorization token object to be used when starting the call. If the value is set to nil, no
* oauth authentication will be used.
*
* If tokenProvider exists, it takes precedence over the token set by oauth2AccessToken.
*/
@property(atomic, strong) id<GRPCAuthorizationProtocol> tokenProvider;
@end @end

@ -16,6 +16,8 @@
* *
*/ */
#import <objc/runtime.h>
#import "GRPCCall+OAuth2.h" #import "GRPCCall+OAuth2.h"
static NSString * const kAuthorizationHeader = @"authorization"; static NSString * const kAuthorizationHeader = @"authorization";
@ -23,6 +25,7 @@ static NSString * const kBearerPrefix = @"Bearer ";
static NSString * const kChallengeHeader = @"www-authenticate"; static NSString * const kChallengeHeader = @"www-authenticate";
@implementation GRPCCall (OAuth2) @implementation GRPCCall (OAuth2)
@dynamic tokenProvider;
- (NSString *)oauth2AccessToken { - (NSString *)oauth2AccessToken {
NSString *headerValue = self.requestHeaders[kAuthorizationHeader]; NSString *headerValue = self.requestHeaders[kAuthorizationHeader];
@ -45,4 +48,12 @@ static NSString * const kChallengeHeader = @"www-authenticate";
return self.responseHeaders[kChallengeHeader]; return self.responseHeaders[kChallengeHeader];
} }
- (void)setTokenProvider:(id<GRPCAuthorizationProtocol>)tokenProvider {
objc_setAssociatedObject(self, @selector(tokenProvider), tokenProvider, OBJC_ASSOCIATION_RETAIN);
}
- (id<GRPCAuthorizationProtocol>)tokenProvider {
return objc_getAssociatedObject(self, @selector(tokenProvider));
}
@end @end

@ -139,13 +139,6 @@ typedef NS_ENUM(NSUInteger, GRPCErrorCode) {
GRPCErrorCodeDataLoss = 15, GRPCErrorCodeDataLoss = 15,
}; };
/**
* The protocol of an OAuth2 token object from which GRPCCall can acquire a token.
*/
@protocol GRPCAuthorizationProtocol
- (void)getTokenWithHandler:(void (^)(NSString *token))hander;
@end
/** /**
* Safety remark of a gRPC method as defined in RFC 2616 Section 9.1 * Safety remark of a gRPC method as defined in RFC 2616 Section 9.1
*/ */
@ -222,14 +215,6 @@ extern id const kGRPCTrailersKey;
*/ */
@property(atomic, readonly) NSDictionary *responseTrailers; @property(atomic, readonly) NSDictionary *responseTrailers;
/**
* The authorization token object to be used when starting the call. If the value is set to nil, no
* oauth authentication will be used.
*
* Not compatible with property oauth2AccessToken in GRPCCall (OAuth2). Do not use both at the same time.
*/
@property(atomic, strong) id<GRPCAuthorizationProtocol> tokenProvider;
/** /**
* The request writer has to write NSData objects into the provided Writeable. The server will * The request writer has to write NSData objects into the provided Writeable. The server will
* receive each of those separately and in order as distinct messages. * receive each of those separately and in order as distinct messages.

@ -18,6 +18,8 @@
#import "GRPCCall.h" #import "GRPCCall.h"
#import "GRPCCall+OAuth2.h"
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#import <RxLibrary/GRXConcurrentWriteable.h> #import <RxLibrary/GRXConcurrentWriteable.h>

Loading…
Cancel
Save