summaryrefslogtreecommitdiff
path: root/imap/src/c-client/oauth2_aux.c
diff options
context:
space:
mode:
authorEduardo Chappa <chappa@washington.edu>2021-07-30 09:00:04 -0600
committerEduardo Chappa <chappa@washington.edu>2021-07-30 09:00:04 -0600
commitbba1f63e9be0b65c090d1707a6c9168443604ed6 (patch)
tree7dc6534d6693936d1249f81e790f264750d5e248 /imap/src/c-client/oauth2_aux.c
parentc39814e31d89bd14111347aadca9f0e225b8f06d (diff)
downloadalpine-bba1f63e9be0b65c090d1707a6c9168443604ed6.tar.xz
* Improvements to the http and json code.
Diffstat (limited to 'imap/src/c-client/oauth2_aux.c')
-rw-r--r--imap/src/c-client/oauth2_aux.c72
1 files changed, 19 insertions, 53 deletions
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){