Merged private changes

This commit is contained in:
2021-07-23 19:32:56 +10:00
24 changed files with 45 additions and 60 deletions
+7
View File
@@ -0,0 +1,7 @@
.git*
tools*
mysql*
letsencrypt*
.env*
+1 -1
View File
@@ -5,7 +5,7 @@ WEB_PORT=3200
DB_HOSTNAME=database DB_HOSTNAME=database
DB_DATABASE=auth DB_DATABASE=auth
DB_USERNAME=auth DB_USERNAME=auth
DB_PASSWORD=venusaur DB_PASSWORD=charizard
MAIL_SMTP=smtp.example.com MAIL_SMTP=smtp.example.com
MAIL_USERNAME=foobar@example.com MAIL_USERNAME=foobar@example.com
+1 -1
View File
@@ -2,8 +2,8 @@
FROM node:15 FROM node:15
WORKDIR "/app" WORKDIR "/app"
COPY package*.json ./ COPY package*.json ./
RUN npm install --production
COPY . /app COPY . /app
RUN npm install --production
EXPOSE 3200 EXPOSE 3200
USER node USER node
ENTRYPOINT ["bash", "-c"] ENTRYPOINT ["bash", "-c"]
+7 -29
View File
@@ -1,26 +1,25 @@
{ {
"name": "auth-server", "name": "auth-server",
"version": "1.0.0", "version": "1.3.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"version": "1.0.0", "version": "1.3.1",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^8.2.0", "dotenv": "^8.6.0",
"express": "^4.17.1", "express": "^4.17.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"mariadb": "^2.5.2", "mariadb": "^2.5.4",
"node-cron": "^2.0.3", "node-cron": "^2.0.3",
"nodemailer": "^6.5.0", "nodemailer": "^6.6.3",
"sequelize": "^6.5.0" "sequelize": "^6.6.5"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^2.0.7" "nodemon": "^2.0.12"
} }
}, },
"node_modules/@sindresorhus/is": { "node_modules/@sindresorhus/is": {
@@ -732,20 +731,6 @@
"node": ">= 0.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": { "node_modules/get-stream": {
"version": "4.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", "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", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" "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": { "get-stream": {
"version": "4.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz",
+2 -2
View File
@@ -9,7 +9,7 @@ const route = async (req, res) => {
}, { }, {
where: { where: {
username: { username: {
[Op.eq]: req.body.username [Op.eq]: req.body.username || ''
}, },
admin: { admin: {
[Op.not]: true [Op.not]: true
@@ -27,7 +27,7 @@ const route = async (req, res) => {
//forcibly logout //forcibly logout
tokens.destroy({ tokens.destroy({
where: { where: {
username: req.body.username username: req.body.username || ''
} }
}); });
+3 -2
View File
@@ -25,8 +25,9 @@ module.exports = async () => {
}); });
if (adminRecord == null) { 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({ 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}`, username: `${process.env.ADMIN_DEFAULT_USERNAME}`,
hash: await bcrypt.hash(`${process.env.ADMIN_DEFAULT_PASSWORD}`, await bcrypt.genSalt(11)), hash: await bcrypt.hash(`${process.env.ADMIN_DEFAULT_PASSWORD}`, await bcrypt.genSalt(11)),
type: 'normal', type: 'normal',
@@ -34,6 +35,6 @@ module.exports = async () => {
mod: true 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})`);
} }
}; };
+1 -1
View File
@@ -10,7 +10,7 @@ const route = async (req, res) => {
}, { }, {
where: { where: {
username: { username: {
[Op.eq]: req.body.username [Op.eq]: req.body.username || ''
} }
} }
}); });
+1 -1
View File
@@ -9,7 +9,7 @@ const route = async (req, res) => {
}, { }, {
where: { where: {
username: { username: {
[Op.eq]: req.body.username [Op.eq]: req.body.username || ''
} }
} }
}); });
+1 -1
View File
@@ -12,7 +12,7 @@ router.use(tokenAuth);
router.use(async (req, res, next) => { router.use(async (req, res, next) => {
const record = await accounts.findOne({ const record = await accounts.findOne({
where: { where: {
username: req.user.username username: req.user.username || ''
} }
}); });
+1 -1
View File
@@ -9,7 +9,7 @@ const route = async (req, res) => {
}, { }, {
where: { where: {
username: { username: {
[Op.eq]: req.body.username [Op.eq]: req.body.username || ''
} }
} }
}); });
+1 -1
View File
@@ -10,7 +10,7 @@ const route = async (req, res) => {
}, { }, {
where: { where: {
username: { username: {
[Op.eq]: req.body.username [Op.eq]: req.body.username || ''
} }
} }
}); });
+2 -2
View File
@@ -11,7 +11,7 @@ const { accounts } = require('../database/models');
const route = async (req, res) => { const route = async (req, res) => {
const account = await accounts.findOne({ const account = await accounts.findOne({
where: { where: {
id: req.user.id index: req.user.index
} }
}); });
@@ -30,7 +30,7 @@ const route = async (req, res) => {
}, },
{ {
where: { where: {
id: req.user.id index: req.user.index
} }
}); });
+1 -1
View File
@@ -4,7 +4,7 @@ const { accounts } = require('../database/models');
const route = async (req, res) => { const route = async (req, res) => {
const account = await accounts.findOne({ const account = await accounts.findOne({
where: { where: {
id: req.user.id index: req.user.index
} }
}); });
+1 -1
View File
@@ -16,7 +16,7 @@ const route = async (req, res) => {
hash: hash hash: hash
}, { }, {
where: { where: {
id: req.user.id index: req.user.index
} }
}); });
+1 -1
View File
@@ -20,7 +20,7 @@ router.use(tokenAuth);
router.use(async (req, res, next) => { router.use(async (req, res, next) => {
const record = await accounts.findOne({ const record = await accounts.findOne({
where: { where: {
username: req.user.username username: req.user.username || ''
} }
}); });
+3 -3
View File
@@ -19,7 +19,7 @@ const route = async (req, res) => {
//get the existing account //get the existing account
const account = await accounts.findOne({ const account = await accounts.findOne({
where: { where: {
email: req.body.email email: req.body.email || ''
} }
}); });
@@ -38,7 +38,7 @@ const route = async (req, res) => {
//cancel deletion if any //cancel deletion if any
await accounts.update({ deletion: null }, { await accounts.update({ deletion: null }, {
where: { where: {
id: account.id index: account.index
} }
}); });
@@ -48,7 +48,7 @@ const route = async (req, res) => {
} }
//generate the JWT //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 //finally
res.status(200).json(tokens); res.status(200).json(tokens);
+1 -1
View File
@@ -70,7 +70,7 @@ const validateDetails = async (body) => {
//check for existing username //check for existing username
const usernameRecord = await accounts.findOne({ const usernameRecord = await accounts.findOne({
where: { where: {
username: body.username username: body.username || ''
} }
}); });
+2 -2
View File
@@ -5,7 +5,7 @@ const route = async (req, res) => {
//get the existing pending signup //get the existing pending signup
const info = await pendingSignups.findOne({ const info = await pendingSignups.findOne({
where: { where: {
username: req.query.username username: req.query.username || ''
} }
}); });
@@ -29,7 +29,7 @@ const route = async (req, res) => {
//delete the pending signup //delete the pending signup
pendingSignups.destroy({ pendingSignups.destroy({
where: { where: {
username: req.query.username username: req.query.username || ''
} }
}); });
+1 -1
View File
@@ -2,7 +2,7 @@ const Sequelize = require('sequelize');
const sequelize = require('..'); const sequelize = require('..');
module.exports = sequelize.define('accounts', { module.exports = sequelize.define('accounts', {
id: { index: {
type: Sequelize.INTEGER(11), type: Sequelize.INTEGER(11),
allowNull: false, allowNull: false,
autoIncrement: true, autoIncrement: true,
+1 -1
View File
@@ -3,5 +3,5 @@ const sequelize = require('..');
module.exports = sequelize.define('tokens', { module.exports = sequelize.define('tokens', {
token: 'varchar(320)', token: 'varchar(320)',
username: 'varchar(320)' username: 'varchar(320)' //TODO: why username?
}); });
+1 -2
View File
@@ -5,11 +5,10 @@ require('dotenv').config();
const express = require('express'); const express = require('express');
const app = express(); const app = express();
const server = require('http').Server(app); const server = require('http').Server(app);
const bodyParser = require('body-parser');
const cors = require('cors'); const cors = require('cors');
//config //config
app.use(bodyParser.json()); app.use(express.json());
app.use(cors()); app.use(cors());
//database connection //database connection
+2 -2
View File
@@ -2,9 +2,9 @@ const jwt = require('jsonwebtoken');
const { tokens } = require('../database/models'); const { tokens } = require('../database/models');
//generates a JWT token based on the given arguments //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 = { const content = {
id, index,
username, username,
type, type,
admin, admin,
+1 -1
View File
@@ -24,7 +24,7 @@ module.exports = (token, callback) => {
return callback(403); 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); destroy(token);
+2 -2
View File
@@ -6,11 +6,11 @@ module.exports = username => {
if (username.length < 8 && username.length > 100) { if (username.length < 8 && username.length > 100) {
return false; return false;
} }
if (!isAlpha(username)) { if (!isAlpha(username)) {
return false; return false;
} }
return true; return true;
} }