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 * error)43 static void on_oauth2_response(void* arg, grpc_error* 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", grpc_error_string(error));
49   } else {
50     GPR_ASSERT(request->md_array.size == 1);
51     token_slice = GRPC_MDVALUE(request->md_array.md[0]);
52     token = static_cast<char*>(gpr_malloc(GRPC_SLICE_LENGTH(token_slice) + 1));
53     memcpy(token, GRPC_SLICE_START_PTR(token_slice),
54            GRPC_SLICE_LENGTH(token_slice));
55     token[GRPC_SLICE_LENGTH(token_slice)] = '\0';
56   }
57   grpc_credentials_mdelem_array_destroy(&request->md_array);
58   gpr_mu_lock(request->mu);
59   request->is_done = true;
60   request->token = token;
61   GRPC_LOG_IF_ERROR(
62       "pollset_kick",
63       grpc_pollset_kick(grpc_polling_entity_pollset(&request->pops), nullptr));
64   gpr_mu_unlock(request->mu);
65 }
66 
do_nothing(void *,grpc_error *)67 static void do_nothing(void* /*arg*/, grpc_error* /*error*/) {}
68 
grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials * creds)69 char* grpc_test_fetch_oauth2_token_with_credentials(
70     grpc_call_credentials* creds) {
71   oauth2_request request;
72   memset(&request, 0, sizeof(request));
73   grpc_core::ExecCtx exec_ctx;
74   grpc_closure do_nothing_closure;
75   grpc_auth_metadata_context null_ctx = {"", "", nullptr, nullptr};
76 
77   grpc_pollset* pollset =
78       static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
79   grpc_pollset_init(pollset, &request.mu);
80   request.pops = grpc_polling_entity_create_from_pollset(pollset);
81   request.is_done = false;
82 
83   GRPC_CLOSURE_INIT(&do_nothing_closure, do_nothing, nullptr,
84                     grpc_schedule_on_exec_ctx);
85 
86   GRPC_CLOSURE_INIT(&request.closure, on_oauth2_response, &request,
87                     grpc_schedule_on_exec_ctx);
88 
89   grpc_error* error = GRPC_ERROR_NONE;
90   if (creds->get_request_metadata(&request.pops, null_ctx, &request.md_array,
91                                   &request.closure, &error)) {
92     // Synchronous result; invoke callback directly.
93     on_oauth2_response(&request, error);
94     GRPC_ERROR_UNREF(error);
95   }
96   grpc_core::ExecCtx::Get()->Flush();
97 
98   gpr_mu_lock(request.mu);
99   while (!request.is_done) {
100     grpc_pollset_worker* worker = nullptr;
101     if (!GRPC_LOG_IF_ERROR(
102             "pollset_work",
103             grpc_pollset_work(grpc_polling_entity_pollset(&request.pops),
104                               &worker, GRPC_MILLIS_INF_FUTURE))) {
105       request.is_done = true;
106     }
107   }
108   gpr_mu_unlock(request.mu);
109 
110   grpc_pollset_shutdown(grpc_polling_entity_pollset(&request.pops),
111                         &do_nothing_closure);
112   grpc_core::ExecCtx::Get()->Flush();
113   grpc_pollset_destroy(grpc_polling_entity_pollset(&request.pops));
114   gpr_free(pollset);
115   return request.token;
116 }
117