grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json( const grpc_json *json) { grpc_auth_refresh_token result; 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."); 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 (!success) grpc_auth_refresh_token_destruct(&result); 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; }