Skip to content
Snippets Groups Projects
Commit c95110a7 authored by Muxi Yan's avatar Muxi Yan
Browse files

Move OAuth2 provider to GRPCCall+OAuth2

parent 7dc61e04
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,13 @@ ...@@ -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 @@ ...@@ -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 @@ ...@@ -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 "; ...@@ -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"; ...@@ -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) { ...@@ -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; ...@@ -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 @@ ...@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment