canopy/src/validators/accountValidator.js

180 lines
4.6 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const { checkSchema } = require('express-validator');
//local imports
const {isRank} = require('./permissionsValidator');
module.exports.user = function(field = 'user'){
return checkSchema({
[field]: {
escape: true,
trim: true,
isAlphanumeric: {
errorMessage: "Usernames must be alphanumeric."
},
isLength: {
options: {
min: 1,
max: 22
},
errorMessage: "Usernames must be between 1 and 22 characters."
},
}
});
}
function getPassSchema(field = 'pass'){
//Heavily simplified from previous versions.
//Trimming passwords is iffy, and escaping them is a down-right bad idea
return {
[field]: {
notEmpty: true,
}
}
}
module.exports.pass = function(field = 'pass'){
return checkSchema(getPassSchema(field));
}
module.exports.securePass = function(field = 'pass'){
const schema = getPassSchema(field);
schema[field].isStrongPassword = {
options: {
minLength: 8,
minLowercase: 1,
minUppercase: 1,
minNumbers: 1,
minSymbols: 1
},
errorMessage: "Passwords must contain 8 characters, including at least one: Upper, Lower, Number, and Special char."
}
return checkSchema(schema);
}
module.exports.email = function(field = 'email'){
return checkSchema({
[field]: {
optional: true,
isEmail: {
errorMessage: "Invalid E-Mail Address"
},
normalizeEmail: true
}
});
}
module.exports.img = function(field = 'img'){
return checkSchema({
[field]: {
optional: true,
isURL: {
options: {
require_tld: false,
require_host: false
},
errorMessage: "Invalid URL."
},
trim: true
}
});
}
module.exports.pronouns = function(field = 'pronouns'){
return checkSchema({
[field]: {
optional: true,
trim: true,
isLength: {
options: {
min: 0,
max: 15
},
errorMessage: "Pronouns must be under 15 characters."
},
escape: true
}
});
}
module.exports.signature = function(field = 'signature'){
return checkSchema({
[field]: {
optional: true,
trim: true,
isLength: {
options: {
min: 1,
max: 25
},
errorMessage: "Signature must be between 1 and 25 characters."
},
escape: true
}
});
}
module.exports.bio = function(field = 'bio'){
return checkSchema({
[field]: {
optional: true,
trim: true,
isLength: {
options: {
min: 1,
max: 1000
},
errorMessage: "Bio must be between 1 and 1000 characters."
},
escape: true
}
});
}
module.exports.rank = function(field = 'rank'){
return checkSchema({
[field]: {
escape: true,
trim: true,
custom: {
options: isRank,
},
errorMessage: "Invalid rank."
}
});
}
module.exports.securityToken = function(field = 'token'){
return checkSchema({
[field]: {
escape: true,
trim: true,
isHexadecimal: true,
isLength: {
options: {
min: 32,
max: 32
}
},
errorMessage: "Invalid security token."
}
});
}