Line data Source code
1 : /*
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 : #include "test/core/security/oauth2_utils.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/grpc.h>
39 : #include <grpc/grpc_security.h>
40 : #include <grpc/support/alloc.h>
41 : #include <grpc/support/log.h>
42 : #include <grpc/support/slice.h>
43 : #include <grpc/support/sync.h>
44 :
45 : #include "src/core/security/credentials.h"
46 :
47 : typedef struct {
48 : grpc_pollset pollset;
49 : int is_done;
50 : char *token;
51 : } oauth2_request;
52 :
53 0 : static void on_oauth2_response(grpc_exec_ctx *exec_ctx, void *user_data,
54 : grpc_credentials_md *md_elems, size_t num_md,
55 : grpc_credentials_status status) {
56 0 : oauth2_request *request = user_data;
57 0 : char *token = NULL;
58 : gpr_slice token_slice;
59 0 : if (status == GRPC_CREDENTIALS_ERROR) {
60 0 : gpr_log(GPR_ERROR, "Fetching token failed.");
61 : } else {
62 0 : GPR_ASSERT(num_md == 1);
63 0 : token_slice = md_elems[0].value;
64 0 : token = gpr_malloc(GPR_SLICE_LENGTH(token_slice) + 1);
65 0 : memcpy(token, GPR_SLICE_START_PTR(token_slice),
66 0 : GPR_SLICE_LENGTH(token_slice));
67 0 : token[GPR_SLICE_LENGTH(token_slice)] = '\0';
68 : }
69 0 : gpr_mu_lock(GRPC_POLLSET_MU(&request->pollset));
70 0 : request->is_done = 1;
71 0 : request->token = token;
72 0 : grpc_pollset_kick(&request->pollset, NULL);
73 0 : gpr_mu_unlock(GRPC_POLLSET_MU(&request->pollset));
74 0 : }
75 :
76 0 : static void do_nothing(grpc_exec_ctx *exec_ctx, void *unused, int success) {}
77 :
78 0 : char *grpc_test_fetch_oauth2_token_with_credentials(grpc_credentials *creds) {
79 : oauth2_request request;
80 0 : grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
81 : grpc_closure do_nothing_closure;
82 0 : grpc_pollset_init(&request.pollset);
83 0 : request.is_done = 0;
84 :
85 0 : grpc_closure_init(&do_nothing_closure, do_nothing, NULL);
86 :
87 0 : grpc_credentials_get_request_metadata(&exec_ctx, creds, &request.pollset, "",
88 : on_oauth2_response, &request);
89 :
90 0 : grpc_exec_ctx_finish(&exec_ctx);
91 :
92 0 : gpr_mu_lock(GRPC_POLLSET_MU(&request.pollset));
93 0 : while (!request.is_done) {
94 : grpc_pollset_worker worker;
95 0 : grpc_pollset_work(&exec_ctx, &request.pollset, &worker,
96 : gpr_now(GPR_CLOCK_MONOTONIC),
97 : gpr_inf_future(GPR_CLOCK_MONOTONIC));
98 : }
99 0 : gpr_mu_unlock(GRPC_POLLSET_MU(&request.pollset));
100 :
101 0 : grpc_pollset_shutdown(&exec_ctx, &request.pollset, &do_nothing_closure);
102 0 : grpc_exec_ctx_finish(&exec_ctx);
103 0 : grpc_pollset_destroy(&request.pollset);
104 0 : return request.token;
105 : }
|