OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 gpr_mu *mu; |
| 49 grpc_pollset *pollset; |
| 50 int is_done; |
| 51 char *token; |
| 52 } oauth2_request; |
| 53 |
| 54 static void on_oauth2_response(grpc_exec_ctx *exec_ctx, void *user_data, |
| 55 grpc_credentials_md *md_elems, size_t num_md, |
| 56 grpc_credentials_status status) { |
| 57 oauth2_request *request = user_data; |
| 58 char *token = NULL; |
| 59 gpr_slice token_slice; |
| 60 if (status == GRPC_CREDENTIALS_ERROR) { |
| 61 gpr_log(GPR_ERROR, "Fetching token failed."); |
| 62 } else { |
| 63 GPR_ASSERT(num_md == 1); |
| 64 token_slice = md_elems[0].value; |
| 65 token = gpr_malloc(GPR_SLICE_LENGTH(token_slice) + 1); |
| 66 memcpy(token, GPR_SLICE_START_PTR(token_slice), |
| 67 GPR_SLICE_LENGTH(token_slice)); |
| 68 token[GPR_SLICE_LENGTH(token_slice)] = '\0'; |
| 69 } |
| 70 gpr_mu_lock(request->mu); |
| 71 request->is_done = 1; |
| 72 request->token = token; |
| 73 grpc_pollset_kick(request->pollset, NULL); |
| 74 gpr_mu_unlock(request->mu); |
| 75 } |
| 76 |
| 77 static void do_nothing(grpc_exec_ctx *exec_ctx, void *unused, bool success) {} |
| 78 |
| 79 char *grpc_test_fetch_oauth2_token_with_credentials( |
| 80 grpc_call_credentials *creds) { |
| 81 oauth2_request request; |
| 82 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 83 grpc_closure do_nothing_closure; |
| 84 grpc_auth_metadata_context null_ctx = {"", "", NULL, NULL}; |
| 85 |
| 86 request.pollset = gpr_malloc(grpc_pollset_size()); |
| 87 grpc_pollset_init(request.pollset, &request.mu); |
| 88 request.is_done = 0; |
| 89 |
| 90 grpc_closure_init(&do_nothing_closure, do_nothing, NULL); |
| 91 |
| 92 grpc_call_credentials_get_request_metadata(&exec_ctx, creds, request.pollset, |
| 93 null_ctx, on_oauth2_response, |
| 94 &request); |
| 95 |
| 96 grpc_exec_ctx_finish(&exec_ctx); |
| 97 |
| 98 gpr_mu_lock(request.mu); |
| 99 while (!request.is_done) { |
| 100 grpc_pollset_worker *worker = NULL; |
| 101 grpc_pollset_work(&exec_ctx, request.pollset, &worker, |
| 102 gpr_now(GPR_CLOCK_MONOTONIC), |
| 103 gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 104 } |
| 105 gpr_mu_unlock(request.mu); |
| 106 |
| 107 grpc_pollset_shutdown(&exec_ctx, request.pollset, &do_nothing_closure); |
| 108 grpc_exec_ctx_finish(&exec_ctx); |
| 109 grpc_pollset_destroy(request.pollset); |
| 110 gpr_free(request.pollset); |
| 111 return request.token; |
| 112 } |
OLD | NEW |