From bba1f63e9be0b65c090d1707a6c9168443604ed6 Mon Sep 17 00:00:00 2001 From: Eduardo Chappa Date: Fri, 30 Jul 2021 09:00:04 -0600 Subject: * Improvements to the http and json code. --- imap/src/c-client/oauth2_aux.c | 72 +++++++++++------------------------------- 1 file changed, 19 insertions(+), 53 deletions(-) (limited to 'imap/src/c-client/oauth2_aux.c') diff --git a/imap/src/c-client/oauth2_aux.c b/imap/src/c-client/oauth2_aux.c index 550f3b56..9d9fa304 100644 --- a/imap/src/c-client/oauth2_aux.c +++ b/imap/src/c-client/oauth2_aux.c @@ -114,11 +114,10 @@ JSON_S *oauth2_json_reply(OAUTH2_SERVER_METHOD_S RefreshMethod, OAUTH2_S *oauth2 if(strcmp(RefreshMethod.name, "POST") == 0 && ((stream = http_open(server)) != NULL) && ((s = http_post_param(stream, params)) != NULL)){ - unsigned char *u = s; - json = json_parse(&u); + json = json_parse(s); fs_give((void **) &s); } - *status = stream->status ? stream->status->code : -1; + *status = stream && stream->status ? stream->status->code : -1; if(stream) http_close(stream); if(server) fs_give((void **) &server); @@ -164,18 +163,9 @@ mm_login_oauth2_c_client_method (NETMBX *mb, char *user, char *method, if(json != NULL){ JSON_X *jx; - jx = json_body_value(json, "device_code"); - if(jx && jx->jtype == JString) - oauth2->devicecode.device_code = cpystr((char *) jx->value); - - jx = json_body_value(json, "user_code"); - if(jx && jx->jtype == JString) - oauth2->devicecode.user_code = cpystr((char *) jx->value); - - jx = json_body_value(json, "verification_uri"); - if(jx && jx->jtype == JString) - oauth2->devicecode.verification_uri = cpystr((char *) jx->value); - + json_assign ((void **) &oauth2->devicecode.device_code, json, "device_code", JString); + json_assign ((void **) &oauth2->devicecode.user_code, json, "user_code", JString); + json_assign ((void **) &oauth2->devicecode.verification_uri, json, "verification_uri", JString); if((jx = json_body_value(json, "expires_in")) != NULL) switch(jx->jtype){ case JString: oauth2->devicecode.expires_in = atoi((char *) jx->value); @@ -194,10 +184,7 @@ mm_login_oauth2_c_client_method (NETMBX *mb, char *user, char *method, default : break; } - jx = json_body_value(json, "message"); - if(jx && jx->jtype == JString) - oauth2->devicecode.message = cpystr((char *) jx->value); - + json_assign ((void **) &oauth2->devicecode.message, json, "message", JString); json_free(&json); if(oauth2->devicecode.verification_uri && oauth2->devicecode.user_code){ @@ -220,10 +207,7 @@ mm_login_oauth2_c_client_method (NETMBX *mb, char *user, char *method, case HTTP_UNAUTHORIZED: mm_log("Client not authorized (wrong client-id?)", ERROR); break; - case HTTP_OK: jx = json_body_value(json, "access_token"); - if(jx && jx->jtype == JString) - oauth2->access_token = cpystr((char *) jx->value); - + case HTTP_OK: json_assign ((void **) &oauth2->access_token, json, "access_token", JString); if((jx = json_body_value(json, "expires_in")) != NULL) switch(jx->jtype){ case JString: oauth2->expiration = time(0) + atol((char *) jx->value); @@ -282,30 +266,21 @@ mm_login_oauth2_c_client_method (NETMBX *mb, char *user, char *method, JSON_X *jx; switch(status){ - case HTTP_OK : jx = json_body_value(json, "refresh_token"); - if(jx && jx->jtype == JString) - oauth2->param[OA2_RefreshToken].value = cpystr((char *) jx->value); - - jx = json_body_value(json, "access_token"); - if(jx && jx->jtype == JString) - oauth2->access_token = cpystr((char *) jx->value); + case HTTP_OK : json_assign ((void **) &oauth2->param[OA2_RefreshToken].value, json, "refresh_token", JString); + json_assign ((void **) &oauth2->access_token, json, "access_token", JString); - if((jx = json_body_value(json, "expires_in")) != NULL) - switch(jx->jtype){ + if((jx = json_body_value(json, "expires_in")) != NULL) + switch(jx->jtype){ case JString: oauth2->expiration = time(0) + atol((char *) jx->value); break; case JLong : oauth2->expiration = time(0) + *(long *) jx->value; break; default : break; - } - - jx = json_body_value(json, "expires_in"); - if(jx && jx->jtype == JString) - oauth2->expiration = time(0) + atol((char *) jx->value); + } - oauth2->cancel_refresh_token = 0; /* do not cancel this token. It is good */ + oauth2->cancel_refresh_token = 0; /* do not cancel this token. It is good */ - break; + break; case HTTP_BAD : break; @@ -346,15 +321,11 @@ void oauth2deviceinfo_get_accesscode(void *inp, void *outp) if(json != NULL){ JSON_X *jx; - char *error; + char *error = NIL; switch(status){ - case HTTP_BAD : jx = json_body_value(json, "error"); - if(jx && jx->jtype == JString) - error = cpystr((char *) jx->value); - else - break; - + case HTTP_BAD : json_assign ((void **) &error, json, "error", JString); + if(!error) break; if(compare_cstring(error, "authorization_pending") == 0) rv = OA2_CODE_WAIT; else if(compare_cstring(error, "authorization_declined") == 0) @@ -368,13 +339,8 @@ void oauth2deviceinfo_get_accesscode(void *inp, void *outp) break; - case HTTP_OK : jx = json_body_value(json, "refresh_token"); - if(jx && jx->jtype == JString) - oauth2->param[OA2_RefreshToken].value = cpystr((char *) jx->value); - - jx = json_body_value(json, "access_token"); - if(jx && jx->jtype == JString) - oauth2->access_token = cpystr((char *) jx->value); + case HTTP_OK : json_assign ((void **) &oauth2->param[OA2_RefreshToken].value, json, "refresh_token", JString); + json_assign ((void **) &oauth2->access_token, json, "access_token", JString); if((jx = json_body_value(json, "expires_in")) != NULL) switch(jx->jtype){ -- cgit v1.2.3-54-g00ecf