mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.9 KiB
119 lines
3.9 KiB
10 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2015 gRPC authors.
|
||
10 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
10 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
10 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
10 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#include "test/core/security/oauth2_utils.h"
|
||
|
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <grpc/grpc.h>
|
||
|
#include <grpc/grpc_security.h>
|
||
8 years ago
|
#include <grpc/slice.h>
|
||
10 years ago
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
#include <grpc/support/sync.h>
|
||
|
|
||
9 years ago
|
#include "src/core/lib/security/credentials/credentials.h"
|
||
10 years ago
|
|
||
9 years ago
|
typedef struct {
|
||
7 years ago
|
gpr_mu* mu;
|
||
9 years ago
|
grpc_polling_entity pops;
|
||
7 years ago
|
bool is_done;
|
||
7 years ago
|
char* token;
|
||
7 years ago
|
|
||
|
grpc_credentials_mdelem_array md_array;
|
||
|
grpc_closure closure;
|
||
10 years ago
|
} oauth2_request;
|
||
10 years ago
|
|
||
7 years ago
|
static void on_oauth2_response(grpc_exec_ctx* exec_ctx, void* arg,
|
||
|
grpc_error* error) {
|
||
|
oauth2_request* request = (oauth2_request*)arg;
|
||
|
char* token = NULL;
|
||
8 years ago
|
grpc_slice token_slice;
|
||
7 years ago
|
if (error != GRPC_ERROR_NONE) {
|
||
|
gpr_log(GPR_ERROR, "Fetching token failed: %s", grpc_error_string(error));
|
||
9 years ago
|
} else {
|
||
7 years ago
|
GPR_ASSERT(request->md_array.size == 1);
|
||
|
token_slice = GRPC_MDVALUE(request->md_array.md[0]);
|
||
7 years ago
|
token = (char*)gpr_malloc(GRPC_SLICE_LENGTH(token_slice) + 1);
|
||
8 years ago
|
memcpy(token, GRPC_SLICE_START_PTR(token_slice),
|
||
|
GRPC_SLICE_LENGTH(token_slice));
|
||
|
token[GRPC_SLICE_LENGTH(token_slice)] = '\0';
|
||
9 years ago
|
}
|
||
7 years ago
|
grpc_credentials_mdelem_array_destroy(exec_ctx, &request->md_array);
|
||
9 years ago
|
gpr_mu_lock(request->mu);
|
||
7 years ago
|
request->is_done = true;
|
||
10 years ago
|
request->token = token;
|
||
9 years ago
|
GRPC_LOG_IF_ERROR(
|
||
|
"pollset_kick",
|
||
7 years ago
|
grpc_pollset_kick(exec_ctx, grpc_polling_entity_pollset(&request->pops),
|
||
|
NULL));
|
||
9 years ago
|
gpr_mu_unlock(request->mu);
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
static void do_nothing(grpc_exec_ctx* exec_ctx, void* unused,
|
||
|
grpc_error* error) {}
|
||
10 years ago
|
|
||
7 years ago
|
char* grpc_test_fetch_oauth2_token_with_credentials(
|
||
|
grpc_call_credentials* creds) {
|
||
10 years ago
|
oauth2_request request;
|
||
7 years ago
|
memset(&request, 0, sizeof(request));
|
||
9 years ago
|
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
|
||
9 years ago
|
grpc_closure do_nothing_closure;
|
||
9 years ago
|
grpc_auth_metadata_context null_ctx = {"", "", NULL, NULL};
|
||
|
|
||
7 years ago
|
grpc_pollset* pollset = (grpc_pollset*)gpr_zalloc(grpc_pollset_size());
|
||
9 years ago
|
grpc_pollset_init(pollset, &request.mu);
|
||
9 years ago
|
request.pops = grpc_polling_entity_create_from_pollset(pollset);
|
||
7 years ago
|
request.is_done = false;
|
||
10 years ago
|
|
||
8 years ago
|
GRPC_CLOSURE_INIT(&do_nothing_closure, do_nothing, NULL,
|
||
8 years ago
|
grpc_schedule_on_exec_ctx);
|
||
9 years ago
|
|
||
7 years ago
|
GRPC_CLOSURE_INIT(&request.closure, on_oauth2_response, &request,
|
||
|
grpc_schedule_on_exec_ctx);
|
||
9 years ago
|
|
||
7 years ago
|
grpc_error* error = GRPC_ERROR_NONE;
|
||
7 years ago
|
if (grpc_call_credentials_get_request_metadata(
|
||
|
&exec_ctx, creds, &request.pops, null_ctx, &request.md_array,
|
||
|
&request.closure, &error)) {
|
||
|
// Synchronous result; invoke callback directly.
|
||
|
on_oauth2_response(&exec_ctx, &request, error);
|
||
|
GRPC_ERROR_UNREF(error);
|
||
|
}
|
||
|
grpc_exec_ctx_flush(&exec_ctx);
|
||
10 years ago
|
|
||
9 years ago
|
gpr_mu_lock(request.mu);
|
||
9 years ago
|
while (!request.is_done) {
|
||
7 years ago
|
grpc_pollset_worker* worker = NULL;
|
||
9 years ago
|
if (!GRPC_LOG_IF_ERROR(
|
||
9 years ago
|
"pollset_work",
|
||
9 years ago
|
grpc_pollset_work(&exec_ctx,
|
||
|
grpc_polling_entity_pollset(&request.pops),
|
||
8 years ago
|
&worker, GRPC_MILLIS_INF_FUTURE))) {
|
||
7 years ago
|
request.is_done = true;
|
||
9 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
gpr_mu_unlock(request.mu);
|
||
10 years ago
|
|
||
9 years ago
|
grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&request.pops),
|
||
9 years ago
|
&do_nothing_closure);
|
||
9 years ago
|
grpc_exec_ctx_finish(&exec_ctx);
|
||
9 years ago
|
gpr_free(grpc_polling_entity_pollset(&request.pops));
|
||
10 years ago
|
return request.token;
|
||
10 years ago
|
}
|