aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Muxi Yan <mxyan@google.com>2017-07-27 18:19:00 -0700
committerGravatar Muxi Yan <mxyan@google.com>2017-07-27 18:19:00 -0700
commitc95110a721e74bc7bf12cb9376d5997409473c58 (patch)
tree3a3e033b8d0c766750446846cbb4860d012c461c
parent7dc61e04c916ad0c4472267be8a15d6e4aa0c493 (diff)
Move OAuth2 provider to GRPCCall+OAuth2
-rw-r--r--src/objective-c/GRPCClient/GRPCCall+OAuth2.h15
-rw-r--r--src/objective-c/GRPCClient/GRPCCall+OAuth2.m11
-rw-r--r--src/objective-c/GRPCClient/GRPCCall.h15
-rw-r--r--src/objective-c/GRPCClient/GRPCCall.m2
4 files changed, 28 insertions, 15 deletions
diff --git a/src/objective-c/GRPCClient/GRPCCall+OAuth2.h b/src/objective-c/GRPCClient/GRPCCall+OAuth2.h
index 65465e9523..adb1042aa0 100644
--- a/src/objective-c/GRPCClient/GRPCCall+OAuth2.h
+++ b/src/objective-c/GRPCClient/GRPCCall+OAuth2.h
@@ -18,6 +18,13 @@
#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. */
@interface GRPCCall (OAuth2)
@@ -33,4 +40,12 @@
/** Returns the value (if any) of the "www-authenticate" response header (the challenge header). */
@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
diff --git a/src/objective-c/GRPCClient/GRPCCall+OAuth2.m b/src/objective-c/GRPCClient/GRPCCall+OAuth2.m
index eaa7465087..8451ebe870 100644
--- a/src/objective-c/GRPCClient/GRPCCall+OAuth2.m
+++ b/src/objective-c/GRPCClient/GRPCCall+OAuth2.m
@@ -16,6 +16,8 @@
*
*/
+#import <objc/runtime.h>
+
#import "GRPCCall+OAuth2.h"
static NSString * const kAuthorizationHeader = @"authorization";
@@ -23,6 +25,7 @@ static NSString * const kBearerPrefix = @"Bearer ";
static NSString * const kChallengeHeader = @"www-authenticate";
@implementation GRPCCall (OAuth2)
+@dynamic tokenProvider;
- (NSString *)oauth2AccessToken {
NSString *headerValue = self.requestHeaders[kAuthorizationHeader];
@@ -45,4 +48,12 @@ static NSString * const kChallengeHeader = @"www-authenticate";
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
diff --git a/src/objective-c/GRPCClient/GRPCCall.h b/src/objective-c/GRPCClient/GRPCCall.h
index 11e898242e..178a446c8b 100644
--- a/src/objective-c/GRPCClient/GRPCCall.h
+++ b/src/objective-c/GRPCClient/GRPCCall.h
@@ -140,13 +140,6 @@ typedef NS_ENUM(NSUInteger, GRPCErrorCode) {
};
/**
- * 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
*/
typedef NS_ENUM(NSUInteger, GRPCCallSafety) {
@@ -223,14 +216,6 @@ extern id const kGRPCTrailersKey;
@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
* receive each of those separately and in order as distinct messages.
* A gRPC call might not complete until the request writer finishes. On the other hand, the request
diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m
index 7cc94ad2fe..436c19e354 100644
--- a/src/objective-c/GRPCClient/GRPCCall.m
+++ b/src/objective-c/GRPCClient/GRPCCall.m
@@ -18,6 +18,8 @@
#import "GRPCCall.h"
+#import "GRPCCall+OAuth2.h"
+
#include <grpc/grpc.h>
#include <grpc/support/time.h>
#import <RxLibrary/GRXConcurrentWriteable.h>