X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Froutes%2Fusers.js;h=a3fa70619381f52b8b0d94ee9a7222370db54bf4;hb=ad65975c9150ac761c7e7c6696930d4e9e87396c;hp=389625c1f3d46a3079034d3fd2fe82d10177bc23;hpb=d9a7a1e40254bda6e545514596a7363048c084f9;p=vchess.git diff --git a/server/routes/users.js b/server/routes/users.js index 389625c1..a3fa7061 100644 --- a/server/routes/users.js +++ b/server/routes/users.js @@ -4,25 +4,47 @@ const sendEmail = require('../utils/mailer'); const genToken = require("../utils/tokenGenerator"); const access = require("../utils/access"); const params = require("../config/parameters"); +const sanitizeHtml_pkg = require('sanitize-html'); + +const allowedTags = [ + 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'li', 'b', + 'i', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div', 'table', + 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre' +]; +function sanitizeHtml(text) { + return sanitizeHtml_pkg(text, { allowedTags: allowedTags }); +} + +router.get("/userbio", access.ajax, (req,res) => { + const uid = req.query["id"]; + if (!!(uid.toString().match(/^[0-9]+$/))) { + UserModel.getBio(uid, (err, bio) => { + res.json(bio); + }); + } +}); + +router.put('/userbio', access.logged, access.ajax, (req,res) => { + const bio = sanitizeHtml(req.body.bio); + UserModel.setBio(req.userId, bio); + res.json({}); +}); router.post('/register', access.unlogged, access.ajax, (req,res) => { const name = req.body.name; const email = req.body.email; const notify = !!req.body.notify; - if (UserModel.checkNameEmail({name: name, email: email})) - { - UserModel.create(name, email, notify, (err,ret) => { - if (err) - { + if (UserModel.checkNameEmail({name: name, email: email})) { + UserModel.create(name, email, notify, (err, ret) => { + if (!!err) { const msg = err.code == "SQLITE_CONSTRAINT" ? "User name or email already in use" : "User creation failed. Try again"; res.json({errmsg: msg}); } - else - { + else { const user = { - id: ret.uid, + id: ret.id, name: name, email: email, }; @@ -40,14 +62,17 @@ router.get("/whoami", access.ajax, (req,res) => { name: user.name, email: user.email, id: user.id, - notify: user.notify, + notify: user.notify }); }; - const anonymous = {name:"", email:"", id:0, notify:false}; - if (!req.cookies.token) - callback(anonymous); - else if (req.cookies.token.match(/^[a-z0-9]+$/)) - { + const anonymous = { + name: "", + email: "", + id: 0, + notify: false + }; + if (!req.cookies.token) callback(anonymous); + else if (req.cookies.token.match(/^[a-z0-9]+$/)) { UserModel.getOne("sessionToken", req.cookies.token, (err, user) => { callback(user || anonymous); }); @@ -57,10 +82,10 @@ router.get("/whoami", access.ajax, (req,res) => { // NOTE: this method is safe because only IDs and names are returned router.get("/users", access.ajax, (req,res) => { const ids = req.query["ids"]; - if (ids.match(/^([0-9]+,?)+$/)) //NOTE: slightly too permissive - { - UserModel.getByIds(ids, (err,users) => { - res.json({users:users}); + // NOTE: slightly too permissive RegExp + if (ids.match(/^([0-9]+,?)+$/)) { + UserModel.getByIds(ids, (err, users) => { + res.json({ users:users }); }); } }); @@ -68,8 +93,7 @@ router.get("/users", access.ajax, (req,res) => { router.put('/update', access.logged, access.ajax, (req,res) => { const name = req.body.name; const email = req.body.email; - if (UserModel.checkNameEmail({name: name, email: email})); - { + if (UserModel.checkNameEmail({name: name, email: email})) { const user = { id: req.userId, name: name, @@ -81,17 +105,10 @@ router.put('/update', access.logged, access.ajax, (req,res) => { } }); -// Special route to update newsRead timestamp: -router.put('/newsread', access.logged, access.ajax, (req,res) => { - UserModel.setNewsRead(req.userId); - res.json({}); -}); - // Authentication-related methods: // to: object user (to who we send an email) -function setAndSendLoginToken(subject, to, res) -{ +function setAndSendLoginToken(subject, to, res) { // Set login token and send welcome(back) email with auth link const token = genToken(params.token.length); UserModel.setLoginToken(token, to.id); @@ -108,8 +125,7 @@ function setAndSendLoginToken(subject, to, res) router.get('/sendtoken', access.unlogged, access.ajax, (req,res) => { const nameOrEmail = decodeURIComponent(req.query.nameOrEmail); const type = (nameOrEmail.indexOf('@') >= 0 ? "email" : "name"); - if (UserModel.checkNameEmail({[type]: nameOrEmail})) - { + if (UserModel.checkNameEmail({[type]: nameOrEmail})) { UserModel.getOne(type, nameOrEmail, (err,user) => { access.checkRequest(res, err, user, "Unknown user", () => { setAndSendLoginToken("Token for " + params.siteURL, user, res); @@ -127,8 +143,7 @@ router.get('/authenticate', access.unlogged, access.ajax, (req,res) => { // If token older than params.tokenExpire, do nothing if (Date.now() > user.loginTime + params.token.expire) res.json({errmsg: "Token expired"}); - else - { + else { // Generate session token (if not exists) + destroy login token UserModel.trySetSessionToken(user.id, (token) => { res.cookie("token", token, {