@ -114,20 +114,6 @@ void grpc_credentials_get_request_metadata(grpc_credentials *creds,
creds - > vtable - > get_request_metadata ( creds , service_url , cb , user_data ) ;
creds - > vtable - > get_request_metadata ( creds , service_url , cb , user_data ) ;
}
}
grpc_mdctx * grpc_credentials_get_or_create_metadata_context (
grpc_credentials * creds ) {
grpc_mdctx * mdctx = NULL ;
if ( creds ! = NULL & & creds - > vtable - > get_metadata_context ! = NULL ) {
mdctx = creds - > vtable - > get_metadata_context ( creds ) ;
}
if ( mdctx = = NULL ) {
return grpc_mdctx_create ( ) ;
} else {
grpc_mdctx_ref ( mdctx ) ;
return mdctx ;
}
}
grpc_security_status grpc_credentials_create_security_connector (
grpc_security_status grpc_credentials_create_security_connector (
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * request_metadata_creds ,
grpc_credentials * request_metadata_creds ,
@ -208,10 +194,6 @@ static int ssl_has_request_metadata_only(const grpc_credentials *creds) {
return 0 ;
return 0 ;
}
}
static grpc_mdctx * ssl_get_metadata_context ( grpc_credentials * creds ) {
return NULL ;
}
static grpc_security_status ssl_create_security_connector (
static grpc_security_status ssl_create_security_connector (
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * request_metadata_creds ,
grpc_credentials * request_metadata_creds ,
@ -249,8 +231,8 @@ static grpc_security_status ssl_server_create_security_connector(
}
}
static grpc_credentials_vtable ssl_vtable = {
static grpc_credentials_vtable ssl_vtable = {
ssl_destroy , ssl_has_request_metadata , ssl_has_request_metadata_only ,
ssl_destroy , ssl_has_request_metadata , ssl_has_request_metadata_only , NULL ,
ssl_get_metadata_context , NULL , ssl_ create_security_connector } ;
ssl_create_security_connector } ;
static grpc_server_credentials_vtable ssl_server_vtable = {
static grpc_server_credentials_vtable ssl_server_vtable = {
ssl_server_destroy , ssl_server_create_security_connector } ;
ssl_server_destroy , ssl_server_create_security_connector } ;
@ -341,13 +323,12 @@ grpc_server_credentials *grpc_ssl_server_credentials_create(
typedef struct {
typedef struct {
grpc_credentials base ;
grpc_credentials base ;
grpc_mdctx * md_ctx ;
/* Have a simple cache for now with just 1 entry. We could have a map based on
/* Have a simple cache for now with just 1 entry. We could have a map based on
the service_url for a more sophisticated one . */
the service_url for a more sophisticated one . */
gpr_mu cache_mu ;
gpr_mu cache_mu ;
struct {
struct {
grpc_mdelem * jwt_md ;
grpc_credentials_md_store * jwt_md ;
char * service_url ;
char * service_url ;
gpr_timespec jwt_expiration ;
gpr_timespec jwt_expiration ;
} cached ;
} cached ;
@ -358,7 +339,7 @@ typedef struct {
static void jwt_reset_cache ( grpc_jwt_credentials * c ) {
static void jwt_reset_cache ( grpc_jwt_credentials * c ) {
if ( c - > cached . jwt_md ! = NULL ) {
if ( c - > cached . jwt_md ! = NULL ) {
grpc_mdelem _unref ( c - > cached . jwt_md ) ;
grpc_credentials_md_store _unref ( c - > cached . jwt_md ) ;
c - > cached . jwt_md = NULL ;
c - > cached . jwt_md = NULL ;
}
}
if ( c - > cached . service_url ! = NULL ) {
if ( c - > cached . service_url ! = NULL ) {
@ -373,7 +354,6 @@ static void jwt_destroy(grpc_credentials *creds) {
grpc_auth_json_key_destruct ( & c - > key ) ;
grpc_auth_json_key_destruct ( & c - > key ) ;
jwt_reset_cache ( c ) ;
jwt_reset_cache ( c ) ;
gpr_mu_destroy ( & c - > cache_mu ) ;
gpr_mu_destroy ( & c - > cache_mu ) ;
grpc_mdctx_unref ( c - > md_ctx ) ;
gpr_free ( c ) ;
gpr_free ( c ) ;
}
}
@ -393,7 +373,7 @@ static void jwt_get_request_metadata(grpc_credentials *creds,
0 } ;
0 } ;
/* See if we can return a cached jwt. */
/* See if we can return a cached jwt. */
grpc_mdelem * jwt_md = NULL ;
grpc_credentials_md_store * jwt_md = NULL ;
{
{
gpr_mu_lock ( & c - > cache_mu ) ;
gpr_mu_lock ( & c - > cache_mu ) ;
if ( c - > cached . service_url ! = NULL & &
if ( c - > cached . service_url ! = NULL & &
@ -401,7 +381,7 @@ static void jwt_get_request_metadata(grpc_credentials *creds,
c - > cached . jwt_md ! = NULL & &
c - > cached . jwt_md ! = NULL & &
( gpr_time_cmp ( gpr_time_sub ( c - > cached . jwt_expiration , gpr_now ( ) ) ,
( gpr_time_cmp ( gpr_time_sub ( c - > cached . jwt_expiration , gpr_now ( ) ) ,
refresh_threshold ) > 0 ) ) {
refresh_threshold ) > 0 ) ) {
jwt_md = grpc_mdelem _ref ( c - > cached . jwt_md ) ;
jwt_md = grpc_credentials_md_store _ref ( c - > cached . jwt_md ) ;
}
}
gpr_mu_unlock ( & c - > cache_mu ) ;
gpr_mu_unlock ( & c - > cache_mu ) ;
}
}
@ -418,30 +398,26 @@ static void jwt_get_request_metadata(grpc_credentials *creds,
gpr_free ( jwt ) ;
gpr_free ( jwt ) ;
c - > cached . jwt_expiration = gpr_time_add ( gpr_now ( ) , c - > jwt_lifetime ) ;
c - > cached . jwt_expiration = gpr_time_add ( gpr_now ( ) , c - > jwt_lifetime ) ;
c - > cached . service_url = gpr_strdup ( service_url ) ;
c - > cached . service_url = gpr_strdup ( service_url ) ;
c - > cached . jwt_md = grpc_mdelem_from_strings (
c - > cached . jwt_md = grpc_credentials_md_store_create ( 1 ) ;
c - > md_ctx , GRPC_AUTHORIZATION_METADATA_KEY , md_value ) ;
grpc_credentials_md_store_add_cstrings (
c - > cached . jwt_md , GRPC_AUTHORIZATION_METADATA_KEY , md_value ) ;
gpr_free ( md_value ) ;
gpr_free ( md_value ) ;
jwt_md = grpc_mdelem _ref ( c - > cached . jwt_md ) ;
jwt_md = grpc_credentials_md_store _ref ( c - > cached . jwt_md ) ;
}
}
gpr_mu_unlock ( & c - > cache_mu ) ;
gpr_mu_unlock ( & c - > cache_mu ) ;
}
}
if ( jwt_md ! = NULL ) {
if ( jwt_md ! = NULL ) {
cb ( user_data , & jwt_md , 1 , GRPC_CREDENTIALS_OK ) ;
cb ( user_data , jwt_md - > entries , jwt_md - > num_entries , GRPC_CREDENTIALS_OK ) ;
grpc_mdelem _unref ( jwt_md ) ;
grpc_credentials_md_store _unref ( jwt_md ) ;
} else {
} else {
cb ( user_data , NULL , 0 , GRPC_CREDENTIALS_ERROR ) ;
cb ( user_data , NULL , 0 , GRPC_CREDENTIALS_ERROR ) ;
}
}
}
}
static grpc_mdctx * jwt_get_metadata_context ( grpc_credentials * creds ) {
grpc_jwt_credentials * c = ( grpc_jwt_credentials * ) creds ;
return c - > md_ctx ;
}
static grpc_credentials_vtable jwt_vtable = {
static grpc_credentials_vtable jwt_vtable = {
jwt_destroy , jwt_has_request_metadata , jwt_has_request_metadata_only ,
jwt_destroy , jwt_has_request_metadata , jwt_has_request_metadata_only ,
jwt_get_metadata_context , jwt_get_ request_metadata , NULL } ;
jwt_get_request_metadata , NULL } ;
grpc_credentials * grpc_jwt_credentials_create ( const char * json_key ,
grpc_credentials * grpc_jwt_credentials_create ( const char * json_key ,
gpr_timespec token_lifetime ) {
gpr_timespec token_lifetime ) {
@ -456,7 +432,6 @@ grpc_credentials *grpc_jwt_credentials_create(const char *json_key,
c - > base . type = GRPC_CREDENTIALS_TYPE_JWT ;
c - > base . type = GRPC_CREDENTIALS_TYPE_JWT ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
c - > base . vtable = & jwt_vtable ;
c - > base . vtable = & jwt_vtable ;
c - > md_ctx = grpc_mdctx_create ( ) ;
c - > key = key ;
c - > key = key ;
c - > jwt_lifetime = token_lifetime ;
c - > jwt_lifetime = token_lifetime ;
gpr_mu_init ( & c - > cache_mu ) ;
gpr_mu_init ( & c - > cache_mu ) ;
@ -476,8 +451,7 @@ typedef void (*grpc_fetch_oauth2_func)(grpc_credentials_metadata_request *req,
typedef struct {
typedef struct {
grpc_credentials base ;
grpc_credentials base ;
gpr_mu mu ;
gpr_mu mu ;
grpc_mdctx * md_ctx ;
grpc_credentials_md_store * access_token_md ;
grpc_mdelem * access_token_md ;
gpr_timespec token_expiration ;
gpr_timespec token_expiration ;
grpc_fetch_oauth2_func fetch_func ;
grpc_fetch_oauth2_func fetch_func ;
} grpc_oauth2_token_fetcher_credentials ;
} grpc_oauth2_token_fetcher_credentials ;
@ -485,11 +459,8 @@ typedef struct {
static void oauth2_token_fetcher_destroy ( grpc_credentials * creds ) {
static void oauth2_token_fetcher_destroy ( grpc_credentials * creds ) {
grpc_oauth2_token_fetcher_credentials * c =
grpc_oauth2_token_fetcher_credentials * c =
( grpc_oauth2_token_fetcher_credentials * ) creds ;
( grpc_oauth2_token_fetcher_credentials * ) creds ;
if ( c - > access_token_md ! = NULL ) {
grpc_credentials_md_store_unref ( c - > access_token_md ) ;
grpc_mdelem_unref ( c - > access_token_md ) ;
}
gpr_mu_destroy ( & c - > mu ) ;
gpr_mu_destroy ( & c - > mu ) ;
grpc_mdctx_unref ( c - > md_ctx ) ;
gpr_free ( c ) ;
gpr_free ( c ) ;
}
}
@ -505,8 +476,8 @@ static int oauth2_token_fetcher_has_request_metadata_only(
grpc_credentials_status
grpc_credentials_status
grpc_oauth2_token_fetcher_credentials_parse_server_response (
grpc_oauth2_token_fetcher_credentials_parse_server_response (
const grpc_httpcli_response * response , grpc_mdctx * ctx ,
const grpc_httpcli_response * response ,
grpc_mdelem * * token_ele m , gpr_timespec * token_lifetime ) {
grpc_credentials_md_store * * token_md , gpr_timespec * token_lifetime ) {
char * null_terminated_body = NULL ;
char * null_terminated_body = NULL ;
char * new_access_token = NULL ;
char * new_access_token = NULL ;
grpc_credentials_status status = GRPC_CREDENTIALS_OK ;
grpc_credentials_status status = GRPC_CREDENTIALS_OK ;
@ -574,16 +545,17 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
access_token - > value ) ;
access_token - > value ) ;
token_lifetime - > tv_sec = strtol ( expires_in - > value , NULL , 10 ) ;
token_lifetime - > tv_sec = strtol ( expires_in - > value , NULL , 10 ) ;
token_lifetime - > tv_nsec = 0 ;
token_lifetime - > tv_nsec = 0 ;
if ( * token_elem ! = NULL ) grpc_mdelem_unref ( * token_elem ) ;
if ( * token_md ! = NULL ) grpc_credentials_md_store_unref ( * token_md ) ;
* token_elem = grpc_mdelem_from_strings ( ctx , GRPC_AUTHORIZATION_METADATA_KEY ,
* token_md = grpc_credentials_md_store_create ( 1 ) ;
new_access_token ) ;
grpc_credentials_md_store_add_cstrings (
* token_md , GRPC_AUTHORIZATION_METADATA_KEY , new_access_token ) ;
status = GRPC_CREDENTIALS_OK ;
status = GRPC_CREDENTIALS_OK ;
}
}
end :
end :
if ( status ! = GRPC_CREDENTIALS_OK & & ( * token_ele m ! = NULL ) ) {
if ( status ! = GRPC_CREDENTIALS_OK & & ( * token_md ! = NULL ) ) {
grpc_mdelem _unref ( * token_ele m ) ;
grpc_credentials_md_store _unref ( * token_md ) ;
* token_ele m = NULL ;
* token_md = NULL ;
}
}
if ( null_terminated_body ! = NULL ) gpr_free ( null_terminated_body ) ;
if ( null_terminated_body ! = NULL ) gpr_free ( null_terminated_body ) ;
if ( new_access_token ! = NULL ) gpr_free ( new_access_token ) ;
if ( new_access_token ! = NULL ) gpr_free ( new_access_token ) ;
@ -602,10 +574,11 @@ static void on_oauth2_token_fetcher_http_response(
gpr_mu_lock ( & c - > mu ) ;
gpr_mu_lock ( & c - > mu ) ;
status = grpc_oauth2_token_fetcher_credentials_parse_server_response (
status = grpc_oauth2_token_fetcher_credentials_parse_server_response (
response , c - > md_ctx , & c - > access_token_md , & token_lifetime ) ;
response , & c - > access_token_md , & token_lifetime ) ;
if ( status = = GRPC_CREDENTIALS_OK ) {
if ( status = = GRPC_CREDENTIALS_OK ) {
c - > token_expiration = gpr_time_add ( gpr_now ( ) , token_lifetime ) ;
c - > token_expiration = gpr_time_add ( gpr_now ( ) , token_lifetime ) ;
r - > cb ( r - > user_data , & c - > access_token_md , 1 , status ) ;
r - > cb ( r - > user_data , c - > access_token_md - > entries ,
c - > access_token_md - > num_entries , status ) ;
} else {
} else {
c - > token_expiration = gpr_inf_past ;
c - > token_expiration = gpr_inf_past ;
r - > cb ( r - > user_data , NULL , 0 , status ) ;
r - > cb ( r - > user_data , NULL , 0 , status ) ;
@ -621,19 +594,20 @@ static void oauth2_token_fetcher_get_request_metadata(
( grpc_oauth2_token_fetcher_credentials * ) creds ;
( grpc_oauth2_token_fetcher_credentials * ) creds ;
gpr_timespec refresh_threshold = { GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS ,
gpr_timespec refresh_threshold = { GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS ,
0 } ;
0 } ;
grpc_mdelem * cached_access_token_md = NULL ;
grpc_credentials_md_store * cached_access_token_md = NULL ;
{
{
gpr_mu_lock ( & c - > mu ) ;
gpr_mu_lock ( & c - > mu ) ;
if ( c - > access_token_md ! = NULL & &
if ( c - > access_token_md ! = NULL & &
( gpr_time_cmp ( gpr_time_sub ( c - > token_expiration , gpr_now ( ) ) ,
( gpr_time_cmp ( gpr_time_sub ( c - > token_expiration , gpr_now ( ) ) ,
refresh_threshold ) > 0 ) ) {
refresh_threshold ) > 0 ) ) {
cached_access_token_md = grpc_mdelem _ref ( c - > access_token_md ) ;
cached_access_token_md = grpc_credentials_md_store _ref ( c - > access_token_md ) ;
}
}
gpr_mu_unlock ( & c - > mu ) ;
gpr_mu_unlock ( & c - > mu ) ;
}
}
if ( cached_access_token_md ! = NULL ) {
if ( cached_access_token_md ! = NULL ) {
cb ( user_data , & cached_access_token_md , 1 , GRPC_CREDENTIALS_OK ) ;
cb ( user_data , cached_access_token_md - > entries ,
grpc_mdelem_unref ( cached_access_token_md ) ;
cached_access_token_md - > num_entries , GRPC_CREDENTIALS_OK ) ;
grpc_credentials_md_store_unref ( cached_access_token_md ) ;
} else {
} else {
c - > fetch_func (
c - > fetch_func (
grpc_credentials_metadata_request_create ( creds , cb , user_data ) ,
grpc_credentials_metadata_request_create ( creds , cb , user_data ) ,
@ -648,24 +622,15 @@ static void init_oauth2_token_fetcher(grpc_oauth2_token_fetcher_credentials *c,
c - > base . type = GRPC_CREDENTIALS_TYPE_OAUTH2 ;
c - > base . type = GRPC_CREDENTIALS_TYPE_OAUTH2 ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
gpr_mu_init ( & c - > mu ) ;
gpr_mu_init ( & c - > mu ) ;
c - > md_ctx = grpc_mdctx_create ( ) ;
c - > token_expiration = gpr_inf_past ;
c - > token_expiration = gpr_inf_past ;
c - > fetch_func = fetch_func ;
c - > fetch_func = fetch_func ;
}
}
static grpc_mdctx * oauth2_token_fetcher_get_metadata_context (
grpc_credentials * creds ) {
grpc_oauth2_token_fetcher_credentials * c =
( grpc_oauth2_token_fetcher_credentials * ) creds ;
return c - > md_ctx ;
}
/* -- ComputeEngine credentials. -- */
/* -- ComputeEngine credentials. -- */
static grpc_credentials_vtable compute_engine_vtable = {
static grpc_credentials_vtable compute_engine_vtable = {
oauth2_token_fetcher_destroy , oauth2_token_fetcher_has_request_metadata ,
oauth2_token_fetcher_destroy , oauth2_token_fetcher_has_request_metadata ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_get_metadata_context ,
oauth2_token_fetcher_get_request_metadata , NULL } ;
oauth2_token_fetcher_get_request_metadata , NULL } ;
static void compute_engine_fetch_oauth2 (
static void compute_engine_fetch_oauth2 (
@ -709,7 +674,6 @@ static void service_account_destroy(grpc_credentials *creds) {
static grpc_credentials_vtable service_account_vtable = {
static grpc_credentials_vtable service_account_vtable = {
service_account_destroy , oauth2_token_fetcher_has_request_metadata ,
service_account_destroy , oauth2_token_fetcher_has_request_metadata ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_get_metadata_context ,
oauth2_token_fetcher_get_request_metadata , NULL } ;
oauth2_token_fetcher_get_request_metadata , NULL } ;
static void service_account_fetch_oauth2 (
static void service_account_fetch_oauth2 (
@ -783,7 +747,6 @@ static void refresh_token_destroy(grpc_credentials *creds) {
static grpc_credentials_vtable refresh_token_vtable = {
static grpc_credentials_vtable refresh_token_vtable = {
refresh_token_destroy , oauth2_token_fetcher_has_request_metadata ,
refresh_token_destroy , oauth2_token_fetcher_has_request_metadata ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_has_request_metadata_only ,
oauth2_token_fetcher_get_metadata_context ,
oauth2_token_fetcher_get_request_metadata , NULL } ;
oauth2_token_fetcher_get_request_metadata , NULL } ;
static void refresh_token_fetch_oauth2 (
static void refresh_token_fetch_oauth2 (
@ -832,17 +795,13 @@ grpc_credentials *grpc_refresh_token_credentials_create(
typedef struct {
typedef struct {
grpc_credentials base ;
grpc_credentials base ;
grpc_mdctx * md_ctx ;
grpc_credentials_md_store * access_token_md ;
grpc_mdelem * access_token_md ;
int is_async ;
int is_async ;
} grpc_fake_oauth2_credentials ;
} grpc_fake_oauth2_credentials ;
static void fake_oauth2_destroy ( grpc_credentials * creds ) {
static void fake_oauth2_destroy ( grpc_credentials * creds ) {
grpc_fake_oauth2_credentials * c = ( grpc_fake_oauth2_credentials * ) creds ;
grpc_fake_oauth2_credentials * c = ( grpc_fake_oauth2_credentials * ) creds ;
if ( c - > access_token_md ! = NULL ) {
grpc_credentials_md_store_unref ( c - > access_token_md ) ;
grpc_mdelem_unref ( c - > access_token_md ) ;
}
grpc_mdctx_unref ( c - > md_ctx ) ;
gpr_free ( c ) ;
gpr_free ( c ) ;
}
}
@ -860,7 +819,8 @@ void on_simulated_token_fetch_done(void *user_data, int success) {
( grpc_credentials_metadata_request * ) user_data ;
( grpc_credentials_metadata_request * ) user_data ;
grpc_fake_oauth2_credentials * c = ( grpc_fake_oauth2_credentials * ) r - > creds ;
grpc_fake_oauth2_credentials * c = ( grpc_fake_oauth2_credentials * ) r - > creds ;
GPR_ASSERT ( success ) ;
GPR_ASSERT ( success ) ;
r - > cb ( r - > user_data , & c - > access_token_md , 1 , GRPC_CREDENTIALS_OK ) ;
r - > cb ( r - > user_data , c - > access_token_md - > entries ,
c - > access_token_md - > num_entries , GRPC_CREDENTIALS_OK ) ;
grpc_credentials_metadata_request_destroy ( r ) ;
grpc_credentials_metadata_request_destroy ( r ) ;
}
}
@ -875,19 +835,14 @@ static void fake_oauth2_get_request_metadata(grpc_credentials *creds,
on_simulated_token_fetch_done ,
on_simulated_token_fetch_done ,
grpc_credentials_metadata_request_create ( creds , cb , user_data ) ) ;
grpc_credentials_metadata_request_create ( creds , cb , user_data ) ) ;
} else {
} else {
cb ( user_data , & c - > access_token_md , 1 , GRPC_CREDENTIALS_OK ) ;
cb ( user_data , c - > access_token_md - > entries , 1 , GRPC_CREDENTIALS_OK ) ;
}
}
}
}
static grpc_mdctx * fake_oauth2_get_metadata_context ( grpc_credentials * creds ) {
grpc_fake_oauth2_credentials * c = ( grpc_fake_oauth2_credentials * ) creds ;
return c - > md_ctx ;
}
static grpc_credentials_vtable fake_oauth2_vtable = {
static grpc_credentials_vtable fake_oauth2_vtable = {
fake_oauth2_destroy , fake_oauth2_has_request_metadata ,
fake_oauth2_destroy , fake_oauth2_has_request_metadata ,
fake_oauth2_has_request_metadata_only , fake_oauth2_get_metadata_context ,
fake_oauth2_has_request_metadata_only , fake_oauth2_get_request_metadata ,
fake_oauth2_get_request_metadata , NULL } ;
NULL } ;
grpc_credentials * grpc_fake_oauth2_credentials_create (
grpc_credentials * grpc_fake_oauth2_credentials_create (
const char * token_md_value , int is_async ) {
const char * token_md_value , int is_async ) {
@ -897,9 +852,9 @@ grpc_credentials *grpc_fake_oauth2_credentials_create(
c - > base . type = GRPC_CREDENTIALS_TYPE_OAUTH2 ;
c - > base . type = GRPC_CREDENTIALS_TYPE_OAUTH2 ;
c - > base . vtable = & fake_oauth2_vtable ;
c - > base . vtable = & fake_oauth2_vtable ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
c - > md_ctx = grpc_mdctx_create ( ) ;
c - > access_token_ md = grpc_credentials_md_store_create ( 1 ) ;
c - > access_token_md = grpc_mdelem_from_ strings(
grpc_credentials_md_store_add_c strings(
c - > md_ctx , GRPC_AUTHORIZATION_METADATA_KEY , token_md_value ) ;
c - > access_token_ md, GRPC_AUTHORIZATION_METADATA_KEY , token_md_value ) ;
c - > is_async = is_async ;
c - > is_async = is_async ;
return & c - > base ;
return & c - > base ;
}
}
@ -926,11 +881,6 @@ static int fake_transport_security_has_request_metadata_only(
return 0 ;
return 0 ;
}
}
static grpc_mdctx * fake_transport_security_get_metadata_context (
grpc_credentials * c ) {
return NULL ;
}
static grpc_security_status
static grpc_security_status
fake_transport_security_create_security_connector (
fake_transport_security_create_security_connector (
grpc_credentials * c , const char * target , const grpc_channel_args * args ,
grpc_credentials * c , const char * target , const grpc_channel_args * args ,
@ -950,8 +900,7 @@ fake_transport_security_server_create_security_connector(
static grpc_credentials_vtable fake_transport_security_credentials_vtable = {
static grpc_credentials_vtable fake_transport_security_credentials_vtable = {
fake_transport_security_credentials_destroy ,
fake_transport_security_credentials_destroy ,
fake_transport_security_has_request_metadata ,
fake_transport_security_has_request_metadata ,
fake_transport_security_has_request_metadata_only ,
fake_transport_security_has_request_metadata_only , NULL ,
fake_transport_security_get_metadata_context , NULL ,
fake_transport_security_create_security_connector } ;
fake_transport_security_create_security_connector } ;
static grpc_server_credentials_vtable
static grpc_server_credentials_vtable
@ -988,8 +937,7 @@ typedef struct {
typedef struct {
typedef struct {
grpc_composite_credentials * composite_creds ;
grpc_composite_credentials * composite_creds ;
size_t creds_index ;
size_t creds_index ;
grpc_mdelem * * md_elems ;
grpc_credentials_md_store * md_elems ;
size_t num_md ;
char * service_url ;
char * service_url ;
void * user_data ;
void * user_data ;
grpc_credentials_metadata_cb cb ;
grpc_credentials_metadata_cb cb ;
@ -1031,21 +979,16 @@ static int composite_has_request_metadata_only(const grpc_credentials *creds) {
static void composite_md_context_destroy (
static void composite_md_context_destroy (
grpc_composite_credentials_metadata_context * ctx ) {
grpc_composite_credentials_metadata_context * ctx ) {
size_t i ;
grpc_credentials_md_store_unref ( ctx - > md_elems ) ;
for ( i = 0 ; i < ctx - > num_md ; i + + ) {
grpc_mdelem_unref ( ctx - > md_elems [ i ] ) ;
}
gpr_free ( ctx - > md_elems ) ;
if ( ctx - > service_url ! = NULL ) gpr_free ( ctx - > service_url ) ;
if ( ctx - > service_url ! = NULL ) gpr_free ( ctx - > service_url ) ;
gpr_free ( ctx ) ;
gpr_free ( ctx ) ;
}
}
static void composite_metadata_cb ( void * user_data , grpc_mdelem * * md_elems ,
static void composite_metadata_cb ( void * user_data ,
size_t num_md ,
grpc_credentials_md * md_elems , size_t num_md ,
grpc_credentials_status status ) {
grpc_credentials_status status ) {
grpc_composite_credentials_metadata_context * ctx =
grpc_composite_credentials_metadata_context * ctx =
( grpc_composite_credentials_metadata_context * ) user_data ;
( grpc_composite_credentials_metadata_context * ) user_data ;
size_t i ;
if ( status ! = GRPC_CREDENTIALS_OK ) {
if ( status ! = GRPC_CREDENTIALS_OK ) {
ctx - > cb ( ctx - > user_data , NULL , 0 , status ) ;
ctx - > cb ( ctx - > user_data , NULL , 0 , status ) ;
return ;
return ;
@ -1053,12 +996,11 @@ static void composite_metadata_cb(void *user_data, grpc_mdelem **md_elems,
/* Copy the metadata in the context. */
/* Copy the metadata in the context. */
if ( num_md > 0 ) {
if ( num_md > 0 ) {
ctx - > md_elems = gpr_realloc ( ctx - > md_elems ,
size_t i ;
( ctx - > num_md + num_md ) * sizeof ( grpc_mdelem * ) ) ;
for ( i = 0 ; i < num_md ; i + + ) {
for ( i = 0 ; i < num_md ; i + + ) {
ctx - > md_elems [ i + ctx - > num_md ] = grpc_mdelem_ref ( md_elems [ i ] ) ;
grpc_credentials_md_store_add ( ctx - > md_elems , md_elems [ i ] . key ,
md_elems [ i ] . value ) ;
}
}
ctx - > num_md + = num_md ;
}
}
/* See if we need to get some more metadata. */
/* See if we need to get some more metadata. */
@ -1073,7 +1015,8 @@ static void composite_metadata_cb(void *user_data, grpc_mdelem **md_elems,
}
}
/* We're done!. */
/* We're done!. */
ctx - > cb ( ctx - > user_data , ctx - > md_elems , ctx - > num_md , GRPC_CREDENTIALS_OK ) ;
ctx - > cb ( ctx - > user_data , ctx - > md_elems - > entries , ctx - > md_elems - > num_entries ,
GRPC_CREDENTIALS_OK ) ;
composite_md_context_destroy ( ctx ) ;
composite_md_context_destroy ( ctx ) ;
}
}
@ -1093,6 +1036,7 @@ static void composite_get_request_metadata(grpc_credentials *creds,
ctx - > user_data = user_data ;
ctx - > user_data = user_data ;
ctx - > cb = cb ;
ctx - > cb = cb ;
ctx - > composite_creds = c ;
ctx - > composite_creds = c ;
ctx - > md_elems = grpc_credentials_md_store_create ( c - > inner . num_creds ) ;
while ( ctx - > creds_index < c - > inner . num_creds ) {
while ( ctx - > creds_index < c - > inner . num_creds ) {
grpc_credentials * inner_creds = c - > inner . creds_array [ ctx - > creds_index + + ] ;
grpc_credentials * inner_creds = c - > inner . creds_array [ ctx - > creds_index + + ] ;
if ( grpc_credentials_has_request_metadata ( inner_creds ) ) {
if ( grpc_credentials_has_request_metadata ( inner_creds ) ) {
@ -1104,25 +1048,6 @@ static void composite_get_request_metadata(grpc_credentials *creds,
GPR_ASSERT ( 0 ) ; /* Should have exited before. */
GPR_ASSERT ( 0 ) ; /* Should have exited before. */
}
}
static grpc_mdctx * composite_get_metadata_context ( grpc_credentials * creds ) {
grpc_composite_credentials * c = ( grpc_composite_credentials * ) creds ;
grpc_mdctx * ctx = NULL ;
size_t i ;
for ( i = 0 ; i < c - > inner . num_creds ; i + + ) {
grpc_credentials * inner_creds = c - > inner . creds_array [ i ] ;
grpc_mdctx * inner_ctx = NULL ;
if ( inner_creds - > vtable - > get_metadata_context ! = NULL ) {
inner_ctx = inner_creds - > vtable - > get_metadata_context ( inner_creds ) ;
}
if ( inner_ctx ) {
GPR_ASSERT ( ctx = = NULL & &
" can only have one metadata context per composite credential " ) ;
ctx = inner_ctx ;
}
}
return ctx ;
}
static grpc_security_status composite_create_security_connector (
static grpc_security_status composite_create_security_connector (
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * creds , const char * target , const grpc_channel_args * args ,
grpc_credentials * request_metadata_creds ,
grpc_credentials * request_metadata_creds ,
@ -1139,8 +1064,8 @@ static grpc_security_status composite_create_security_connector(
static grpc_credentials_vtable composite_credentials_vtable = {
static grpc_credentials_vtable composite_credentials_vtable = {
composite_destroy , composite_has_request_metadata ,
composite_destroy , composite_has_request_metadata ,
composite_has_request_metadata_only , composite_get_metadata_context ,
composite_has_request_metadata_only , composite_get_request_ metadata ,
composite_get_request_metadata , composite_ create_security_connector } ;
composite_create_security_connector } ;
static grpc_credentials_array get_creds_array ( grpc_credentials * * creds_addr ) {
static grpc_credentials_array get_creds_array ( grpc_credentials * * creds_addr ) {
grpc_credentials_array result ;
grpc_credentials_array result ;
@ -1237,16 +1162,12 @@ grpc_credentials *grpc_credentials_contains_type(
typedef struct {
typedef struct {
grpc_credentials base ;
grpc_credentials base ;
grpc_mdctx * md_ctx ;
grpc_credentials_md_store * iam_md ;
grpc_mdelem * token_md ;
grpc_mdelem * authority_selector_md ;
} grpc_iam_credentials ;
} grpc_iam_credentials ;
static void iam_destroy ( grpc_credentials * creds ) {
static void iam_destroy ( grpc_credentials * creds ) {
grpc_iam_credentials * c = ( grpc_iam_credentials * ) creds ;
grpc_iam_credentials * c = ( grpc_iam_credentials * ) creds ;
grpc_mdelem_unref ( c - > token_md ) ;
grpc_credentials_md_store_unref ( c - > iam_md ) ;
grpc_mdelem_unref ( c - > authority_selector_md ) ;
grpc_mdctx_unref ( c - > md_ctx ) ;
gpr_free ( c ) ;
gpr_free ( c ) ;
}
}
@ -1263,20 +1184,13 @@ static void iam_get_request_metadata(grpc_credentials *creds,
grpc_credentials_metadata_cb cb ,
grpc_credentials_metadata_cb cb ,
void * user_data ) {
void * user_data ) {
grpc_iam_credentials * c = ( grpc_iam_credentials * ) creds ;
grpc_iam_credentials * c = ( grpc_iam_credentials * ) creds ;
grpc_mdelem * md_array [ 2 ] ;
cb ( user_data , c - > iam_md - > entries , c - > iam_md - > num_entries ,
md_array [ 0 ] = c - > token_md ;
GRPC_CREDENTIALS_OK ) ;
md_array [ 1 ] = c - > authority_selector_md ;
cb ( user_data , md_array , 2 , GRPC_CREDENTIALS_OK ) ;
}
static grpc_mdctx * iam_get_metadata_context ( grpc_credentials * creds ) {
grpc_iam_credentials * c = ( grpc_iam_credentials * ) creds ;
return c - > md_ctx ;
}
}
static grpc_credentials_vtable iam_vtable = {
static grpc_credentials_vtable iam_vtable = {
iam_destroy , iam_has_request_metadata , iam_has_request_metadata_only ,
iam_destroy , iam_has_request_metadata , iam_has_request_metadata_only ,
iam_get_metadata_context , iam_get_ request_metadata , NULL } ;
iam_get_request_metadata , NULL } ;
grpc_credentials * grpc_iam_credentials_create ( const char * token ,
grpc_credentials * grpc_iam_credentials_create ( const char * token ,
const char * authority_selector ) {
const char * authority_selector ) {
@ -1288,10 +1202,10 @@ grpc_credentials *grpc_iam_credentials_create(const char *token,
c - > base . type = GRPC_CREDENTIALS_TYPE_IAM ;
c - > base . type = GRPC_CREDENTIALS_TYPE_IAM ;
c - > base . vtable = & iam_vtable ;
c - > base . vtable = & iam_vtable ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
gpr_ref_init ( & c - > base . refcount , 1 ) ;
c - > md_ctx = grpc_mdctx_create ( ) ;
c - > iam_md = grpc_credentials_md_store_create ( 2 ) ;
c - > token_md = grpc_mdelem_from_ strings(
grpc_credentials_md_store_add_c strings(
c - > md_ctx , GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY , token ) ;
c - > iam_md , GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY , token ) ;
c - > authority_selector_md = grpc_mdelem_from_ strings(
grpc_credentials_md_store_add_c strings(
c - > md_ctx , GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY , authority_selector ) ;
c - > iam_md , GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY , authority_selector ) ;
return & c - > base ;
return & c - > base ;
}
}