commit 4ced03b
default
·
2023-04-09 18:34:05 +0000 UTC
parent 752058b
OAuth login now works.
3 files changed,
+108,
-16
M
httpd.c
+3,
-0
1@@ -179,6 +179,9 @@ void httpd_connection(FILE *f)
2 if (status == 0)
3 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
4
5+ if (status == 0)
6+ status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
7+
8 if (status == 0)
9 status = html_get_handler(req, q_path, &body, &b_size, &ctype);
10 }
+98,
-11
1@@ -73,12 +73,14 @@ xs_dict *app_get(const char *id)
2
3 const char *login_page = ""
4 "<!DOCTYPE html>\n"
5-"<body><h1>%s identify</h1>\n"
6+"<body><h1>%s OAuth identify</h1>\n"
7+"<div style=\"background-color: red; color: white\">%s</div>\n"
8 "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
9 "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
10 "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
11 "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
12 "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
13+"<input type=\"submit\" value=\"OK\">\n"
14 "</form><p>%s</p></body>\n"
15 "";
16
17@@ -90,13 +92,15 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
18
19 {
20 xs *j = xs_json_dumps_pp(req, 4);
21- printf("oauth:\n%s\n", j);
22+ printf("oauth get:\n%s\n", j);
23 }
24
25 int status = 404;
26 xs_dict *msg = xs_dict_get(req, "q_vars");
27 xs *cmd = xs_replace(q_path, "/oauth", "");
28
29+ srv_debug(0, xs_fmt("oauth_get_handler %s", q_path));
30+
31 if (strcmp(cmd, "/authorize") == 0) {
32 const char *cid = xs_dict_get(msg, "client_id");
33 const char *ruri = xs_dict_get(msg, "redirect_uri");
34@@ -110,11 +114,17 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
35 if (app != NULL) {
36 const char *host = xs_dict_get(srv_config, "host");
37
38- *body = xs_fmt(login_page, host, host, ruri, cid, USER_AGENT);
39+ *body = xs_fmt(login_page, host, "", host, ruri, cid, USER_AGENT);
40 *ctype = "text/html";
41 status = 200;
42+
43+ srv_debug(0, xs_fmt("oauth authorize: generating login page"));
44 }
45+ else
46+ srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
47 }
48+ else
49+ srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
50 }
51
52 return status;
53@@ -122,25 +132,70 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
54
55
56 int oauth_post_handler(const xs_dict *req, const char *q_path,
57- const char *payload, int p_size,
58- char **body, int *b_size, char **ctype)
59+ const char *payload, int p_size,
60+ char **body, int *b_size, char **ctype)
61 {
62 if (!xs_startswith(q_path, "/oauth/"))
63 return 0;
64
65+ {
66+ xs *j = xs_json_dumps_pp(req, 4);
67+ printf("oauth post:\n%s\n", j);
68+ }
69+
70 int status = 404;
71 xs_dict *msg = xs_dict_get(req, "p_vars");
72 xs *cmd = xs_replace(q_path, "/oauth", "");
73
74- printf("oauth: %s\n", q_path);
75+ srv_debug(0, xs_fmt("oauth_post_handler %s", q_path));
76+
77+ if (strcmp(cmd, "/x-snac-login") == 0) {
78+ const char *login = xs_dict_get(msg, "login");
79+ const char *passwd = xs_dict_get(msg, "passwd");
80+ const char *redir = xs_dict_get(msg, "redir");
81+ const char *cid = xs_dict_get(msg, "cid");
82+
83+ const char *host = xs_dict_get(srv_config, "host");
84+
85+ /* by default, generate another login form with an error */
86+ *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, USER_AGENT);
87+ *ctype = "text/html";
88+ status = 200;
89+
90+ if (login && passwd && redir && cid) {
91+ snac snac;
92
93+ if (user_open(&snac, login)) {
94+ /* check the login + password */
95+ if (check_password(login, passwd,
96+ xs_dict_get(snac.config, "passwd"))) {
97+ /* success! redirect to the desired uri */
98+ xs *code = random_str();
99+
100+ xs_free(*body);
101+ *body = xs_fmt("%s?code=%s", redir, code);
102+ status = 303;
103+
104+ srv_debug(0, xs_fmt("oauth x-snac-login: redirect to %s", *body));
105+ }
106+ else
107+ srv_debug(0, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
108+
109+ user_free(&snac);
110+ }
111+ else
112+ srv_debug(0, xs_fmt("oauth x-snac-login: bad user '%s'", login));
113+ }
114+ else
115+ srv_debug(0, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
116+ }
117+ else
118 if (strcmp(cmd, "/token") == 0) {
119 const char *gtype = xs_dict_get(msg, "grant_type");
120 const char *code = xs_dict_get(msg, "code");
121 const char *cid = xs_dict_get(msg, "client_id");
122 const char *csec = xs_dict_get(msg, "client_secret");
123 const char *ruri = xs_dict_get(msg, "redirect_uri");
124- const char *scope = xs_dict_get(msg, "scope");
125
126 if (gtype && code && cid && csec && ruri) {
127 xs *rsp = xs_dict_new();
128@@ -149,15 +204,18 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
129
130 rsp = xs_dict_append(rsp, "access_token", token);
131 rsp = xs_dict_append(rsp, "token_type", "Bearer");
132- rsp = xs_dict_append(rsp, "scope", scope);
133 rsp = xs_dict_append(rsp, "created_at", cat);
134
135 *body = xs_json_dumps_pp(rsp, 4);
136 *ctype = "application/json";
137 status = 200;
138+
139+ srv_debug(0, xs_fmt("oauth token: successful login, token %s", token));
140 }
141- else
142+ else {
143+ srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
144 status = 400;
145+ }
146 }
147 else
148 if (strcmp(cmd, "/revoke") == 0) {
149@@ -178,13 +236,42 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
150 }
151
152
153+int mastoapi_get_handler(const xs_dict *req, const char *q_path,
154+ char **body, int *b_size, char **ctype)
155+{
156+ if (!xs_startswith(q_path, "/api/v1/"))
157+ return 0;
158+
159+ {
160+ xs *j = xs_json_dumps_pp(req, 4);
161+ printf("mastoapi get:\n%s\n", j);
162+ }
163+
164+ int status = 404;
165+ xs_dict *msg = xs_dict_get(req, "q_vars");
166+ xs *cmd = xs_replace(q_path, "/api/v1", "");
167+
168+ srv_debug(0, xs_fmt("mastoapi_get_handler %s", q_path));
169+
170+ if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
171+ }
172+
173+ return status;
174+}
175+
176+
177 int mastoapi_post_handler(const xs_dict *req, const char *q_path,
178- const char *payload, int p_size,
179- char **body, int *b_size, char **ctype)
180+ const char *payload, int p_size,
181+ char **body, int *b_size, char **ctype)
182 {
183 if (!xs_startswith(q_path, "/api/v1/"))
184 return 0;
185
186+ {
187+ xs *j = xs_json_dumps_pp(req, 4);
188+ printf("mastoapi post:\n%s\n", j);
189+ }
190+
191 int status = 404;
192 xs *msg = NULL;
193 char *i_ctype = xs_dict_get(req, "content-type");
M
snac.h
+7,
-5
1@@ -224,11 +224,13 @@ int job_fifo_ready(void);
2 void job_post(const xs_val *job, int urgent);
3 void job_wait(xs_val **job);
4
5-int mastoapi_post_handler(const xs_dict *req, const char *q_path,
6- const char *payload, int p_size,
7- char **body, int *b_size, char **ctype);
8 int oauth_get_handler(const xs_dict *req, const char *q_path,
9 char **body, int *b_size, char **ctype);
10 int oauth_post_handler(const xs_dict *req, const char *q_path,
11- const char *payload, int p_size,
12- char **body, int *b_size, char **ctype);
13+ const char *payload, int p_size,
14+ char **body, int *b_size, char **ctype);
15+int mastoapi_get_handler(const xs_dict *req, const char *q_path,
16+ char **body, int *b_size, char **ctype);
17+int mastoapi_post_handler(const xs_dict *req, const char *q_path,
18+ const char *payload, int p_size,
19+ char **body, int *b_size, char **ctype);