summaryrefslogtreecommitdiff
path: root/shopify.c
blob: e7749963bef20f0c8b75620884f4cb2e23e86a83 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <fcntl.h>
#include <sys/stat.h>
#include "shopify.h"
#include "crypt.h"
#include "base64.h"
#include "regex.h"
#include "config.h"
#include "request.h"
#include "session.h"
#include "token.h"

#define AUTHORIZE_URL \
	"https://%s/oauth/authorize?client_id=%s&scope=%s&redirect_uri=%s%s"\
	"&state=%s"
#define AUTHORIZE_URL_LEN strlen(AUTHORIZE_URL) - strlen("%s") * 6

#define PAGE \
	"<!DOCTYPE html>\n"\
	"<html lang=\"en\">\n"\
	"\t<head>\n"\
	"\t\t<meta charset=\"utf-8\"/>\n"\
	"\t</head>\n"\
	"\t<body>\n"\
	"\t\t<script src=\"https://unpkg.com/@shopify/app-bridge@3\"/>\n"\
	"\t\t</script>\n"\
	"\t\t<script>\n"\
	"\t\t\tvar appBridge = window['app-bridge'];\n"\
	"\t\t\tvar redirect = appBridge.actions.Redirect;\n"\
	"\t\t\tredirect.create(appBridge.createApp({\n"\
	"\t\t\t\tapiKey: '%s',\n"\
	"\t\t\t\thost: '%s'\n"\
	"\t\t\t})).dispatch(redirect.Action.REMOTE, '%s');\n"\
	"\t\t</script>\n"\
	"\t</body>\n"\
	"</html>\n"
#define PAGE_LEN strlen(PAGE) - strlen("%s") * 3

#define FRAME "frame-ancestors https://%s https://admin.shopify.com;"
#define FRAME_LEN strlen(FRAME) - strlen("%s")

#define EMBEDDEDAPP_URL "https://%s/apps/%s/"
#define EMBEDDEDAPP_URL_LEN strlen(EMBEDDEDAPP_URL) - strlen("%s") * 2

extern inline void crypt_init();
extern inline bool crypt_maccmp(const char *, const char *, const char *);
extern inline void crypt_getnonce(char *, const size_t);
extern inline bool regex_match(const char *);
extern inline void base64_decode(unsigned char *, char **);
extern inline void config_getscopes(const char *, char **);
extern inline void request_init();
extern inline void request_token(const char *, const char *, const char *,
		const char *, char **);
extern inline void request_cleanup();
extern inline void token_parse(char *, struct session *);

void shopify_init()
{
	crypt_init();
	request_init();
	sessions = malloc(sizeof(struct session));
	sessions[0].shop = NULL;
}

static enum MHD_Result getparam(void *cls, enum MHD_ValueKind kind,
		const char *key, const char *val)
{
	if (kind == MHD_GET_ARGUMENT_KIND) {
		struct shopify_param **params = cls;
		int nparams = 0;
		while ((*params)[nparams].key)
			nparams++;
		*params = realloc(*params, sizeof(struct shopify_param)
				* (nparams + 2));
		(*params)[nparams].key = malloc(strlen(key) + 1);
		strcpy((*params)[nparams].key, key);
		(*params)[nparams].val = malloc(strlen(val) + 1);
		strcpy((*params)[nparams].val, val);
		(*params)[nparams + 1].key = NULL;
	}
	return MHD_YES;
}

static inline void clear(struct shopify_param params[])
{
	int i = 0;
	while (params[i].key) {
		free(params[i].val);
		free(params[i++].key);
	}
}

static int paramcmp(const void *param1, const void *param2)
{
	return strcmp(((struct shopify_param *)param1)->key,
			((struct shopify_param *)param2)->key);
}

static int sessioncmp(const void *session1, const void *session2)
{
	return strcmp(((struct session *)session1)->shop,
			((struct session *)session2)->shop);
}

