Improved exception handling, started work on improving error messages for IP bans
This commit is contained in:
parent
7d3c31f0aa
commit
853f67fe15
15 changed files with 118 additions and 77 deletions
|
|
@ -27,7 +27,8 @@ const crypto = require("node:crypto");
|
|||
const {mongoose} = require('mongoose');
|
||||
|
||||
//Local Imports
|
||||
const hashUtil = require('../../utils/hashUtils');
|
||||
const hashUtil = require('../../utils/hashUtils.js');
|
||||
const loggerUtils = require('../../utils/loggerUtils.js')
|
||||
|
||||
const daysToExpire = 7;
|
||||
|
||||
|
|
@ -85,7 +86,7 @@ passwordResetSchema.statics.processExpiredRequests = async function(){
|
|||
passwordResetSchema.methods.consume = async function(pass, confirmPass){
|
||||
//Check confirmation pass
|
||||
if(pass != confirmPass){
|
||||
throw new Error("Confirmation password does not match!");
|
||||
throw loggerUtils.exceptionSmith("Confirmation password does not match!", "validation");
|
||||
}
|
||||
|
||||
//Populate the user reference
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {mongoose} = require('mongoose');
|
||||
|
||||
//Local Imports
|
||||
const hashUtil = require('../../utils/hashUtils');
|
||||
const {userModel} = require('./userSchema');
|
||||
const hashUtil = require('../../utils/hashUtils.js');
|
||||
const {userModel} = require('./userSchema.js');
|
||||
const loggerUtils = require('../../utils/loggerUtils.js');
|
||||
|
||||
const userBanSchema = new mongoose.Schema({
|
||||
user: {
|
||||
|
|
@ -184,21 +185,21 @@ userBanSchema.statics.checkProcessedBans = async function(user){
|
|||
userBanSchema.statics.banByUserDoc = async function(userDB, permanent, expirationDays, ipBan = false){
|
||||
//Prevent missing users
|
||||
if(userDB == null){
|
||||
throw new Error("User not found")
|
||||
throw loggerUtils.exceptionSmith("User not found", "validation");
|
||||
}
|
||||
|
||||
//Ensure the user isn't already banned
|
||||
if(await this.checkBanByUserDoc(userDB) != null){
|
||||
throw new Error("User already banned");
|
||||
throw loggerUtils.exceptionSmith("User already banned", "validation");
|
||||
}
|
||||
|
||||
//Verify time to expire/delete depending on action
|
||||
if(expirationDays < 0){
|
||||
throw new Error("Expiration Days must be a positive integer!");
|
||||
throw loggerUtils.exceptionSmith("Expiration Days must be a positive integer!", "validation");
|
||||
}else if(expirationDays < 30 && permanent){
|
||||
throw new Error("Permanent bans must be given at least 30 days before automatic account deletion!");
|
||||
throw loggerUtils.exceptionSmith("Permanent bans must be given at least 30 days before automatic account deletion!", "validation");
|
||||
}else if(expirationDays > 185){
|
||||
throw new Error("Expiration/Deletion date cannot be longer than half a year out from the original ban date.");
|
||||
throw loggerUtils.exceptionSmith("Expiration/Deletion date cannot be longer than half a year out from the original ban date.", "validation");
|
||||
}
|
||||
|
||||
await banSessions(userDB);
|
||||
|
|
@ -266,13 +267,13 @@ userBanSchema.statics.unbanByUserDoc = async function(userDB){
|
|||
|
||||
//Prevent missing users
|
||||
if(userDB == null){
|
||||
throw new Error("User not found")
|
||||
throw loggerUtils.exceptionSmith("User not found", "validation");
|
||||
}
|
||||
|
||||
const banDB = await this.checkBanByUserDoc(userDB);
|
||||
|
||||
if(!banDB){
|
||||
throw new Error("User already un-banned");
|
||||
throw loggerUtils.exceptionSmith("User already un-banned", "validation");
|
||||
}
|
||||
|
||||
//Use _id in-case mongoose wants to be a cunt
|
||||
|
|
@ -284,7 +285,7 @@ userBanSchema.statics.unbanDeleted = async function(user){
|
|||
const banDB = await this.checkProcessedBans(user);
|
||||
|
||||
if(!banDB){
|
||||
throw new Error("User already un-banned");
|
||||
throw loggerUtils.exceptionSmith("User already un-banned", "validation");
|
||||
}
|
||||
|
||||
const oldBan = await this.deleteOne({_id: banDB._id});
|
||||
|
|
|
|||
|
|
@ -14,9 +14,6 @@ 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/>.*/
|
||||
|
||||
//Node Imports
|
||||
const { profile } = require('console');
|
||||
|
||||
//NPM Imports
|
||||
const {mongoose} = require('mongoose');
|
||||
|
||||
|
|
@ -33,6 +30,7 @@ const playlistSchema = require('../channel/media/playlistSchema');
|
|||
//Utils
|
||||
const hashUtil = require('../../utils/hashUtils');
|
||||
const mailUtil = require('../../utils/mailUtils');
|
||||
const loggerUtils = require('../../utils/loggerUtils')
|
||||
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
|
|
@ -161,7 +159,7 @@ userSchema.pre('save', async function (next){
|
|||
await this.populate('flair');
|
||||
|
||||
if(permissionModel.rankToNum(this.rank) < permissionModel.rankToNum(this.flair.rank)){
|
||||
throw new Error(`User '${this.user}' does not have a high enough rank for flair '${this.flair.displayName}'!`);
|
||||
throw loggerUtils.exceptionSmith(`User '${this.user}' does not have a high enough rank for flair '${this.flair.displayName}'!`, "unauthorized");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -223,7 +221,7 @@ userSchema.statics.register = async function(userObj, ip){
|
|||
|
||||
//If the user is found or someones trying to impersonate tokeboi
|
||||
if(userDB || user.toLowerCase() == "tokebot"){
|
||||
throw new Error("User name/email already taken!");
|
||||
throw loggerUtils.exceptionSmith("User name/email already taken!", "validation");
|
||||
}else{
|
||||
//Increment the user count, pulling the id to tattoo to the user
|
||||
const id = await statModel.incrementUserCount();
|
||||
|
|
@ -242,14 +240,14 @@ userSchema.statics.register = async function(userObj, ip){
|
|||
}
|
||||
}
|
||||
}else{
|
||||
throw new Error("Confirmation password doesn't match!");
|
||||
throw loggerUtils.exceptionSmith("Confirmation password doesn't match!", "validation");
|
||||
}
|
||||
}
|
||||
|
||||
userSchema.statics.authenticate = async function(user, pass, failLine = "Bad Username or Password."){
|
||||
//check for missing pass
|
||||
if(!user || !pass){
|
||||
throw new Error("Missing user/pass.");
|
||||
throw loggerUtils.exceptionSmith("Missing user/pass.", "validation");
|
||||
}
|
||||
|
||||
//get the user if it exists
|
||||
|
|
@ -268,9 +266,9 @@ userSchema.statics.authenticate = async function(user, pass, failLine = "Bad Use
|
|||
badLogin();
|
||||
}
|
||||
|
||||
//standardize bad login response so it's unknowin which is bad for security reasons.
|
||||
//standardize bad login response so it's unknown which is bad for security reasons.
|
||||
function badLogin(){
|
||||
throw new Error(failLine);
|
||||
throw loggerUtils.exceptionSmith(failLine, "unauthorized");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -688,11 +686,11 @@ userSchema.methods.changePassword = async function(passChange){
|
|||
await this.killAllSessions("Your password has been reset.");
|
||||
}else{
|
||||
//confirmation pass doesn't match
|
||||
throw new Error("Mismatched confirmation password!");
|
||||
throw loggerUtils.exceptionSmith("Mismatched confirmation password!", "validation");
|
||||
}
|
||||
}else{
|
||||
//Old password wrong
|
||||
throw new Error("Incorrect Password!");
|
||||
throw loggerUtils.exceptionSmith("Incorrect Password!", "validation");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -716,7 +714,7 @@ userSchema.methods.nuke = async function(pass){
|
|||
//Check we have a confirmation password
|
||||
if(pass == "" || pass == null){
|
||||
//scream and shout
|
||||
throw new Error("No confirmation password!");
|
||||
throw loggerUtils.exceptionSmith("No confirmation password!", "validation");
|
||||
}
|
||||
|
||||
//Check that the password is correct
|
||||
|
|
@ -725,7 +723,7 @@ userSchema.methods.nuke = async function(pass){
|
|||
var oldUser = await this.deleteOne();
|
||||
}else{
|
||||
//complain about a bad pass
|
||||
throw new Error("Bad pass.");
|
||||
throw loggerUtils.exceptionSmith("Bad pass.", "unauthorized");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue