@@ -42,8 +42,7 @@ struct http_data {
4242};
4343
4444#if defined(HAVE_OPENSSL )
45- char * encoded_token (const char * token_uri , const char * client_email , const char * private_key_id , int * token_length ) {
46- time_t now = time (NULL );
45+ char * encoded_token (const char * token_uri , const char * client_email , const char * private_key_id , int * token_length , time_t now ) {
4746 time_t then = now + 3600 ;
4847 int tlength = 1 + snprintf (NULL , 0 , "{\"typ\":\"JWT\",\"alg\":\"RS256\",\"kid\":\"%s\"}" , private_key_id );
4948 int payload_length = 1 + snprintf (NULL , 0 , "{\"iat\":\"%ld\",\"exp\":\"%ld\",\"iss\":\"%s\",\"aud\":\"%s\",\"scope\":\"https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/devstorage.read_only https://www.googleapis.com/auth/devstorage.read_write\"}" , now , then , client_email ,token_uri );
@@ -93,7 +92,8 @@ switch_status_t gcs_refresh_authorization (http_profile_t *profile)
9392 char content [GCS_SIGNATURE_LENGTH_MAX ];
9493 char * signature_url_encoded = NULL ;
9594 time_t exp ;
96- token = encoded_token (profile -> region , profile -> gcs_email , profile -> aws_s3_access_key_id , & token_length );
95+ time_t now = time (NULL );
96+ token = encoded_token (profile -> region , profile -> gcs_email , profile -> aws_s3_access_key_id , & token_length , now );
9797 encoded = malloc (sizeof (char ) * 343 );
9898 signtoken (token , token_length , profile -> secret_access_key , encoded );
9999 assertion = malloc (sizeof (char ) * (1 + token_length + 343 ));
@@ -102,14 +102,11 @@ switch_status_t gcs_refresh_authorization (http_profile_t *profile)
102102 signature_url_encoded = switch_string_replace (assertion , "+" , "%2B" );
103103 sprintf (content ,"%s%s" , "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" , signature_url_encoded );
104104 auth = gcs_auth_request (content , profile -> region );
105- if (profile -> gcs_credentials != NULL ) {
106- free (profile -> gcs_credentials );
107- }
108105 profile -> gcs_credentials = auth ;
109- exp = time ( NULL ) + 3540 ;
106+ exp = now + 3540 ;
110107 profile -> expires = exp ;
111108 switch_log_printf (SWITCH_CHANNEL_LOG , SWITCH_LOG_DEBUG , "Credentials Expries Unix Time: %ld" , exp );
112- free (assertion );
109+ switch_safe_free (assertion );
113110 return SWITCH_STATUS_SUCCESS ;
114111}
115112#endif
@@ -190,19 +187,26 @@ switch_status_t gcs_config_profile(switch_xml_t xml, http_profile_t *profile,swi
190187 return status ;
191188 }
192189 json = cJSON_Parse (contents );
193-
194- jsonstr = cJSON_GetObjectItem (json ,"private_key_id" )-> valuestring ;
195- profile -> aws_s3_access_key_id = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
196- strcpy (profile -> aws_s3_access_key_id , jsonstr );
197- jsonstr = cJSON_GetObjectItem (json ,"private_key" )-> valuestring ;
198- profile -> secret_access_key = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
199- strcpy (profile -> secret_access_key , jsonstr );
200- jsonstr = cJSON_GetObjectItem (json ,"client_email" )-> valuestring ;
201- profile -> gcs_email = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
202- strcpy (profile -> gcs_email , jsonstr );
203- jsonstr = cJSON_GetObjectItem (json ,"token_uri" )-> valuestring ;
204- profile -> region = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
205- strcpy (profile -> region , jsonstr );
190+ if (cJSON_GetObjectItem (json ,"private_key_id" ) != NULL ) {
191+ jsonstr = cJSON_GetObjectItem (json ,"private_key_id" )-> valuestring ;
192+ profile -> aws_s3_access_key_id = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
193+ strcpy (profile -> aws_s3_access_key_id , jsonstr );
194+ }
195+ if (cJSON_GetObjectItem (json ,"private_key" ) != NULL ) {
196+ jsonstr = cJSON_GetObjectItem (json ,"private_key" )-> valuestring ;
197+ profile -> secret_access_key = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
198+ strcpy (profile -> secret_access_key , jsonstr );
199+ }
200+ if (cJSON_GetObjectItem (json ,"client_email" ) != NULL ) {
201+ jsonstr = cJSON_GetObjectItem (json ,"client_email" )-> valuestring ;
202+ profile -> gcs_email = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
203+ strcpy (profile -> gcs_email , jsonstr );
204+ }
205+ if (cJSON_GetObjectItem (json ,"token_uri" ) != NULL ) {
206+ jsonstr = cJSON_GetObjectItem (json ,"token_uri" )-> valuestring ;
207+ profile -> region = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
208+ strcpy (profile -> region , jsonstr );
209+ }
206210 cJSON_Delete (json );
207211 free (contents );
208212 } else {
@@ -297,21 +301,24 @@ char *gcs_auth_request(char *content, char *url) {
297301 switch_curl_easy_setopt (curl_handle , CURLOPT_WRITEDATA , (void * )& http_data );
298302
299303 res = switch_curl_easy_perform (curl_handle );
300- curl_easy_cleanup (curl_handle );
304+ switch_curl_easy_cleanup (curl_handle );
305+
306+ if (res != CURLE_OK )
307+ fprintf (stderr , "curl_easy_perform() failed: %s\n" ,
308+ switch_curl_easy_strerror (res ));
301309
302310 if (http_data .stream .data && !zstr ((char * ) http_data .stream .data ) && strcmp (" " , http_data .stream .data )) {
303311 cJSON * json = {0 };
304- char * jsonstr ;
305312 json = cJSON_Parse (http_data .stream .data );
306- jsonstr = cJSON_GetObjectItem (json ,"access_token" )-> valuestring ;
307- response = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
308- strcpy (response , jsonstr );
313+
314+ if (cJSON_GetObjectItem (json ,"access_token" ) != NULL ) {
315+ char * jsonstr ;
316+ jsonstr = cJSON_GetObjectItem (json ,"access_token" )-> valuestring ;
317+ response = malloc (sizeof (char ) * (1 + strlen (jsonstr )));
318+ strcpy (response , jsonstr );
319+ }
309320 cJSON_Delete (json );
310321 }
311-
312- if (res != CURLE_OK )
313- fprintf (stderr , "curl_easy_perform() failed: %s\n" ,
314- switch_curl_easy_strerror (res ));
315322 return response ;
316323}
317324#endif
0 commit comments