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 "MakeRPCViewController.h" 20 21#import <AuthTestService/AuthSample.pbrpc.h> 22#import <Google/SignIn.h> 23#import <ProtoRPC/ProtoRPC.h> 24 25NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo"; 26 27static NSString * const kTestHostAddress = @"grpc-test.sandbox.googleapis.com"; 28 29// Category for RPC errors to create the descriptions as we want them to appear on our view. 30@interface NSError (AuthSample) 31- (NSString *)UIDescription; 32@end 33 34@implementation NSError (AuthSample) 35- (NSString *)UIDescription { 36 if (self.code == GRPCErrorCodeUnauthenticated) { 37 // Authentication error. OAuth2 specifies we'll receive a challenge header. 38 // |userInfo[kGRPCHeadersKey]| is the dictionary of response headers. 39 NSString *challengeHeader = self.userInfo[kGRPCHeadersKey][@"www-authenticate"] ?: @""; 40 return [@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader]; 41 } else { 42 // Any other error. 43 return [NSString stringWithFormat:@"Unexpected RPC error %li: %@", 44 (long)self.code, self.localizedDescription]; 45 } 46} 47@end 48 49@implementation MakeRPCViewController 50 51- (void)viewWillAppear:(BOOL)animated { 52 53 // Create a service client and a proto request as usual. 54 AUTHTestService *client = [[AUTHTestService alloc] initWithHost:kTestHostAddress]; 55 56 AUTHRequest *request = [AUTHRequest message]; 57 request.fillUsername = YES; 58 request.fillOauthScope = YES; 59 60 // Create a not-yet-started RPC. We want to set the request headers on this object before starting 61 // it. 62 ProtoRPC *call = 63 [client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) { 64 if (response) { 65 // This test server responds with the email and scope of the access token it receives. 66 self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@", 67 response.oauthScope, response.username]; 68 69 } else { 70 self.mainLabel.text = error.UIDescription; 71 } 72 }]; 73 74 // Set the access token to be used. 75 NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken; 76 call.requestHeaders[@"Authorization"] = [@"Bearer " stringByAppendingString:accessToken]; 77 78 // Start the RPC. 79 [call start]; 80 81 self.mainLabel.text = @"Waiting for RPC to complete..."; 82} 83 84@end 85