commit ec6f94e

default  ·  2023-05-04 09:52:04 +0000 UTC
parent f6ef275
New url /oauth/x-snac-get-token.
1 files changed,  +52, -0
+52, -0
 1@@ -221,6 +221,16 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
 2         else
 3             srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
 4     }
 5+    else
 6+    if (strcmp(cmd, "/x-snac-get-token") == 0) {
 7+        const char *host = xs_dict_get(srv_config, "host");
 8+
 9+        *body  = xs_fmt(login_page, host, "", host, "oauth/x-snac-get-token",
10+                        "", "", "", USER_AGENT);
11+        *ctype = "text/html";
12+        status = 200;
13+
14+    }
15 
16     return status;
17 }
18@@ -427,6 +437,48 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
19             status = 403;
20         }
21     }
22+    if (strcmp(cmd, "/x-snac-get-token") == 0) {
23+        const char *login  = xs_dict_get(args, "login");
24+        const char *passwd = xs_dict_get(args, "passwd");
25+
26+        const char *host = xs_dict_get(srv_config, "host");
27+
28+        /* by default, generate another login form with an error */
29+        *body  = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
30+                        "", "", "", USER_AGENT);
31+        *ctype = "text/html";
32+        status = 200;
33+
34+        if (login && passwd) {
35+            snac user;
36+
37+            if (user_open(&user, login)) {
38+                /* check the login + password */
39+                if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
40+                    /* success! create a new token */
41+                    xs *tokid = random_str();
42+
43+                    srv_debug(1, xs_fmt("x-snac-new-token: "
44+                                    "successful login for %s, new token %s", login, tokid));
45+
46+                    xs *token = xs_dict_new();
47+                    token = xs_dict_append(token, "token",         tokid);
48+                    token = xs_dict_append(token, "client_id",     "snac-client");
49+                    token = xs_dict_append(token, "client_secret", "");
50+                    token = xs_dict_append(token, "uid",           login);
51+                    token = xs_dict_append(token, "code",          "");
52+
53+                    token_add(tokid, token);
54+
55+                    *ctype = "text/plain";
56+                    xs_free(*body);
57+                    *body = xs_dup(tokid);
58+                }
59+
60+                user_free(&user);
61+            }
62+        }
63+    }
64 
65     return status;
66 }