blob: 410ae7d92bd6540c5c4d337dddea23e195c1d912 [file] [log] [blame]
Jorge Canizales64428ac2015-06-11 15:21:55 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#import "MakeRPCViewController.h"
35
36#import <AuthTestService/AuthSample.pbrpc.h>
37#import <Google/SignIn.h>
38#import <gRPC/ProtoRPC.h>
39#include <gRPC/status.h>
40
41NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo";
42
43static NSString * const kTestHostAddress = @"grpc-test.sandbox.google.com";
44
45@implementation MakeRPCViewController
46
47- (void)viewWillAppear:(BOOL)animated {
48
49 // Create a service client and a proto request as usual.
50 AUTHTestService *client = [[AUTHTestService alloc] initWithHost:kTestHostAddress];
51
52 AUTHRequest *request = [AUTHRequest message];
53 request.fillUsername = YES;
54 request.fillOauthScope = YES;
55
56 // Create a not-yet-started RPC. We want to set the request headers on this object before starting
57 // it.
58 __block ProtoRPC *call =
59 [client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) {
60 if (response) {
61 // This test server responds with the email and scope of the access token it receives.
62 self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@",
63 response.oauthScope, response.username];
64
65 } else if (error.code == GRPC_STATUS_UNAUTHENTICATED) {
66 // Authentication error. OAuth2 specifies we'll receive a challenge header.
67 NSString *challengeHeader = call.responseMetadata[@"www-authenticate"][0] ?: @"";
68 self.mainLabel.text =
69 [@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader];
70
71 } else {
72 // Any other error.
73 self.mainLabel.text = [NSString stringWithFormat:@"Unexpected RPC error %li: %@",
74 (long)error.code, error.localizedDescription];
75 }
76 }];
77
78 // Set the access token to be used.
79 NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken;
80 call.requestMetadata = [NSMutableDictionary dictionaryWithDictionary:
81 @{@"Authorization": [@"Bearer " stringByAppendingString:accessToken]}];
82
83 // Start the RPC.
84 [call start];
85
86 self.mainLabel.text = @"Waiting for RPC to complete...";
87}
88
89@end