]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-oauth2: Add helper for parsing json strings
authorAki Tuomi <aki.tuomi@open-xchange.com>
Sat, 8 Feb 2020 17:51:06 +0000 (19:51 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Thu, 20 Feb 2020 11:19:56 +0000 (11:19 +0000)
src/lib-oauth2/oauth2-private.h
src/lib-oauth2/oauth2.c

index b8ffe59879e0684d13cc4008c589c800d39128a4..9e0b0b9e61f42f3d6704a754706c356c4edaf3e7 100644 (file)
@@ -2,6 +2,8 @@
 #ifndef OAUTH2_PRIVATE_H
 #define OAUTH2_PRIVATE_H 1
 
+struct json_tree;
+
 struct oauth2_request {
        pool_t pool;
 
@@ -34,5 +36,7 @@ void oauth2_request_set_headers(struct oauth2_request *req,
 void oauth2_request_free_internal(struct oauth2_request *req);
 
 void oauth2_parse_json(struct oauth2_request *req);
+int oauth2_json_tree_build(const buffer_t *json, struct json_tree **tree_r,
+                          const char **error_r);
 
 #endif
index 50d4392aa83b3abd197100228915cb2947802ad1..5cee85cbff8c80132d7e2749a6c5e3da2738d1ec 100644 (file)
@@ -4,11 +4,35 @@
 #include "ioloop.h"
 #include "istream.h"
 #include "http-client.h"
-#include "json-parser.h"
+#include "json-tree.h"
 #include "oauth2.h"
 #include "oauth2-private.h"
 #include "safe-memset.h"
 
+int oauth2_json_tree_build(const buffer_t *json, struct json_tree **tree_r,
+                          const char **error_r)
+{
+       struct istream *is = i_stream_create_from_buffer(json);
+       struct json_parser *parser = json_parser_init(is);
+       struct json_tree *tree = json_tree_init();
+       enum json_type type;
+       const char *value;
+       int ret;
+       while ((ret = json_parse_next(parser, &type, &value)) > 0) {
+               /* this is safe to reuse here because it gets rewritten in while
+                  loop */
+               ret = json_tree_append(tree, type, value);
+               i_assert(ret == 0);
+       }
+       ret = json_parser_deinit(&parser, error_r);
+       i_stream_unref(&is);
+       if (ret != 0)
+               json_tree_deinit(&tree);
+       else
+               *tree_r = tree;
+       return ret;
+}
+
 void
 oauth2_parse_json(struct oauth2_request *req)
 {