From 1fa2c1228f279b7e298b6e1e3037924717539211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Thu, 22 Sep 2022 15:37:57 +0800 Subject: Merge request_gettoken --- Makefile.am | 2 +- common.h | 11 +++++++++++ graphql.c | 32 ++++++++++++++++++++++++++++++++ request.c | 34 ---------------------------------- request.h | 34 ---------------------------------- shopify.c | 39 ++++++++++++++++++++++++++++----------- 6 files changed, 72 insertions(+), 80 deletions(-) create mode 100644 common.h create mode 100644 graphql.c delete mode 100644 request.c delete mode 100644 request.h diff --git a/Makefile.am b/Makefile.am index 5222786..235bf7a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ lib_LTLIBRARIES = libshopify.la -libshopify_la_SOURCES = shopify.c request.c +libshopify_la_SOURCES = shopify.c graphql.c libshopify_la_CPPFLAGS = $(DEPS_CFLAGS) include_HEADERS = shopify.h pkgconfigdir = $(libdir)/pkgconfig diff --git a/common.h b/common.h new file mode 100644 index 0000000..44601fd --- /dev/null +++ b/common.h @@ -0,0 +1,11 @@ +#include +#include + +static size_t append(char *data, size_t size, size_t nmemb, char **res) +{ + size_t realsize = size * nmemb; + size_t res_len = *res ? strlen(*res) : 0; + *res = realloc(*res, res_len + realsize + 1); + strlcpy(&(*res)[res_len], data, realsize + 1); + return realsize; +} diff --git a/graphql.c b/graphql.c new file mode 100644 index 0000000..d0e1ca8 --- /dev/null +++ b/graphql.c @@ -0,0 +1,32 @@ +#include +#include "shopify.h" +#include "common.h" + +void shopify_graphql(const char *query, const struct shopify_session *session, + char **json) +{ + CURL *curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query); + + static const char *url_tmpl + = "https://%s/admin/api/2022-07/graphql.json"; + char url[strlen(url_tmpl) - strlen("%s") + strlen(session->shop) + 1]; + sprintf(url, url_tmpl, session->shop); + curl_easy_setopt(curl, CURLOPT_URL, url); + + static const char *hdr_tmpl = "X-Shopify-Access-Token: %s"; + char header[strlen(hdr_tmpl) - strlen("%s") + + strlen(session->access_token) + 1]; + sprintf(header, hdr_tmpl, session->access_token); + + struct curl_slist *list = NULL; + list = curl_slist_append(list, header); + list = curl_slist_append(list, "Content-Type: application/graphql"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); + + curl_easy_perform(curl); + curl_slist_free_all(list); + curl_easy_cleanup(curl); +} diff --git a/request.c b/request.c deleted file mode 100644 index 099491b..0000000 --- a/request.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "request.h" -#include "shopify.h" - -extern inline void request_gettoken(const char *, const char *, const char *, - const char *, char **); - -void shopify_graphql(const char *query, const struct shopify_session *session, - char **json) -{ - CURL *curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query); - - static const char *url_tmpl - = "https://%s/admin/api/2022-07/graphql.json"; - char url[strlen(url_tmpl) - strlen("%s") + strlen(session->shop) + 1]; - sprintf(url, url_tmpl, session->shop); - curl_easy_setopt(curl, CURLOPT_URL, url); - - static const char *hdr_tmpl = "X-Shopify-Access-Token: %s"; - char header[strlen(hdr_tmpl) - strlen("%s") - + strlen(session->access_token) + 1]; - sprintf(header, hdr_tmpl, session->access_token); - - struct curl_slist *list = NULL; - list = curl_slist_append(list, header); - list = curl_slist_append(list, "Content-Type: application/graphql"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - - curl_easy_perform(curl); - curl_slist_free_all(list); - curl_easy_cleanup(curl); -} diff --git a/request.h b/request.h deleted file mode 100644 index 9bbbcf2..0000000 --- a/request.h +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include - -static size_t append(char *data, size_t size, size_t nmemb, char **res) -{ - size_t realsize = size * nmemb; - size_t res_len = *res ? strlen(*res) : 0; - *res = realloc(*res, res_len + realsize + 1); - strlcpy(&(*res)[res_len], data, realsize + 1); - return realsize; -} - -static inline void request_gettoken(const char *host, const char *key, - const char *secret_key, const char *code, char **json) -{ - CURL *curl = curl_easy_init(); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, json); - - static const char *url_tmpl = "https://%s/oauth/access_token"; - char url[strlen(url_tmpl) - strlen("%s") + strlen(host) + 1]; - sprintf(url, url_tmpl, host); - curl_easy_setopt(curl, CURLOPT_URL, url); - - static const char *post_tmpl = "client_id=%s&client_secret=%s&code=%s"; - char post[strlen(post_tmpl) - strlen("%s") * 3 + strlen(key) - + strlen(secret_key) + strlen(code) + 1]; - sprintf(post, post_tmpl, key, secret_key, code); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); - - curl_easy_perform(curl); - curl_easy_cleanup(curl); -} diff --git a/shopify.c b/shopify.c index 3df37fb..6ef6678 100644 --- a/shopify.c +++ b/shopify.c @@ -4,12 +4,13 @@ #include #include #include +#include #include #include #include "shopify.h" #include "regex.h" -#include "request.h" #include "sessiontoken.h" +#include "common.h" #define AUTH_URL \ "https://%s/oauth/authorize?client_id=%s&scope=%s&redirect_uri=%s%s"\ @@ -326,6 +327,7 @@ static enum MHD_Result handle_request(void *cls, struct MHD_Connection *con, size_t host_len = 0; bool embedded = false; char *dec_host = NULL; + size_t dec_host_len = 0; if (params) { host = ((struct parameter *)bsearch(&(struct parameter) { "host" }, params, nparams, @@ -338,10 +340,11 @@ static enum MHD_Result handle_request(void *cls, struct MHD_Connection *con, gnutls_datum_t result; gnutls_base64_decode2(&(gnutls_datum_t){ (unsigned char *)host, - strlen(host) + host_len }, &result); - dec_host = malloc(result.size + 1); - strlcpy(dec_host, (const char *)result.data, result.size + 1); + dec_host_len = result.size; + dec_host = malloc(dec_host_len + 1); + strlcpy(dec_host, (const char *)result.data, dec_host_len + 1); gnutls_free(result.data); } @@ -356,13 +359,27 @@ static enum MHD_Result handle_request(void *cls, struct MHD_Connection *con, &(struct parameter){ "code" }, params, nparams, sizeof(struct parameter), keycmp))->val; - char *access_token = NULL; - request_gettoken(dec_host, api_key, api_secret_key, code, - &access_token); + + CURL *curl = curl_easy_init(); + char *json = NULL; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); + static const char *url_tmpl = "https://%s/oauth/access_token"; + char url[strlen(url_tmpl) - strlen("%s") + dec_host_len + 1]; + sprintf(url, url_tmpl, dec_host); + curl_easy_setopt(curl, CURLOPT_URL, url); + static const char *post_tmpl + = "client_id=%s&client_secret=%s&code=%s"; + char post[strlen(post_tmpl) - strlen("%s") * 3 + strlen(api_key) + + strlen(api_secret_key) + strlen(code) + 1]; + sprintf(post, post_tmpl, api_key, api_secret_key, code); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); + curl_easy_perform(curl); + curl_easy_cleanup(curl); json_tokener *tokener = json_tokener_new(); - json_object *obj = json_tokener_parse_ex(tokener, access_token, - strlen(access_token)); + json_object *obj = json_tokener_parse_ex(tokener, json, + strlen(json)); struct json_object_iterator iter = json_object_iter_begin(obj); struct json_object_iterator iter_end = json_object_iter_end(obj); @@ -382,7 +399,7 @@ static enum MHD_Result handle_request(void *cls, struct MHD_Connection *con, json_object_iter_next(&iter); } json_tokener_free(tokener); - free(access_token); + free(json); ret = redirect(dec_host, app_id, con, &res); } else if (session_token) { @@ -437,7 +454,7 @@ static enum MHD_Result handle_request(void *cls, struct MHD_Connection *con, for (int i = 0; i < hex_len; i++) sprintf(nonce, "%s%02x", nonce, hex[i]); - const size_t auth_url_len = AUTH_URL_LEN + strlen(dec_host) + const size_t auth_url_len = AUTH_URL_LEN + dec_host_len + api_key_len + strlen(scopes) + app_url_len + strlen(redir_url) + nonce_len; char auth_url[auth_url_len + 1]; -- cgit v1.2.3