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.
105 lines
3.3 KiB
105 lines
3.3 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 <stdio.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/cmdline.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
#include <grpc/support/sync.h>
|
||
|
|
||
9 years ago
|
#include "src/core/lib/iomgr/load_file.h"
|
||
9 years ago
|
#include "src/core/lib/security/credentials/credentials.h"
|
||
10 years ago
|
#include "test/core/security/oauth2_utils.h"
|
||
10 years ago
|
|
||
9 years ago
|
static grpc_call_credentials *create_refresh_token_creds(
|
||
10 years ago
|
const char *json_refresh_token_file_path) {
|
||
8 years ago
|
grpc_slice refresh_token;
|
||
9 years ago
|
GPR_ASSERT(GRPC_LOG_IF_ERROR(
|
||
|
"load_file",
|
||
9 years ago
|
grpc_load_file(json_refresh_token_file_path, 1, &refresh_token)));
|
||
9 years ago
|
return grpc_google_refresh_token_credentials_create(
|
||
8 years ago
|
(const char *)GRPC_SLICE_START_PTR(refresh_token), NULL);
|
||
10 years ago
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
9 years ago
|
grpc_call_credentials *creds = NULL;
|
||
10 years ago
|
char *json_key_file_path = NULL;
|
||
7 years ago
|
const char *json_refresh_token_file_path = NULL;
|
||
10 years ago
|
char *token = NULL;
|
||
10 years ago
|
int use_gce = 0;
|
||
|
char *scope = NULL;
|
||
|
gpr_cmdline *cl = gpr_cmdline_create("fetch_oauth2");
|
||
10 years ago
|
gpr_cmdline_add_string(cl, "json_refresh_token",
|
||
9 years ago
|
"File path of the json refresh token.",
|
||
10 years ago
|
&json_refresh_token_file_path);
|
||
10 years ago
|
gpr_cmdline_add_flag(
|
||
|
cl, "gce",
|
||
|
"Get a token from the GCE metadata server (only works in GCE).",
|
||
|
&use_gce);
|
||
|
gpr_cmdline_parse(cl, argc, argv);
|
||
|
|
||
|
grpc_init();
|
||
|
|
||
10 years ago
|
if (json_key_file_path != NULL && json_refresh_token_file_path != NULL) {
|
||
|
gpr_log(GPR_ERROR,
|
||
|
"--json_key and --json_refresh_token are mutually exclusive.");
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (use_gce) {
|
||
|
if (json_key_file_path != NULL || scope != NULL) {
|
||
|
gpr_log(GPR_INFO,
|
||
|
"Ignoring json key and scope to get a token from the GCE "
|
||
|
"metadata server.");
|
||
|
}
|
||
9 years ago
|
creds = grpc_google_compute_engine_credentials_create(NULL);
|
||
10 years ago
|
if (creds == NULL) {
|
||
|
gpr_log(GPR_ERROR, "Could not create gce credentials.");
|
||
|
exit(1);
|
||
|
}
|
||
10 years ago
|
} else if (json_refresh_token_file_path != NULL) {
|
||
|
creds = create_refresh_token_creds(json_refresh_token_file_path);
|
||
|
if (creds == NULL) {
|
||
|
gpr_log(GPR_ERROR,
|
||
|
"Could not create refresh token creds. %s does probably not "
|
||
|
"contain a valid json refresh token.",
|
||
|
json_refresh_token_file_path);
|
||
|
exit(1);
|
||
|
}
|
||
10 years ago
|
} else {
|
||
9 years ago
|
gpr_log(GPR_ERROR, "Missing --gce or --json_refresh_token option.");
|
||
|
exit(1);
|
||
10 years ago
|
}
|
||
|
GPR_ASSERT(creds != NULL);
|
||
|
|
||
10 years ago
|
token = grpc_test_fetch_oauth2_token_with_credentials(creds);
|
||
|
if (token != NULL) {
|
||
|
printf("Got token: %s.\n", token);
|
||
|
gpr_free(token);
|
||
|
}
|
||
9 years ago
|
grpc_call_credentials_release(creds);
|
||
10 years ago
|
gpr_cmdline_destroy(cl);
|
||
|
grpc_shutdown();
|
||
|
return 0;
|
||
10 years ago
|
}
|