|
|
|
@ -1,22 +1,21 @@ |
|
|
|
import { checkStringParam, errorOut, notYet, randomElement, requestIp, reverseString, hours, minutes } from '../logic/utils.js'; |
|
|
|
import { checkStringParam, errorOut, notYet, randomElement, ipAddress, reverseString, hours, minutes } from '../logic/utils.js'; |
|
|
|
import { Animals } from '../misc/animals.js'; |
|
|
|
import { reissueToken, generateToken, newTokenExpiry, hashPassword, doesPasswordMatch, isEndpointAllowedForBannedUsers, isEndpointProtected, generateRestoreCode, restoreValidity, restoreAttempts } from '../logic/security.js'; |
|
|
|
import { sendRestorationLink } from '../logic/email.js'; |
|
|
|
import { UserSession } from '../logic/session.js'; |
|
|
|
import { SpamCache } from '../logic/cache.js'; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {Object} props |
|
|
|
* @param {import('fastify').FastifyInstance} props.app |
|
|
|
* @param {import('sequelize/types').Sequelize} props.db |
|
|
|
*/ |
|
|
|
/** @param {import('./route').props} */ |
|
|
|
|
|
|
|
|
|
|
|
function AuthController({app, db}){ |
|
|
|
const { Users } = db.models; |
|
|
|
|
|
|
|
const signInSpam = new SpamCache(); |
|
|
|
const signUpSpam = new SpamCache(); |
|
|
|
const restoreApplySpam = new SpamCache(); |
|
|
|
const spamCheck = { |
|
|
|
signIn: new SpamCache(), |
|
|
|
signUp: new SpamCache(), |
|
|
|
restoreApply: new SpamCache() |
|
|
|
}; |
|
|
|
|
|
|
|
{ //validate token header and put .session in every request
|
|
|
|
app.decorateRequest('session', null); |
|
|
|
@ -45,7 +44,7 @@ function AuthController({app, db}){ |
|
|
|
app.post('/auth/sign-in', async (request, reply) => { |
|
|
|
const {email, paswd} = request.body || {}; |
|
|
|
|
|
|
|
if(signInSpam.check(requestIp(request), [[5000, 2*hours]])){ |
|
|
|
if(spamCheck.signIn.check(ipAddress(request), [[5000, 2*hours]])){ |
|
|
|
return errorOut(reply, 'error.too_fast'); |
|
|
|
} |
|
|
|
|
|
|
|
@ -70,7 +69,7 @@ function AuthController({app, db}){ |
|
|
|
app.post('/auth/sign-up', async (request, reply) => { |
|
|
|
const {email, username, paswd} = request.body || {}; |
|
|
|
|
|
|
|
if(signUpSpam.check(requestIp(request), [[5000, 2*hours], [100, 10*minutes]])){ |
|
|
|
if(spamCheck.signUp.check(ipAddress(request), [[5000, 2*hours], [100, 10*minutes]])){ |
|
|
|
return errorOut(reply, 'error.too_fast'); |
|
|
|
} |
|
|
|
|
|
|
|
@ -101,7 +100,7 @@ function AuthController({app, db}){ |
|
|
|
tokenExpiry: newTokenExpiry(), |
|
|
|
paswd: hashPassword(reverseString(paswd)), |
|
|
|
role: 'user', |
|
|
|
firstIp: requestIp(request) |
|
|
|
firstIp: ipAddress(request) |
|
|
|
}; |
|
|
|
await Users.create(newUser); |
|
|
|
return {token: newUser.token}; |
|
|
|
@ -130,7 +129,7 @@ function AuthController({app, db}){ |
|
|
|
const {email, code, newpaswd} = request.body || {}; |
|
|
|
const changeRequested = (newpaswd != null); |
|
|
|
|
|
|
|
if(restoreApplySpam.check(requestIp(request), [[100, 12*hours], [25, 30*minutes]])){ |
|
|
|
if(spamCheck.restoreApply.check(ipAddress(request), [[100, 12*hours], [25, 30*minutes]])){ |
|
|
|
return errorOut(reply, 'error.too_fast'); |
|
|
|
} |
|
|
|
|
|
|
|
|