/* Takes ownership of creds_path if not NULL. */ static grpc_error *create_default_creds_from_path( grpc_exec_ctx *exec_ctx, char *creds_path, grpc_call_credentials **creds) { grpc_json *json = NULL; grpc_auth_json_key key; grpc_auth_refresh_token token; grpc_call_credentials *result = NULL; grpc_slice creds_data = grpc_empty_slice(); grpc_error *error = GRPC_ERROR_NONE; if (creds_path == NULL) { error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("creds_path unset"); goto end; } error = grpc_load_file(creds_path, 0, &creds_data); if (error != GRPC_ERROR_NONE) { goto end; } json = grpc_json_parse_string_with_len( (char *)GRPC_SLICE_START_PTR(creds_data), GRPC_SLICE_LENGTH(creds_data)); if (json == NULL) { error = grpc_error_set_str( GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed to parse JSON"), GRPC_ERROR_STR_RAW_BYTES, grpc_slice_ref_internal(creds_data)); goto end; } /* First, try an auth json key. */ key = grpc_auth_json_key_create_from_json(json); if (grpc_auth_json_key_is_valid(&key)) { result = grpc_service_account_jwt_access_credentials_create_from_auth_json_key( exec_ctx, key, grpc_max_auth_token_lifetime()); if (result == NULL) { error = GRPC_ERROR_CREATE_FROM_STATIC_STRING( "grpc_service_account_jwt_access_credentials_create_from_auth_json_" "key failed"); } goto end; } /* Then try a refresh token if the auth json key was invalid. */ token = grpc_auth_refresh_token_create_from_json(json); if (grpc_auth_refresh_token_is_valid(&token)) { result = grpc_refresh_token_credentials_create_from_auth_refresh_token(token); if (result == NULL) { error = GRPC_ERROR_CREATE_FROM_STATIC_STRING( "grpc_refresh_token_credentials_create_from_auth_refresh_token " "failed"); } goto end; } end: GPR_ASSERT((result == NULL) + (error == GRPC_ERROR_NONE) == 1); if (creds_path != NULL) gpr_free(creds_path); grpc_slice_unref_internal(exec_ctx, creds_data); if (json != NULL) grpc_json_destroy(json); *creds = result; return error; }
static void test_parse_refresh_token_failure_no_refresh_token(void) { const char refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"type\": \"authorized_user\"}"; grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string(refresh_token_str); GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); }
static void test_parse_refresh_token_failure_no_client_secret(void) { const char refresh_token_str[] = "{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string(refresh_token_str); GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); }
static void test_parse_refresh_token_failure_no_client_id(void) { const char refresh_token_str[] = "{ \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," " \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," " \"type\": \"authorized_user\"}"; grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string(refresh_token_str); GPR_ASSERT(!grpc_auth_refresh_token_is_valid(&refresh_token)); }
grpc_credentials *grpc_refresh_token_credentials_create_from_auth_refresh_token( grpc_auth_refresh_token refresh_token) { grpc_refresh_token_credentials *c; if (!grpc_auth_refresh_token_is_valid(&refresh_token)) { gpr_log(GPR_ERROR, "Invalid input for refresh token credentials creation"); return NULL; } c = gpr_malloc(sizeof(grpc_refresh_token_credentials)); memset(c, 0, sizeof(grpc_refresh_token_credentials)); init_oauth2_token_fetcher(&c->base, refresh_token_fetch_oauth2); c->base.base.vtable = &refresh_token_vtable; c->refresh_token = refresh_token; return &c->base.base; }
static void test_parse_refresh_token_success(void) { grpc_auth_refresh_token refresh_token = grpc_auth_refresh_token_create_from_string(test_refresh_token_str); GPR_ASSERT(grpc_auth_refresh_token_is_valid(&refresh_token)); GPR_ASSERT(refresh_token.type != NULL && (strcmp(refresh_token.type, "authorized_user") == 0)); GPR_ASSERT(refresh_token.client_id != NULL && (strcmp(refresh_token.client_id, "32555999999.apps.googleusercontent.com") == 0)); GPR_ASSERT( refresh_token.client_secret != NULL && (strcmp(refresh_token.client_secret, "EmssLNjJy1332hD4KFsecret") == 0)); GPR_ASSERT(refresh_token.refresh_token != NULL && (strcmp(refresh_token.refresh_token, "1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42") == 0)); grpc_auth_refresh_token_destruct(&refresh_token); }
/* Takes ownership of creds_path if not NULL. */ static grpc_call_credentials *create_default_creds_from_path(char *creds_path) { grpc_json *json = NULL; grpc_auth_json_key key; grpc_auth_refresh_token token; grpc_call_credentials *result = NULL; gpr_slice creds_data = gpr_empty_slice(); int file_ok = 0; if (creds_path == NULL) goto end; creds_data = gpr_load_file(creds_path, 0, &file_ok); if (!file_ok) goto end; json = grpc_json_parse_string_with_len( (char *)GPR_SLICE_START_PTR(creds_data), GPR_SLICE_LENGTH(creds_data)); if (json == NULL) goto end; /* First, try an auth json key. */ key = grpc_auth_json_key_create_from_json(json); if (grpc_auth_json_key_is_valid(&key)) { result = grpc_service_account_jwt_access_credentials_create_from_auth_json_key( key, grpc_max_auth_token_lifetime()); goto end; } /* Then try a refresh token if the auth json key was invalid. */ token = grpc_auth_refresh_token_create_from_json(json); if (grpc_auth_refresh_token_is_valid(&token)) { result = grpc_refresh_token_credentials_create_from_auth_refresh_token(token); goto end; } end: if (creds_path != NULL) gpr_free(creds_path); gpr_slice_unref(creds_data); if (json != NULL) grpc_json_destroy(json); return result; }