xref: /aosp_15_r20/external/grpc-grpc/examples/objective-c/auth_sample/MakeRPCViewController.m (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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@interface MakeRPCViewController ()<GRPCProtoResponseHandler>
50
51@end
52
53@implementation MakeRPCViewController
54
55- (dispatch_queue_t)dispatchQueue {
56  return dispatch_get_main_queue();
57}
58
59- (void)viewWillAppear:(BOOL)animated {
60
61  // Create a service client and a proto request as usual.
62  AUTHTestService *client = [[AUTHTestService alloc] initWithHost:kTestHostAddress];
63
64  AUTHRequest *request = [AUTHRequest message];
65  request.fillUsername = YES;
66  request.fillOauthScope = YES;
67
68  // Set the request header with call options
69  GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
70  options.oauth2AccessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken;
71  GRPCUnaryProtoCall *call = [client unaryCallWithMessage:request
72                                          responseHandler:self
73                                              callOptions:options];
74  [call start];
75
76  self.mainLabel.text = @"Waiting for RPC to complete...";
77}
78
79- (void)didReceiveProtoMessage:(GPBMessage *)message {
80  AUTHResponse *response = (AUTHResponse *)message;
81  if (response) {
82    // This test server responds with the email and scope of the access token it receives.
83    self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@",
84                           response.oauthScope, response.username];
85  }
86}
87
88- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
89  if (error) {
90    self.mainLabel.text = error.UIDescription;
91  }
92}
93
94@end
95