]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-sasl: mech-oauthbearer - Use the new lib-json
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sun, 28 Jul 2019 09:24:11 +0000 (11:24 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Sat, 18 Nov 2023 18:58:04 +0000 (18:58 +0000)
src/lib-imap-client/Makefile.am
src/lib-sasl/Makefile.am
src/lib-sasl/mech-oauthbearer.c
src/lib-smtp/Makefile.am

index f0f049a088eed9b6b68d1b5a9f5e338fe055a81c..f5721e12066b65d98ac1eb4c6f07e18cf94b9a04 100644 (file)
@@ -38,6 +38,7 @@ test_deps = \
        ../lib-dns/libdns.la \
        ../lib-auth/libauth.la \
        ../lib-otp/libotp.la \
+       ../lib-json/libjson.la \
        ../lib-test/libtest.la \
        ../lib/liblib.la
 
index ac0e99bb1cd9f83c6ebf36792ab682e95209c399..914be5e0c4946e35b50508df78466549691a5d52 100644 (file)
@@ -3,7 +3,8 @@ noinst_LTLIBRARIES = libsasl.la
 AM_CPPFLAGS = \
        -I$(top_srcdir)/src/lib \
        -I$(top_srcdir)/src/lib-test \
-       -I$(top_srcdir)/src/lib-auth
+       -I$(top_srcdir)/src/lib-auth \
+       -I$(top_srcdir)/src/lib-json
 
 libsasl_la_SOURCES = \
        mech-external.c \
@@ -13,7 +14,8 @@ libsasl_la_SOURCES = \
        mech-scram.c \
        dsasl-client.c
 libsasl_la_DEPENDENCIES = \
-       ../lib-auth/libauth.la
+       ../lib-auth/libauth.la \
+       ../lib-json/libjson.la
 
 headers = \
        dsasl-client.h \
@@ -30,6 +32,7 @@ noinst_PROGRAMS = $(test_programs)
 test_libs = \
        $(noinst_LTLIBRARIES) \
        ../lib-auth/libauth.la \
+       ../lib-json/libjson.la \
        ../lib-test/libtest.la \
        ../lib/liblib.la
 
index 62ec6bc7aea356f1d53a2cfa35a78343cc343cad..c9aee473228c2dc962e1ea5eb138a03a8d86d691 100644 (file)
@@ -3,7 +3,7 @@
 #include "lib.h"
 #include "str.h"
 #include "net.h"
-#include "json-parser.h"
+#include "json-istream.h"
 #include "istream.h"
 #include "dsasl-client-private.h"
 
@@ -38,40 +38,50 @@ mech_oauthbearer_input(struct dsasl_client *_client,
                   we are only interested in extracting status if possible
                   so we don't really need to much error handling. */
                struct istream *is = i_stream_create_from_data(input, input_len);
-               const char *status = NULL, *value;
-               const char *error = NULL;
-               enum json_type jtype;
-               bool found_status = FALSE;
-               struct json_parser *parser = json_parser_init(is);
-               while (json_parse_next(parser, &jtype, &value)>0) {
-                       if (found_status && status == NULL) {
-                               if (jtype == JSON_TYPE_STRING ||
-                                   jtype == JSON_TYPE_NUMBER)
-                                       status = t_strdup(value);
+               struct json_node jnode;
+               struct json_istream *jis = json_istream_create_object(
+                       is, NULL, JSON_PARSER_FLAG_NUMBERS_AS_STRING);
+               const char *error;
+               int ret;
+
+               i_stream_unref(&is);
+
+               ret = 1;
+               while (ret > 0) {
+                       ret = json_istream_read(jis, &jnode);
+                       i_assert(ret != 0);
+                       if (ret < 0)
                                break;
-                       } else if (jtype == JSON_TYPE_OBJECT_KEY &&
-                                  strcmp(value, "status") == 0) {
-                               found_status = TRUE;
-                       } else json_parse_skip_next(parser);
+                       i_assert(jnode.name != NULL);
+                       if (strcmp(jnode.name, "status") == 0) {
+                               if (!json_node_is_string(&jnode) &&
+                                   !json_node_is_number(&jnode)) {
+                                       *error_r = "Status field in response is not a string or a number.";
+                                       return -1;
+                               }
+                               client->status = p_strdup(
+                                       _client->pool,
+                                       json_node_get_str(&jnode));
+                       }
+                       json_istream_skip(jis);
                }
 
-               /* deinitialize json parser */
-               int ret = json_parser_deinit(&parser, &error);
-               i_stream_unref(&is);
+               ret = json_istream_finish(&jis, &error);
+               i_assert(ret != 0);
+               if (ret < 0) {
+                       *error_r = t_strdup_printf(
+                               "Error parsing JSON reply: %s", error);
+                       return -1;
+               }
 
-               if (status != NULL)
-                       client->status = p_strdup(_client->pool, status);
-               else {
-                       ret = -1;
-                       if (error == NULL)
-                               error = "Status value missing";
+               if (client->status == NULL) {
+                       *error_r = "Error parsing JSON reply: "
+                               "Status value missing";
+                       return -1;
                }
-               if (ret < 0)
-                       *error_r = t_strdup_printf("Error parsing JSON reply: %s",
-                                                  error);
-               else
-                       *error_r = t_strdup_printf("Failed to authenticate: %s",
-                                                  client->status);
+
+               *error_r = t_strdup_printf("Failed to authenticate: %s",
+                                          client->status);
                return -1;
        }
        return 0;
index 20162778751e677dba4ecf67bd6019a5e7c41efd..7873ca185a007f61f0aba169a95b0b5c962f2bae 100644 (file)
@@ -113,6 +113,7 @@ test_libs = \
        ../lib-sasl/libsasl.la \
        ../lib-auth/libauth.la \
        ../lib-otp/libotp.la \
+       ../lib-json/libjson.la \
        ../lib-test/libtest.la \
        ../lib/liblib.la \
        $(MODULE_LIBS)
@@ -129,6 +130,7 @@ test_deps = \
        ../lib-settings/libsettings.la \
        ../lib-sasl/libsasl.la \
        ../lib-auth/libauth.la \
+       ../lib-json/libjson.la \
        ../lib-test/libtest.la \
        ../lib/liblib.la