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/security/credentials/credentials.h"
31
32 typedef struct {
33 gpr_mu* mu;
34 grpc_polling_entity pops;
35 bool is_done;
36 char* token;
37
38 grpc_credentials_mdelem_array md_array;
39 grpc_closure closure;
40 } oauth2_request;
41
on_oauth2_response(void * arg,grpc_error * error)42 static void on_oauth2_response(void* arg, grpc_error* error) {
43 oauth2_request* request = static_cast<oauth2_request*>(arg);
44 char* token = nullptr;
45 grpc_slice token_slice;
46 if (error != GRPC_ERROR_NONE) {
47 gpr_log(GPR_ERROR, "Fetching token failed: %s", grpc_error_string(error));
48 } else {
49 GPR_ASSERT(request->md_array.size == 1);
50 token_slice = GRPC_MDVALUE(request->md_array.md[0]);
51 token = static_cast<char*>(gpr_malloc(GRPC_SLICE_LENGTH(token_slice) + 1));
52 memcpy(token, GRPC_SLICE_START_PTR(token_slice),
53 GRPC_SLICE_LENGTH(token_slice));
54 token[GRPC_SLICE_LENGTH(token_slice)] = '\0';
55 }
56 grpc_credentials_mdelem_array_destroy(&request->md_array);
57 gpr_mu_lock(request->mu);
58 request->is_done = true;
59 request->token = token;
60 GRPC_LOG_IF_ERROR(
61 "pollset_kick",
62 grpc_pollset_kick(grpc_polling_entity_pollset(&request->pops), nullptr));
63 gpr_mu_unlock(request->mu);
64 }
65
do_nothing(void * unused,grpc_error * error)66 static void do_nothing(void* unused, grpc_error* error) {}
67
grpc_test_fetch_oauth2_token_with_credentials(grpc_call_credentials * creds)68 char* grpc_test_fetch_oauth2_token_with_credentials(
69 grpc_call_credentials* creds) {
70 oauth2_request request;
71 memset(&request, 0, sizeof(request));
72 grpc_core::ExecCtx exec_ctx;
73 grpc_closure do_nothing_closure;
74 grpc_auth_metadata_context null_ctx = {"", "", nullptr, nullptr};
75
76 grpc_pollset* pollset =
77 static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
78 grpc_pollset_init(pollset, &request.mu);
79 request.pops = grpc_polling_entity_create_from_pollset(pollset);
80 request.is_done = false;
81
82 GRPC_CLOSURE_INIT(&do_nothing_closure, do_nothing, nullptr,
83 grpc_schedule_on_exec_ctx);
84
85 GRPC_CLOSURE_INIT(&request.closure, on_oauth2_response, &request,
86 grpc_schedule_on_exec_ctx);
87
88 grpc_error* error = GRPC_ERROR_NONE;
89 if (grpc_call_credentials_get_request_metadata(creds, &request.pops, null_ctx,
90 &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
113 gpr_free(grpc_polling_entity_pollset(&request.pops));
114 return request.token;
115 }
116