diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3b0902b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git* + +tools* +mysql* +letsencrypt* + +.env* \ No newline at end of file diff --git a/.envdev b/.envdev index 1d1be26..dc6fe19 100644 --- a/.envdev +++ b/.envdev @@ -5,7 +5,7 @@ WEB_PORT=3200 DB_HOSTNAME=database DB_DATABASE=auth DB_USERNAME=auth -DB_PASSWORD=venusaur +DB_PASSWORD=charizard MAIL_SMTP=smtp.example.com MAIL_USERNAME=foobar@example.com diff --git a/Dockerfile b/Dockerfile index 19950fb..cd0c90e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,8 @@ FROM node:15 WORKDIR "/app" COPY package*.json ./ -RUN npm install --production COPY . /app +RUN npm install --production EXPOSE 3200 USER node ENTRYPOINT ["bash", "-c"] diff --git a/package-lock.json b/package-lock.json index 3e00910..0bde151 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,26 +1,25 @@ { "name": "auth-server", - "version": "1.0.0", + "version": "1.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.0.0", + "version": "1.3.1", "license": "ISC", "dependencies": { "bcryptjs": "^2.4.3", - "body-parser": "^1.19.0", "cors": "^2.8.5", - "dotenv": "^8.2.0", + "dotenv": "^8.6.0", "express": "^4.17.1", "jsonwebtoken": "^8.5.1", - "mariadb": "^2.5.2", + "mariadb": "^2.5.4", "node-cron": "^2.0.3", - "nodemailer": "^6.5.0", - "sequelize": "^6.5.0" + "nodemailer": "^6.6.3", + "sequelize": "^6.6.5" }, "devDependencies": { - "nodemon": "^2.0.7" + "nodemon": "^2.0.12" } }, "node_modules/@sindresorhus/is": { @@ -732,20 +731,6 @@ "node": ">= 0.6" } }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/get-stream": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", @@ -2729,13 +2714,6 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, - "fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "optional": true - }, "get-stream": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", diff --git a/server/admin/ban-user.js b/server/admin/ban-user.js index 1128979..3980c33 100644 --- a/server/admin/ban-user.js +++ b/server/admin/ban-user.js @@ -9,7 +9,7 @@ const route = async (req, res) => { }, { where: { username: { - [Op.eq]: req.body.username + [Op.eq]: req.body.username || '' }, admin: { [Op.not]: true @@ -27,7 +27,7 @@ const route = async (req, res) => { //forcibly logout tokens.destroy({ where: { - username: req.body.username + username: req.body.username || '' } }); diff --git a/server/admin/default-account.js b/server/admin/default-account.js index 47ad0ee..b9398eb 100644 --- a/server/admin/default-account.js +++ b/server/admin/default-account.js @@ -25,8 +25,9 @@ module.exports = async () => { }); if (adminRecord == null) { + const webAddress = process.env.WEB_ADDRESS == 'localhost' ? 'example.com' : process.env.WEB_ADDRESS; //can't log in as "localhost" await accounts.create({ - email: `${process.env.ADMIN_DEFAULT_USERNAME}@${process.env.WEB_ADDRESS}`, + email: `${process.env.ADMIN_DEFAULT_USERNAME}@${webAddress}`, username: `${process.env.ADMIN_DEFAULT_USERNAME}`, hash: await bcrypt.hash(`${process.env.ADMIN_DEFAULT_PASSWORD}`, await bcrypt.genSalt(11)), type: 'normal', @@ -34,6 +35,6 @@ module.exports = async () => { mod: true }); - console.warn(`Created default admin account (email: ${process.env.ADMIN_DEFAULT_USERNAME}@${process.env.WEB_ADDRESS}; password: ${process.env.ADMIN_DEFAULT_PASSWORD})`); + console.warn(`Created default admin account (email: ${process.env.ADMIN_DEFAULT_USERNAME}@${webAddress}; password: ${process.env.ADMIN_DEFAULT_PASSWORD})`); } }; diff --git a/server/admin/grant-admin.js b/server/admin/grant-admin.js index a67dc39..5440f1c 100644 --- a/server/admin/grant-admin.js +++ b/server/admin/grant-admin.js @@ -10,7 +10,7 @@ const route = async (req, res) => { }, { where: { username: { - [Op.eq]: req.body.username + [Op.eq]: req.body.username || '' } } }); diff --git a/server/admin/grant-mod.js b/server/admin/grant-mod.js index 85c08ef..785d3ae 100644 --- a/server/admin/grant-mod.js +++ b/server/admin/grant-mod.js @@ -9,7 +9,7 @@ const route = async (req, res) => { }, { where: { username: { - [Op.eq]: req.body.username + [Op.eq]: req.body.username || '' } } }); diff --git a/server/admin/index.js b/server/admin/index.js index 0ca330d..38c6751 100644 --- a/server/admin/index.js +++ b/server/admin/index.js @@ -12,7 +12,7 @@ router.use(tokenAuth); router.use(async (req, res, next) => { const record = await accounts.findOne({ where: { - username: req.user.username + username: req.user.username || '' } }); diff --git a/server/admin/remove-admin.js b/server/admin/remove-admin.js index 8e4c53d..08fd22e 100644 --- a/server/admin/remove-admin.js +++ b/server/admin/remove-admin.js @@ -9,7 +9,7 @@ const route = async (req, res) => { }, { where: { username: { - [Op.eq]: req.body.username + [Op.eq]: req.body.username || '' } } }); diff --git a/server/admin/remove-mod.js b/server/admin/remove-mod.js index bebd220..bcd9814 100644 --- a/server/admin/remove-mod.js +++ b/server/admin/remove-mod.js @@ -10,7 +10,7 @@ const route = async (req, res) => { }, { where: { username: { - [Op.eq]: req.body.username + [Op.eq]: req.body.username || '' } } }); diff --git a/server/auth/account-delete.js b/server/auth/account-delete.js index 239936b..6e7b782 100644 --- a/server/auth/account-delete.js +++ b/server/auth/account-delete.js @@ -11,7 +11,7 @@ const { accounts } = require('../database/models'); const route = async (req, res) => { const account = await accounts.findOne({ where: { - id: req.user.id + index: req.user.index } }); @@ -30,7 +30,7 @@ const route = async (req, res) => { }, { where: { - id: req.user.id + index: req.user.index } }); diff --git a/server/auth/account-query.js b/server/auth/account-query.js index 91bbed2..9350da7 100644 --- a/server/auth/account-query.js +++ b/server/auth/account-query.js @@ -4,7 +4,7 @@ const { accounts } = require('../database/models'); const route = async (req, res) => { const account = await accounts.findOne({ where: { - id: req.user.id + index: req.user.index } }); diff --git a/server/auth/account-update.js b/server/auth/account-update.js index 64acab1..74a7476 100644 --- a/server/auth/account-update.js +++ b/server/auth/account-update.js @@ -16,7 +16,7 @@ const route = async (req, res) => { hash: hash }, { where: { - id: req.user.id + index: req.user.index } }); diff --git a/server/auth/index.js b/server/auth/index.js index b0a79c6..fd445a1 100644 --- a/server/auth/index.js +++ b/server/auth/index.js @@ -20,7 +20,7 @@ router.use(tokenAuth); router.use(async (req, res, next) => { const record = await accounts.findOne({ where: { - username: req.user.username + username: req.user.username || '' } }); diff --git a/server/auth/login.js b/server/auth/login.js index 8140da0..d88fb12 100644 --- a/server/auth/login.js +++ b/server/auth/login.js @@ -19,7 +19,7 @@ const route = async (req, res) => { //get the existing account const account = await accounts.findOne({ where: { - email: req.body.email + email: req.body.email || '' } }); @@ -38,7 +38,7 @@ const route = async (req, res) => { //cancel deletion if any await accounts.update({ deletion: null }, { where: { - id: account.id + index: account.index } }); @@ -48,7 +48,7 @@ const route = async (req, res) => { } //generate the JWT - const tokens = generate(account.id, account.username, account.type, account.admin, account.mod); + const tokens = generate(account.index, account.username, account.type, account.admin, account.mod); //finally res.status(200).json(tokens); diff --git a/server/auth/signup.js b/server/auth/signup.js index 82bfe31..3779a62 100644 --- a/server/auth/signup.js +++ b/server/auth/signup.js @@ -70,7 +70,7 @@ const validateDetails = async (body) => { //check for existing username const usernameRecord = await accounts.findOne({ where: { - username: body.username + username: body.username || '' } }); diff --git a/server/auth/validation.js b/server/auth/validation.js index 75bf13e..0227d4b 100644 --- a/server/auth/validation.js +++ b/server/auth/validation.js @@ -5,7 +5,7 @@ const route = async (req, res) => { //get the existing pending signup const info = await pendingSignups.findOne({ where: { - username: req.query.username + username: req.query.username || '' } }); @@ -29,7 +29,7 @@ const route = async (req, res) => { //delete the pending signup pendingSignups.destroy({ where: { - username: req.query.username + username: req.query.username || '' } }); diff --git a/server/database/models/accounts.js b/server/database/models/accounts.js index 2bd7625..f034740 100644 --- a/server/database/models/accounts.js +++ b/server/database/models/accounts.js @@ -2,7 +2,7 @@ const Sequelize = require('sequelize'); const sequelize = require('..'); module.exports = sequelize.define('accounts', { - id: { + index: { type: Sequelize.INTEGER(11), allowNull: false, autoIncrement: true, diff --git a/server/database/models/tokens.js b/server/database/models/tokens.js index c7dfb8c..519d9ae 100644 --- a/server/database/models/tokens.js +++ b/server/database/models/tokens.js @@ -3,5 +3,5 @@ const sequelize = require('..'); module.exports = sequelize.define('tokens', { token: 'varchar(320)', - username: 'varchar(320)' + username: 'varchar(320)' //TODO: why username? }); diff --git a/server/server.js b/server/server.js index fbd5186..254b9f7 100644 --- a/server/server.js +++ b/server/server.js @@ -5,11 +5,10 @@ require('dotenv').config(); const express = require('express'); const app = express(); const server = require('http').Server(app); -const bodyParser = require('body-parser'); const cors = require('cors'); //config -app.use(bodyParser.json()); +app.use(express.json()); app.use(cors()); //database connection diff --git a/server/utilities/token-generate.js b/server/utilities/token-generate.js index 3f70394..47bfb57 100644 --- a/server/utilities/token-generate.js +++ b/server/utilities/token-generate.js @@ -2,9 +2,9 @@ const jwt = require('jsonwebtoken'); const { tokens } = require('../database/models'); //generates a JWT token based on the given arguments -module.exports = (id, username, type, admin, mod) => { +module.exports = (index, username, type, admin, mod) => { const content = { - id, + index, username, type, admin, diff --git a/server/utilities/token-refresh.js b/server/utilities/token-refresh.js index f8d7b15..9807ca5 100644 --- a/server/utilities/token-refresh.js +++ b/server/utilities/token-refresh.js @@ -24,7 +24,7 @@ module.exports = (token, callback) => { return callback(403); } - const result = generate(user.id, user.username, user.type, user.admin, user.mod); + const result = generate(user.index, user.username, user.type, user.admin, user.mod); destroy(token); diff --git a/server/utilities/validate-username.js b/server/utilities/validate-username.js index 62e8f80..a890006 100644 --- a/server/utilities/validate-username.js +++ b/server/utilities/validate-username.js @@ -6,11 +6,11 @@ module.exports = username => { if (username.length < 8 && username.length > 100) { return false; } - + if (!isAlpha(username)) { return false; } - + return true; }