bool shopify_valid(struct MHD_Connection *conn, const char *url,
		const char *redir_url, const char *secret_key,
		struct shopify_param *params[])
{
	(*params)[0].key = NULL;
	MHD_get_connection_values(conn, MHD_GET_ARGUMENT_KIND, getparam,
			params);
	int nparams = 0;
	while ((*params)[nparams].key)
		nparams++;
	if (!nparams)
		return false;
	qsort(*params, nparams, sizeof(struct shopify_param), paramcmp);
	char *shop = NULL;
	struct shopify_param *param = bsearch(&(struct shopify_param)
			{ "shop" }, *params, nparams,
			sizeof(struct shopify_param), paramcmp);
	if (param)
		shop = param->val;
	if (!shop || !regex_match(shop)) {
		clear(*params);
		return false;
	}
	param = NULL;
	char *query = NULL;
	for (int i = 0; i < nparams; i++) {
		const char *key = (*params)[i].key;
		const char *val = (*params)[i].val;
		if (strcmp(key, "hmac")) {
			size_t query_len = query ? strlen(query) : 0;
			bool last = i == nparams - 1;
			query = realloc(query, query_len + strlen(key)
					+ strlen(val) + !last + 2);
			query[query_len] = '\0';
			sprintf(query, "%s%s=%s%s", query, key, val,
					last ? "" : "&");
		}
	}
	char *hmac = NULL;
	param = bsearch(&(struct shopify_param){ "hmac" }, *params, nparams,
				sizeof(struct shopify_param), paramcmp);
	if (param)
		hmac = param->val;
	if (!hmac || !crypt_maccmp(secret_key, query, hmac)) {
		clear(*params);
		free(query);
		return false;
	}
	free(query);
	if (strcmp(url, redir_url))
		return true;
	char *state = ((struct shopify_param *)bsearch(&(struct shopify_param)
				{ "state" }, *params, nparams,
				sizeof(struct shopify_param), paramcmp))->val;
	int nsessions = 0;
	while (sessions[nsessions].shop)
		nsessions++;
	qsort(sessions, nsessions, sizeof(struct session), sessioncmp);
	struct session *session = bsearch(&(struct session){ shop }, sessions,
			nsessions, sizeof(struct session), sessioncmp);
	if (strcmp(state, session->nonce)) {
		clear(*params);
		return false;
	}
	return true;
}

static inline int redirect(const char *host, const char *id,
		struct MHD_Response **resp, struct MHD_Connection *conn)
{
	char url[EMBEDDEDAPP_URL_LEN + strlen(host) + strlen(id) + 1];
	sprintf(url, EMBEDDEDAPP_URL, host, id);
	*resp = MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT);
	MHD_add_response_header(*resp, "Location", url);
	return MHD_queue_response(conn, MHD_HTTP_PERMANENT_REDIRECT, *resp);
}

