Ejemplo n.º 1
0
static void on_oauth2_token_fetcher_http_response(
    grpc_exec_ctx *exec_ctx, void *user_data,
    const grpc_httpcli_response *response) {
  grpc_credentials_metadata_request *r =
      (grpc_credentials_metadata_request *)user_data;
  grpc_oauth2_token_fetcher_credentials *c =
      (grpc_oauth2_token_fetcher_credentials *)r->creds;
  gpr_timespec token_lifetime;
  grpc_credentials_status status;

  gpr_mu_lock(&c->mu);
  status = grpc_oauth2_token_fetcher_credentials_parse_server_response(
      response, &c->access_token_md, &token_lifetime);
  if (status == GRPC_CREDENTIALS_OK) {
    c->token_expiration =
        gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), token_lifetime);
    r->cb(exec_ctx, r->user_data, c->access_token_md->entries,
          c->access_token_md->num_entries, status);
  } else {
    c->token_expiration = gpr_inf_past(GPR_CLOCK_REALTIME);
    r->cb(exec_ctx, r->user_data, NULL, 0, status);
  }
  gpr_mu_unlock(&c->mu);
  grpc_credentials_metadata_request_destroy(r);
}
Ejemplo n.º 2
0
static void test_oauth2_token_fetcher_creds_parsing_empty_http_body(void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response = http_response(200, "");
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) ==
               GRPC_CREDENTIALS_ERROR);
}
Ejemplo n.º 3
0
static void test_oauth2_token_fetcher_creds_parsing_bad_http_status(void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response =
        http_response(401, valid_oauth2_json_response);
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) ==
               GRPC_CREDENTIALS_ERROR);
}
Ejemplo n.º 4
0
static void test_oauth2_token_fetcher_creds_parsing_missing_token(void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response = http_response(200,
                                     "{"
                                     " \"expires_in\":3599, "
                                     " \"token_type\":\"Bearer\"}");
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) ==
               GRPC_CREDENTIALS_ERROR);
}
Ejemplo n.º 5
0
static void test_oauth2_token_fetcher_creds_parsing_missing_token_lifetime(
    void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response =
        http_response(200,
                      "{\"access_token\":\"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_\","
                      " \"token_type\":\"Bearer\"}");
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) ==
               GRPC_CREDENTIALS_ERROR);
}
Ejemplo n.º 6
0
static void test_oauth2_token_fetcher_creds_parsing_ok(void) {
    grpc_credentials_md_store *token_md = NULL;
    gpr_timespec token_lifetime;
    grpc_httpcli_response response =
        http_response(200, valid_oauth2_json_response);
    GPR_ASSERT(grpc_oauth2_token_fetcher_credentials_parse_server_response(
                   &response, &token_md, &token_lifetime) == GRPC_CREDENTIALS_OK);
    GPR_ASSERT(token_lifetime.tv_sec == 3599);
    GPR_ASSERT(token_lifetime.tv_nsec == 0);
    GPR_ASSERT(token_md->num_entries == 1);
    GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].key, "Authorization") == 0);
    GPR_ASSERT(gpr_slice_str_cmp(token_md->entries[0].value,
                                 "Bearer ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_") ==
               0);
    grpc_credentials_md_store_unref(token_md);
}