summaryrefslogtreecommitdiff
path: root/token.h
blob: 40f600ebe8dd80051331275f043dc87fd2e72998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <json.h>

static inline void token_parse(const char *tok, struct shopify_session *session)
{
	json_tokener *tokener = json_tokener_new();
	json_object *obj = json_tokener_parse_ex(tokener, tok, strlen(tok));
	struct json_object_iterator iter = json_object_iter_begin(obj);
	struct json_object_iterator iter_end = json_object_iter_end(obj);
	while (!json_object_iter_equal(&iter, &iter_end)) {
		if (!strcmp(json_object_iter_peek_name(&iter),
					"access_token")) {
			const char *val = json_object_get_string(
					json_object_iter_peek_value(&iter));
			session->token = malloc(strlen(val) + 1);
			strcpy(session->token, val);
		} else if (!strcmp(json_object_iter_peek_name(&iter),
					"scope")) {
			const char *val = json_object_get_string(
					json_object_iter_peek_value(&iter));
			session->scope = malloc(strlen(val) + 1);
			strcpy(session->scope, val);
		}
		json_object_iter_next(&iter);
	}
	json_tokener_free(tokener);
}