talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 133d759fe4ead4e6950d5f8e54415fdd1808a963
parent f6213f8aab664d14349b32a276bedb3a1d9c73f5
Author: Michael Rasmussen <mir@datanom.net>
Date:   Tue,  9 Jan 2024 11:36:43 +0100

Fix bug #Bug 4730 add some more debug output. Patch from Olaf Hering

Signed-off-by: Michael Rasmussen <mir@datanom.net>

Diffstat:
Msrc/oauth2.c | 106+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 55 insertions(+), 51 deletions(-)

diff --git a/src/oauth2.c b/src/oauth2.c @@ -130,7 +130,6 @@ static gchar *OAUTH2CodeMarker[5][2] = { static gint oauth2_post_request (gchar *buf, gchar *host, gchar *resource, gchar *header, gchar *body); static gint oauth2_filter_refresh (gchar *json, gchar *refresh_token); static gint oauth2_filter_access (gchar *json, gchar *access_token, gint *expiry); -static gint oauth2_contact_server (SockInfo *sock, gchar *request, gchar *response); static gint oauth2_post_request (gchar *buf, gchar *host, gchar *resource, gchar *header, gchar *body) @@ -215,6 +214,50 @@ static gchar* oauth2_get_token_from_response(Oauth2Service provider, const gchar return token; } +static gchar *oauth2_contact_server(SockInfo *sock, const gchar *request) +{ + gboolean got_some_error, timeout; + gint ret; + char buf[1024]; + GString *response = g_string_sized_new(sizeof(buf)); + time_t end_time = time(NULL); + + end_time += prefs_common_get_prefs()->io_timeout_secs; + + if (!response) + return NULL; + + if (sock_write(sock, request, strlen(request)) < 0) { + log_message(LOG_PROTOCOL, _("OAuth2 socket write error\n")); + return NULL; + } + + do { + ret = sock_read(sock, buf, sizeof(buf) - 1); + got_some_error = ret < 0; + timeout = time(NULL) > end_time; + + if (timeout) + break; + + if (ret < 0 && errno == EAGAIN) + continue; + + if (got_some_error) + break; + + if (ret) { + buf[ret] = '\0'; + g_string_append_len(response, buf, ret); + } + } while (ret); + + if (timeout) + log_message(LOG_PROTOCOL, _("OAuth2 socket timeout error\n")); + + return g_string_free(response, got_some_error || timeout); +} + int oauth2_obtain_tokens (Oauth2Service provider, OAUTH2Data *OAUTH2Data, const gchar *authcode) { gchar *request; @@ -267,7 +310,6 @@ int oauth2_obtain_tokens (Oauth2Service provider, OAUTH2Data *OAUTH2Data, const refresh_token = g_malloc(OAUTH2BUFSIZE+1); access_token = g_malloc(OAUTH2BUFSIZE+1); request = g_malloc(OAUTH2BUFSIZE+1); - response = g_malloc0(OAUTH2BUFSIZE+1); if(OAUTH2Data->custom_client_id) client_id = g_strdup(OAUTH2Data->custom_client_id); @@ -329,11 +371,12 @@ int oauth2_obtain_tokens (Oauth2Service provider, OAUTH2Data *OAUTH2Data, const header = g_strconcat ("", NULL); } - debug_print("Complete body: %s\n", body); + debug_print("Complete body: %s\n", body); oauth2_post_request (request, OAUTH2info[i][OA2_BASE_URL], OAUTH2info[i][OA2_ACCESS_RESOURCE], header, body); - ret = oauth2_contact_server (sock, request, response); + response = oauth2_contact_server (sock, request); + debug_print("Response from server: %s\n", response); - if(oauth2_filter_access (response, access_token, &expiry) == 0){ + if(response && oauth2_filter_access (response, access_token, &expiry) == 0){ OAUTH2Data->access_token = g_strdup(access_token); OAUTH2Data->expiry = expiry; OAUTH2Data->expiry_str = g_strdup_printf ("%i", expiry); @@ -345,7 +388,7 @@ int oauth2_obtain_tokens (Oauth2Service provider, OAUTH2Data *OAUTH2Data, const ret = 1; } - if(oauth2_filter_refresh (response, refresh_token) == 0){ + if(response && oauth2_filter_refresh (response, refresh_token) == 0){ OAUTH2Data->refresh_token = g_strdup(refresh_token); log_message(LOG_PROTOCOL, _("OAuth2 refresh token obtained\n")); }else{ @@ -408,7 +451,6 @@ gint oauth2_use_refresh_token (Oauth2Service provider, OAUTH2Data *OAUTH2Data) access_token = g_malloc(OAUTH2BUFSIZE+1); refresh_token = g_malloc(OAUTH2BUFSIZE+1); request = g_malloc(OAUTH2BUFSIZE+1); - response = g_malloc(OAUTH2BUFSIZE+1); if(OAUTH2Data->custom_client_id) client_id = g_strdup(OAUTH2Data->custom_client_id); @@ -467,9 +509,12 @@ gint oauth2_use_refresh_token (Oauth2Service provider, OAUTH2Data *OAUTH2Data) } oauth2_post_request (request, OAUTH2info[i][OA2_BASE_URL], OAUTH2info[i][OA2_REFRESH_RESOURCE], header, body); - ret = oauth2_contact_server (sock, request, response); + debug_print("Request: %s\n", request); + response = oauth2_contact_server (sock, request); + debug_print("Response from server: %s\n", response); - if(oauth2_filter_access (response, access_token, &expiry) == 0){ + + if(response && oauth2_filter_access (response, access_token, &expiry) == 0){ OAUTH2Data->access_token = g_strdup(access_token); OAUTH2Data->expiry = expiry; OAUTH2Data->expiry_str = g_strdup_printf ("%i", expiry); @@ -481,7 +526,7 @@ gint oauth2_use_refresh_token (Oauth2Service provider, OAUTH2Data *OAUTH2Data) ret = 1; } - if (oauth2_filter_refresh (response, refresh_token) == 0) { + if (response && oauth2_filter_refresh (response, refresh_token) == 0) { OAUTH2Data->refresh_token = g_strdup(refresh_token); log_message(LOG_PROTOCOL, _("OAuth2 replacement refresh token provided\n")); } else @@ -503,47 +548,6 @@ gint oauth2_use_refresh_token (Oauth2Service provider, OAUTH2Data *OAUTH2Data) return (ret); } -static gint oauth2_contact_server (SockInfo *sock, gchar *request, gchar *response) -{ - gint ret; - gchar *token; - gint toread = OAUTH2BUFSIZE; - time_t startplus = time(NULL); - gchar *tmp; - - gint timeout_secs = prefs_common_get_prefs()->io_timeout_secs; - startplus += timeout_secs; - - if (sock_write (sock, request, strlen(request)) < 0) { - log_message(LOG_PROTOCOL, _("OAuth2 socket write error\n")); - return (1); - } - - token = g_strconcat ("", NULL); - do { - - ret = sock_read (sock, response, OAUTH2BUFSIZE); - if (ret < 0 && errno == EAGAIN) - continue; - if (ret < 0) - break; - if (ret == 0) - break; - - toread -= ret; - tmp = g_strconcat(token, response, NULL); - g_free(token); - token = tmp; - } while ((toread > 0) && (time(NULL) < startplus)); - - if(time(NULL) >= startplus) - log_message(LOG_PROTOCOL, _("OAuth2 socket timeout error\n")); - - g_free(token); - - return (0); -} - gint oauth2_authorisation_url (Oauth2Service provider, gchar **url, const gchar *custom_client_id) { gint i;