]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-oauth2: Improve error message when server returns unexpected result
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 26 May 2020 21:30:45 +0000 (00:30 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 7 Dec 2020 08:59:28 +0000 (08:59 +0000)
If the result isn't 2xx or 4xx, use the HTTP response message as the error
message.

src/lib-oauth2/oauth2-request.c

index 4f1a7f735839e02816c9e2a7a653af608a77a48c..8b55b28ecfacc7f79c59ca3466ed667a16fbf3c5 100644 (file)
@@ -53,11 +53,10 @@ oauth2_request_continue(struct oauth2_request *req, const char *error)
        i_zero(&res);
 
        unsigned int status_hi = req->response_status/100;
+       i_assert(status_hi == 2 || status_hi == 4);
 
        if (error != NULL)
                res.error = error;
-       else if (status_hi != 2 && status_hi != 4)
-               res.error = "Internal Server Error";
        else {
                const struct oauth2_field *field;
                /* see if we can figure out when it expires */
@@ -77,6 +76,18 @@ static void
 oauth2_request_response(const struct http_response *response,
                        struct oauth2_request *req)
 {
+       req->response_status = response->status;
+       unsigned int status_hi = req->response_status/100;
+
+       if (status_hi != 2 && status_hi != 4) {
+               /* Unexpected internal error */
+               struct oauth2_request_result res = {
+                       .error = http_response_get_message(response),
+               };
+               oauth2_request_callback(req, &res);
+               return;
+       }
+
        if (response->payload != NULL) {
                req->is = response->payload;
                i_stream_ref(req->is);
@@ -84,7 +95,6 @@ oauth2_request_response(const struct http_response *response,
                req->is = i_stream_create_from_data("", 0);
        }
 
-       req->response_status = response->status;
        p_array_init(&req->fields, req->pool, 1);
        req->parser = json_parser_init(req->is);
        req->json_parsed_cb = oauth2_request_continue;