feat(xo-server/createAuthenticationToken): throw if expiresIn is too high

This commit is contained in:
Julien Fontanet
2022-05-23 18:24:51 +02:00
parent b02bf90c8a
commit 5b7228ed69

View File

@@ -164,15 +164,18 @@ export default class {
// -----------------------------------------------------------------
async createAuthenticationToken({ expiresIn, userId }) {
let duration = this._defaultTokenValidity
if (expiresIn !== undefined) {
duration = parseDuration(expiresIn)
if (duration > this._maxTokenValidity) {
throw new Error('too high expiresIn duration: ' + expiresIn)
}
}
const token = new Token({
id: await generateToken(),
user_id: userId,
expiration:
Date.now() +
Math.min(
expiresIn !== undefined ? parseDuration(expiresIn) : this._defaultTokenValidity,
this._maxTokenValidity
),
expiration: Date.now() + duration,
})
await this._tokens.add(token)