diff options
| author | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-14 18:19:14 +0800 |
|---|---|---|
| committer | ꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id> | 2022-09-14 18:19:14 +0800 |
| commit | 306cf74eb0101a12b51549866a4d60296618ee0b (patch) | |
| tree | 89fe13b234f40f97de84cdb6610bcd11e1d47a88 /request.h | |
OAuth part
The minimum to pass all authentications and arrive at the embedded app
index. This library is to be used with shopify-app-template-c for now,
as it assumes the existence of shopify.app.toml in the parent directory,
and index.html in the frontend directory.
Diffstat (limited to 'request.h')
| -rw-r--r-- | request.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/request.h b/request.h new file mode 100644 index 0000000..7c8fe99 --- /dev/null +++ b/request.h @@ -0,0 +1,46 @@ +#include <curl/curl.h> + +#define TOKEN_URL "https://%s/oauth/access_token" +#define TOKEN_URL_LEN strlen(TOKEN_URL) - strlen("%s") + +#define TOKEN_POST "client_id=%s&client_secret=%s&code=%s" +#define TOKEN_POST_LEN strlen(TOKEN_POST) - strlen("%s") * 3 + +static inline void request_init() +{ + curl_global_init(CURL_GLOBAL_DEFAULT); +} + +static size_t append(char *data, size_t size, size_t nmemb, char **tok) +{ + size_t realsize = size * nmemb; + size_t tok_len = *tok ? strlen(*tok) : 0; + *tok = realloc(*tok, tok_len + realsize + 1); + strlcpy(&(*tok)[tok_len], data, realsize + 1); + return realsize; +} + +static inline void request_token(const char *host, const char *key, + const char *secret_key, const char *code, char **tok) +{ + CURL *curl = curl_easy_init(); + char url[TOKEN_URL_LEN + strlen(host) + 1]; + sprintf(url, TOKEN_URL, host); + curl_easy_setopt(curl, CURLOPT_URL, url); + char post[TOKEN_POST_LEN + strlen(key) + strlen(secret_key) + + strlen(code) + 1]; + sprintf(post, TOKEN_POST, key, secret_key, code); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, tok); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append); +#ifdef DEBUG + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); +#endif + curl_easy_perform(curl); + curl_easy_cleanup(curl); +} + +static inline void request_cleanup() +{ + curl_global_cleanup(); +} |