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 #include "test/core/security/oauth2_utils.h"
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpc/slice.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
29 
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/security/credentials/credentials.h"
32 
33 typedef struct {
34   gpr_mu* mu;
35   grpc_polling_entity pops;
36   bool is_done;
37   char* token;
38 
39   grpc_credentials_mdelem_array md_array;
40   grpc_closure closure;
41 } oauth2_request;
42 
on_oauth2_response(void * arg,grpc_error_handle error)43 static void on_oauth2_response(void* arg, grpc_error_handle error) {
44   oauth2_request* request = static_cast<oauth2_request*>(arg);
45   char* token = nullptr;
46   grpc_slice token_slice;
47   if (error != GRPC_ERROR_NONE) {
48     gpr_log(GPR_ERROR, "Fetching token failed: %s",
49             grpc_error_std_string(error).c_str());
50   } else {
51     GPR_ASSERT(request->md_array.size == 1);
52     token_slice = GRPC_MDVALUE(request->md_array.md[0]);
53     token = static_cast<char*>(gpr_malloc(GRPC_SLICE_LENGTH(token_slice) + 1));
54     memcpy(token, GRPC_SLICE_START_PTR(token_slice),
55            GRPC_SLICE_LENGTH(token_slice));
56     token[GRPC_SLICE_LENGTH(token_slice)] = '\0';
57   }
58   grpc_credentials_mdelem_array_destroy(&request->md_array);
59   gpr_mu_lock(request->mu);
60   request->is_done = true;
61   request->token = token;
62   GRPC_LOG_IF_ERROR(
63       "pollset_kick",
64       grpc_pollset_kick(grpc_polling_entity_pollset(&request->pops), nullptr));
65   gpr_mu_unlock(request->mu);
66 }
67 
do_nothing(void *,grpc_error_handle)68 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
69 
grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials * creds)70 char* grpc_test_fetch_oauth2_token_with_credentials(
71     grpc_call_credentials* creds) {
72   oauth2_request request;
73   memset(&request, 0, sizeof(request));
74   grpc_core::ExecCtx exec_ctx;
75   grpc_closure do_nothing_closure;
76   grpc_auth_metadata_context null_ctx = {"", "", nullptr, nullptr};
77 
78   grpc_pollset* pollset =
79       static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
80   grpc_pollset_init(pollset, &request.mu);
81   request.pops = grpc_polling_entity_create_from_pollset(pollset);
82   request.is_done = false;
83 
84   GRPC_CLOSURE_INIT(&do_nothing_closure, do_nothing, nullptr,
85                     grpc_schedule_on_exec_ctx);
86 
87   GRPC_CLOSURE_INIT(&request.closure, on_oauth2_response, &request,
88                     grpc_schedule_on_exec_ctx);
89 
90   grpc_error_handle error = GRPC_ERROR_NONE;
91   if (creds->get_request_metadata(&request.pops, null_ctx, &request.md_array,
92                                   &request.closure, &error)) {
93     // Synchronous result; invoke callback directly.
94     on_oauth2_response(&request, error);
95     GRPC_ERROR_UNREF(error);
96   }
97   grpc_core::ExecCtx::Get()->Flush();
98 
99   gpr_mu_lock(request.mu);
100   while (!request.is_done) {
101     grpc_pollset_worker* worker = nullptr;
102     if (!GRPC_LOG_IF_ERROR(
103             "pollset_work",
104             grpc_pollset_work(grpc_polling_entity_pollset(&request.pops),
105                               &worker, GRPC_MILLIS_INF_FUTURE))) {
106       request.is_done = true;
107     }
108   }
109   gpr_mu_unlock(request.mu);
110 
111   grpc_pollset_shutdown(grpc_polling_entity_pollset(&request.pops),
112                         &do_nothing_closure);
113   grpc_core::ExecCtx::Get()->Flush();
114   grpc_pollset_destroy(grpc_polling_entity_pollset(&request.pops));
115   gpr_free(pollset);
116   return request.token;
117 }
118