enum MHD_Result shopify_respond(struct shopify_param params[], const char *url,
		const char *redir_url, const char *app_url, const char *app_id,
		const char *key, const char *secret_key, const char *toml_path,
		const char *html_path, struct MHD_Connection *conn,
		struct MHD_Response **resp)
{
	int nparams = 0;
	while (params[nparams].key)
		nparams++;
	char *shop = ((struct shopify_param *)bsearch(&(struct shopify_param)
				{ "shop" }, params, nparams,
				sizeof(struct shopify_param), paramcmp))->val;
	const size_t shop_len = strlen(shop);
	char *host = ((struct shopify_param *)bsearch(&(struct shopify_param)
				{ "host" }, params, nparams,
				sizeof(struct shopify_param), paramcmp))->val;
	struct shopify_param *param = bsearch(&(struct shopify_param)
			{ "embedded" }, params, nparams,
			sizeof(struct shopify_param), paramcmp);
	bool embedded = param && !strcmp(param->val, "1");
	char *decoded_host;
	base64_decode((unsigned char *)host, &decoded_host);
	int nsessions = 0;
	while (sessions[nsessions].shop)
		nsessions++;
	qsort(sessions, nsessions, sizeof(struct session), sessioncmp);
	struct session *session = bsearch(&(struct session){ shop }, sessions,
			nsessions, sizeof(struct session), sessioncmp);
	const size_t key_len = strlen(key);
	char frame[FRAME_LEN + shop_len + 1];
	sprintf(frame, FRAME, shop);
	enum MHD_Result ret;
	if (!strcmp(url, redir_url)) {
		const char *code = ((struct shopify_param *)bsearch(
					&(struct shopify_param){ "code" },
					params, nparams,
					sizeof(struct shopify_param),
					paramcmp))->val;
		char *token = NULL;
		request_token(decoded_host, key, secret_key, code, &token);
		token_parse(token, session);
		free(token);
		ret = redirect(decoded_host, app_id, resp, conn);
	} else if (session && session->token) {
		if (embedded) {
			int fd = open(toml_path, O_RDONLY);
			struct stat sb;
			fstat(fd, &sb);
			char index[sb.st_size + 1];
			read(fd, index, sb.st_size);
			close(fd);
			*resp = MHD_create_response_from_buffer(sb.st_size,
					index, MHD_RESPMEM_MUST_COPY);
			MHD_add_response_header(*resp,
					"Content-Security-Policy", frame);
			ret = MHD_queue_response(conn, MHD_HTTP_OK, *resp);
		} else
			ret = redirect(decoded_host, app_id, resp, conn);
	} else {
		const size_t decoded_host_len = strlen(decoded_host);
		char *scopes = NULL;
		config_getscopes(html_path, &scopes);
		const size_t scopes_len = strlen(scopes);
		static const size_t nonce_len = 64;
		char nonce[nonce_len + 1];
		crypt_getnonce(nonce, nonce_len);
		const size_t authorize_url_len = AUTHORIZE_URL_LEN
			+ decoded_host_len + key_len + scopes_len
			+ strlen(app_url) + strlen(redir_url) + nonce_len;
		char authorize_url[authorize_url_len + 1];
		sprintf(authorize_url, AUTHORIZE_URL, decoded_host, key, scopes,
				app_url, redir_url, nonce);
		free(scopes);
		sessions = realloc(sessions, sizeof(struct session)
				* (nsessions + 2));
		sessions[nsessions].shop = malloc(shop_len + 1);
		strcpy(sessions[nsessions].shop, shop);
		sessions[nsessions].nonce = malloc(nonce_len + 1);
		strcpy(sessions[nsessions].nonce, nonce);
		sessions[nsessions + 1].shop = NULL;
		if (embedded) {
			const size_t page_len = PAGE_LEN + key_len
				+ strlen(host) + authorize_url_len;
			char page[page_len + 1];
			sprintf(page, PAGE, key, host, authorize_url);
			*resp = MHD_create_response_from_buffer(page_len,
					page, MHD_RESPMEM_MUST_COPY);
			MHD_add_response_header(*resp,
					"Content-Security-Policy", frame);
			ret = MHD_queue_response(conn, MHD_HTTP_OK, *resp);
		} else {
			*resp = MHD_create_response_from_buffer(0, "",
					MHD_RESPMEM_PERSISTENT);
			MHD_add_response_header(*resp, "Location",
					authorize_url);
			ret = MHD_queue_response(conn,
					MHD_HTTP_TEMPORARY_REDIRECT, *resp);
		}
	}
	free(decoded_host);
	clear(params);
	return ret;
}

void shopify_cleanup()
{
	request_cleanup();
	int i = 0;
	while (sessions[i].shop) {
		if (sessions[i].scope)
			free(sessions[i].scope);
		if (sessions[i].token)
			free(sessions[i].token);
		free(sessions[i].nonce);
		free(sessions[i++].shop);
	}
	free(sessions);
}