static void test_pairs() { unsigned i; for (i = 0; i < GPR_ARRAY_SIZE(testing_pairs); i++) { testing_pair *pair = testing_pairs + i; char *scratchpad = gpr_strdup(pair->input); grpc_json *json; gpr_log(GPR_INFO, "parsing string %i - should %s", i, pair->output ? "succeed" : "fail"); json = grpc_json_parse_string(scratchpad); if (pair->output) { char *output; GPR_ASSERT(json); output = grpc_json_dump_to_string(json, 0); GPR_ASSERT(output); gpr_log(GPR_INFO, "succeeded with output = %s", output); GPR_ASSERT(strcmp(output, pair->output) == 0); grpc_json_destroy(json); gpr_free(output); } else { gpr_log(GPR_INFO, "failed"); GPR_ASSERT(!json); } gpr_free(scratchpad); } }
grpc_auth_json_key grpc_auth_json_key_create_from_string( const char *json_string) { char *scratchpad = gpr_strdup(json_string); grpc_json *json = grpc_json_parse_string(scratchpad); grpc_auth_json_key result = grpc_auth_json_key_create_from_json(json); if (json != NULL) grpc_json_destroy(json); gpr_free(scratchpad); return result; }
grpc_auth_json_key grpc_auth_json_key_create_from_string( const char *json_string) { grpc_auth_json_key result; char *scratchpad = gpr_strdup(json_string); grpc_json *json = grpc_json_parse_string(scratchpad); BIO *bio = NULL; const char *prop_value; int success = 0; memset(&result, 0, sizeof(grpc_auth_json_key)); result.type = GRPC_AUTH_JSON_TYPE_INVALID; if (json == NULL) { gpr_log(GPR_ERROR, "Invalid json string %s", json_string); goto end; } prop_value = json_get_string_property(json, "type"); if (prop_value == NULL || strcmp(prop_value, GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT)) { goto end; } result.type = GRPC_AUTH_JSON_TYPE_SERVICE_ACCOUNT; if (!set_json_key_string_property(json, "private_key_id", &result.private_key_id) || !set_json_key_string_property(json, "client_id", &result.client_id) || !set_json_key_string_property(json, "client_email", &result.client_email)) { goto end; } prop_value = json_get_string_property(json, "private_key"); if (prop_value == NULL) { goto end; } bio = BIO_new(BIO_s_mem()); success = BIO_puts(bio, prop_value); if ((success < 0) || ((size_t)success != strlen(prop_value))) { gpr_log(GPR_ERROR, "Could not write into openssl BIO."); goto end; } result.private_key = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, ""); if (result.private_key == NULL) { gpr_log(GPR_ERROR, "Could not deserialize private key."); goto end; } success = 1; end: if (bio != NULL) BIO_free(bio); if (json != NULL) grpc_json_destroy(json); if (!success) grpc_auth_json_key_destruct(&result); gpr_free(scratchpad); return result; }
grpc_service_config* grpc_service_config_create(const char* json_string) { grpc_service_config* service_config = gpr_malloc(sizeof(*service_config)); service_config->json_string = gpr_strdup(json_string); service_config->json_tree = grpc_json_parse_string(service_config->json_string); if (service_config->json_tree == NULL) { gpr_log(GPR_INFO, "failed to parse JSON for service config"); gpr_free(service_config->json_string); gpr_free(service_config); return NULL; } return service_config; }
static void test_atypical() { char *scratchpad = gpr_strdup("[[],[]]"); grpc_json *json = grpc_json_parse_string(scratchpad); grpc_json *brother; GPR_ASSERT(json); GPR_ASSERT(json->child); brother = json->child->next; grpc_json_destroy(json->child); json->child = brother; grpc_json_destroy(json); gpr_free(scratchpad); }
static grpc_json *parse_json_part_from_jwt(const char *str, size_t len, char **scratchpad) { char *b64; char *decoded; grpc_json *json; gpr_slice slice; b64 = gpr_malloc(len + 1); strncpy(b64, str, len); b64[len] = '\0'; slice = grpc_base64_decode(b64, 1); GPR_ASSERT(!GPR_SLICE_IS_EMPTY(slice)); decoded = gpr_malloc(GPR_SLICE_LENGTH(slice) + 1); strncpy(decoded, (const char *)GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); decoded[GPR_SLICE_LENGTH(slice)] = '\0'; json = grpc_json_parse_string(decoded); gpr_free(b64); *scratchpad = decoded; gpr_slice_unref(slice); return json; }
static char *redact_private_key(const char *json_key) { char *json_copy = gpr_strdup(json_key); grpc_json *json = grpc_json_parse_string(json_copy); if (!json) { gpr_free(json_copy); return gpr_strdup("<Json failed to parse.>"); } const char *redacted = "<redacted>"; grpc_json *current = json->child; while (current) { if (current->type == GRPC_JSON_STRING && strcmp(current->key, "private_key") == 0) { current->value = (char *)redacted; break; } current = current->next; } char *clean_json = grpc_json_dump_to_string(json, 2); gpr_free(json_copy); grpc_json_destroy(json); return clean_json; }
grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string( const char *json_string) { grpc_auth_refresh_token result; char *scratchpad = gpr_strdup(json_string); grpc_json *json = grpc_json_parse_string(scratchpad); const char *prop_value; int success = 0; memset(&result, 0, sizeof(grpc_auth_refresh_token)); result.type = GRPC_AUTH_JSON_TYPE_INVALID; if (json == NULL) { gpr_log(GPR_ERROR, "Invalid json string %s", json_string); goto end; } prop_value = json_get_string_property(json, "type"); if (prop_value == NULL || strcmp(prop_value, GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER)) { goto end; } result.type = GRPC_AUTH_JSON_TYPE_AUTHORIZED_USER; if (!set_json_key_string_property(json, "client_secret", &result.client_secret) || !set_json_key_string_property(json, "client_id", &result.client_id) || !set_json_key_string_property(json, "refresh_token", &result.refresh_token)) { goto end; } success = 1; end: if (json != NULL) grpc_json_destroy(json); if (!success) grpc_auth_refresh_token_destruct(&result); gpr_free(scratchpad); return result; }
grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response( const grpc_httpcli_response *response, grpc_credentials_md_store **token_md, gpr_timespec *token_lifetime) { char *null_terminated_body = NULL; char *new_access_token = NULL; grpc_credentials_status status = GRPC_CREDENTIALS_OK; grpc_json *json = NULL; if (response == NULL) { gpr_log(GPR_ERROR, "Received NULL response."); status = GRPC_CREDENTIALS_ERROR; goto end; } if (response->body_length > 0) { null_terminated_body = gpr_malloc(response->body_length + 1); null_terminated_body[response->body_length] = '\0'; memcpy(null_terminated_body, response->body, response->body_length); } if (response->status != 200) { gpr_log(GPR_ERROR, "Call to http server ended with error %d [%s].", response->status, null_terminated_body != NULL ? null_terminated_body : ""); status = GRPC_CREDENTIALS_ERROR; goto end; } else { grpc_json *access_token = NULL; grpc_json *token_type = NULL; grpc_json *expires_in = NULL; grpc_json *ptr; json = grpc_json_parse_string(null_terminated_body); if (json == NULL) { gpr_log(GPR_ERROR, "Could not parse JSON from %s", null_terminated_body); status = GRPC_CREDENTIALS_ERROR; goto end; } if (json->type != GRPC_JSON_OBJECT) { gpr_log(GPR_ERROR, "Response should be a JSON object"); status = GRPC_CREDENTIALS_ERROR; goto end; } for (ptr = json->child; ptr; ptr = ptr->next) { if (strcmp(ptr->key, "access_token") == 0) { access_token = ptr; } else if (strcmp(ptr->key, "token_type") == 0) { token_type = ptr; } else if (strcmp(ptr->key, "expires_in") == 0) { expires_in = ptr; } } if (access_token == NULL || access_token->type != GRPC_JSON_STRING) { gpr_log(GPR_ERROR, "Missing or invalid access_token in JSON."); status = GRPC_CREDENTIALS_ERROR; goto end; } if (token_type == NULL || token_type->type != GRPC_JSON_STRING) { gpr_log(GPR_ERROR, "Missing or invalid token_type in JSON."); status = GRPC_CREDENTIALS_ERROR; goto end; } if (expires_in == NULL || expires_in->type != GRPC_JSON_NUMBER) { gpr_log(GPR_ERROR, "Missing or invalid expires_in in JSON."); status = GRPC_CREDENTIALS_ERROR; goto end; } gpr_asprintf(&new_access_token, "%s %s", token_type->value, access_token->value); token_lifetime->tv_sec = strtol(expires_in->value, NULL, 10); token_lifetime->tv_nsec = 0; if (*token_md != NULL) grpc_credentials_md_store_unref(*token_md); *token_md = grpc_credentials_md_store_create(1); grpc_credentials_md_store_add_cstrings( *token_md, GRPC_AUTHORIZATION_METADATA_KEY, new_access_token); status = GRPC_CREDENTIALS_OK; } end: if (status != GRPC_CREDENTIALS_OK && (*token_md != NULL)) { grpc_credentials_md_store_unref(*token_md); *token_md = NULL; } if (null_terminated_body != NULL) gpr_free(null_terminated_body); if (new_access_token != NULL) gpr_free(new_access_token); if (json != NULL) grpc_json_destroy(json); return status; }