commit 664f9cb

default  ·  2024-12-20 07:33:02 +0000 UTC
parent 29ac915
Mastoapi: added badlogin support.
1 files changed,  +62, -48
+62, -48
  1@@ -293,47 +293,54 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
  2             snac snac;
  3 
  4             if (user_open(&snac, login)) {
  5-                /* check the login + password */
  6-                if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  7-                    /* success! redirect to the desired uri */
  8-                    xs *code = random_str();
  9+                const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
 10+                                         xs_dict_get(req, "x-forwarded-for"));
 11 
 12-                    xs_free(*body);
 13+                if (badlogin_check(login, addr)) {
 14+                    /* check the login + password */
 15+                    if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
 16+                        /* success! redirect to the desired uri */
 17+                        xs *code = random_str();
 18 
 19-                    if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
 20-                        *body = xs_dup(code);
 21-                    }
 22-                    else {
 23-                        if (xs_str_in(redir, "?") != -1)
 24-                            *body = xs_fmt("%s&code=%s", redir, code);
 25-                        else
 26-                            *body = xs_fmt("%s?code=%s", redir, code);
 27+                        xs_free(*body);
 28 
 29-                        status = HTTP_STATUS_SEE_OTHER;
 30-                    }
 31+                        if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
 32+                            *body = xs_dup(code);
 33+                        }
 34+                        else {
 35+                            if (xs_str_in(redir, "?") != -1)
 36+                                *body = xs_fmt("%s&code=%s", redir, code);
 37+                            else
 38+                                *body = xs_fmt("%s?code=%s", redir, code);
 39 
 40-                    /* if there is a state, add it */
 41-                    if (!xs_is_null(state) && *state) {
 42-                        *body = xs_str_cat(*body, "&state=");
 43-                        *body = xs_str_cat(*body, state);
 44-                    }
 45+                            status = HTTP_STATUS_SEE_OTHER;
 46+                        }
 47 
 48-                    srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
 49+                        /* if there is a state, add it */
 50+                        if (!xs_is_null(state) && *state) {
 51+                            *body = xs_str_cat(*body, "&state=");
 52+                            *body = xs_str_cat(*body, state);
 53+                        }
 54+
 55+                        srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
 56                                    login, *body));
 57 
 58-                    /* assign the login to the app */
 59-                    xs *app = app_get(cid);
 60+                        /* assign the login to the app */
 61+                        xs *app = app_get(cid);
 62 
 63-                    if (app != NULL) {
 64-                        app = xs_dict_set(app, "uid",  login);
 65-                        app = xs_dict_set(app, "code", code);
 66-                        app_add(cid, app);
 67+                        if (app != NULL) {
 68+                            app = xs_dict_set(app, "uid",  login);
 69+                            app = xs_dict_set(app, "code", code);
 70+                            app_add(cid, app);
 71+                        }
 72+                        else
 73+                            srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
 74+                    }
 75+                    else {
 76+                        srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
 77+                        badlogin_inc(login, addr);
 78                     }
 79-                    else
 80-                        srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
 81                 }
 82-                else
 83-                    srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
 84 
 85                 user_free(&snac);
 86             }
 87@@ -474,29 +481,36 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
 88             snac user;
 89 
 90             if (user_open(&user, login)) {
 91-                /* check the login + password */
 92-                if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
 93-                    /* success! create a new token */
 94-                    xs *tokid = random_str();
 95+                const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
 96+                                         xs_dict_get(req, "x-forwarded-for"));
 97+
 98+                if (badlogin_check(login, addr)) {
 99+                    /* check the login + password */
100+                    if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
101+                        /* success! create a new token */
102+                        xs *tokid = random_str();
103 
104-                    srv_debug(1, xs_fmt("x-snac-new-token: "
105+                        srv_debug(1, xs_fmt("x-snac-new-token: "
106                                     "successful login for %s, new token %s", login, tokid));
107 
108-                    xs *token = xs_dict_new();
109-                    token = xs_dict_append(token, "token",         tokid);
110-                    token = xs_dict_append(token, "client_id",     "snac-client");
111-                    token = xs_dict_append(token, "client_secret", "");
112-                    token = xs_dict_append(token, "uid",           login);
113-                    token = xs_dict_append(token, "code",          "");
114+                        xs *token = xs_dict_new();
115+                        token = xs_dict_append(token, "token",         tokid);
116+                        token = xs_dict_append(token, "client_id",     "snac-client");
117+                        token = xs_dict_append(token, "client_secret", "");
118+                        token = xs_dict_append(token, "uid",           login);
119+                        token = xs_dict_append(token, "code",          "");
120 
121-                    token_add(tokid, token);
122+                        token_add(tokid, token);
123 
124-                    *ctype = "text/plain";
125-                    xs_free(*body);
126-                    *body = xs_dup(tokid);
127-                }
128+                        *ctype = "text/plain";
129+                        xs_free(*body);
130+                        *body = xs_dup(tokid);
131+                    }
132+                    else
133+                        badlogin_inc(login, addr);
134 
135-                user_free(&user);
136+                    user_free(&user);
137+                }
138             }
139         }
140     }