From: Stephan Bosch Date: Sun, 28 Jul 2019 09:24:11 +0000 (+0200) Subject: lib-sasl: mech-oauthbearer - Use the new lib-json X-Git-Tag: 2.4.0~2364 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2679d7a422e26b120cb2c61c2d71490f7cbbcf0d;p=thirdparty%2Fdovecot%2Fcore.git lib-sasl: mech-oauthbearer - Use the new lib-json --- diff --git a/src/lib-imap-client/Makefile.am b/src/lib-imap-client/Makefile.am index f0f049a088..f5721e1206 100644 --- a/src/lib-imap-client/Makefile.am +++ b/src/lib-imap-client/Makefile.am @@ -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 diff --git a/src/lib-sasl/Makefile.am b/src/lib-sasl/Makefile.am index ac0e99bb1c..914be5e0c4 100644 --- a/src/lib-sasl/Makefile.am +++ b/src/lib-sasl/Makefile.am @@ -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 diff --git a/src/lib-sasl/mech-oauthbearer.c b/src/lib-sasl/mech-oauthbearer.c index 62ec6bc7ae..c9aee47322 100644 --- a/src/lib-sasl/mech-oauthbearer.c +++ b/src/lib-sasl/mech-oauthbearer.c @@ -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; diff --git a/src/lib-smtp/Makefile.am b/src/lib-smtp/Makefile.am index 2016277875..7873ca185a 100644 --- a/src/lib-smtp/Makefile.am +++ b/src/lib-smtp/Makefile.am @@ -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