Update AuthSample to v0.6
This commit is contained in:
parent
6fb1b537b1
commit
1748bf2dfe
|
|
@ -35,13 +35,33 @@
|
||||||
|
|
||||||
#import <AuthTestService/AuthSample.pbrpc.h>
|
#import <AuthTestService/AuthSample.pbrpc.h>
|
||||||
#import <Google/SignIn.h>
|
#import <Google/SignIn.h>
|
||||||
#import <gRPC/ProtoRPC.h>
|
#include <grpc/status.h>
|
||||||
#include <gRPC/status.h>
|
#import <ProtoRPC/ProtoRPC.h>
|
||||||
|
|
||||||
NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo";
|
NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo";
|
||||||
|
|
||||||
static NSString * const kTestHostAddress = @"grpc-test.sandbox.google.com";
|
static NSString * const kTestHostAddress = @"grpc-test.sandbox.google.com";
|
||||||
|
|
||||||
|
// Category for RPC errors to create the descriptions as we want them to appear on our view.
|
||||||
|
@interface NSError (AuthSample)
|
||||||
|
- (NSString *)UIDescription;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation NSError (AuthSample)
|
||||||
|
- (NSString *)UIDescription {
|
||||||
|
if (self.code == GRPC_STATUS_UNAUTHENTICATED) {
|
||||||
|
// Authentication error. OAuth2 specifies we'll receive a challenge header.
|
||||||
|
// |userInfo[kGRPCStatusMetadataKey]| is the dictionary of response metadata.
|
||||||
|
NSString *challengeHeader = self.userInfo[kGRPCStatusMetadataKey][@"www-authenticate"] ?: @"";
|
||||||
|
return [@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader];
|
||||||
|
} else {
|
||||||
|
// Any other error.
|
||||||
|
return [NSString stringWithFormat:@"Unexpected RPC error %li: %@",
|
||||||
|
(long)self.code, self.localizedDescription];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MakeRPCViewController
|
@implementation MakeRPCViewController
|
||||||
|
|
||||||
- (void)viewWillAppear:(BOOL)animated {
|
- (void)viewWillAppear:(BOOL)animated {
|
||||||
|
|
@ -55,30 +75,21 @@ static NSString * const kTestHostAddress = @"grpc-test.sandbox.google.com";
|
||||||
|
|
||||||
// Create a not-yet-started RPC. We want to set the request headers on this object before starting
|
// Create a not-yet-started RPC. We want to set the request headers on this object before starting
|
||||||
// it.
|
// it.
|
||||||
__block ProtoRPC *call =
|
ProtoRPC *call =
|
||||||
[client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) {
|
[client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) {
|
||||||
if (response) {
|
if (response) {
|
||||||
// This test server responds with the email and scope of the access token it receives.
|
// This test server responds with the email and scope of the access token it receives.
|
||||||
self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@",
|
self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@",
|
||||||
response.oauthScope, response.username];
|
response.oauthScope, response.username];
|
||||||
|
|
||||||
} else if (error.code == GRPC_STATUS_UNAUTHENTICATED) {
|
|
||||||
// Authentication error. OAuth2 specifies we'll receive a challenge header.
|
|
||||||
NSString *challengeHeader = call.responseMetadata[@"www-authenticate"][0] ?: @"";
|
|
||||||
self.mainLabel.text =
|
|
||||||
[@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader];
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Any other error.
|
self.mainLabel.text = error.UIDescription;
|
||||||
self.mainLabel.text = [NSString stringWithFormat:@"Unexpected RPC error %li: %@",
|
|
||||||
(long)error.code, error.localizedDescription];
|
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
|
|
||||||
// Set the access token to be used.
|
// Set the access token to be used.
|
||||||
NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken;
|
NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken;
|
||||||
call.requestMetadata = [NSMutableDictionary dictionaryWithDictionary:
|
call.requestMetadata[@"Authorization"] = [@"Bearer " stringByAppendingString:accessToken];
|
||||||
@{@"Authorization": [@"Bearer " stringByAppendingString:accessToken]}];
|
|
||||||
|
|
||||||
// Start the RPC.
|
// Start the RPC.
|
||||||
[call start];
|
[call start];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue