feat(token.create): expiresIn param (#611)

Fixes vatesfr/xo-web#1769
This commit is contained in:
Julien Fontanet 2017-10-17 15:22:34 +02:00 committed by GitHub
parent cdd9eed3d8
commit 28172607c6
2 changed files with 22 additions and 5 deletions

View File

@ -1,12 +1,21 @@
// TODO: Prevent token connections from creating tokens. // TODO: Prevent token connections from creating tokens.
// TODO: Token permission. // TODO: Token permission.
export async function create () { export async function create ({ expiresIn }) {
const userId = this.session.get('user_id') return (await this.createAuthenticationToken({
return (await this.createAuthenticationToken({userId})).id expiresIn,
userId: this.session.get('user_id')
})).id
} }
create.description = 'create a new authentication token' create.description = 'create a new authentication token'
create.params = {
expiresIn: {
optional: true,
type: [ 'number', 'string' ]
}
}
create.permission = '' // sign in create.permission = '' // sign in
// ------------------------------------------------------------------- // -------------------------------------------------------------------

View File

@ -1,3 +1,4 @@
import ms from 'ms'
import { noSuchObject } from 'xo-common/api-errors' import { noSuchObject } from 'xo-common/api-errors'
import { ignoreErrors } from 'promise-toolbox' import { ignoreErrors } from 'promise-toolbox'
@ -151,11 +152,18 @@ export default class {
// ----------------------------------------------------------------- // -----------------------------------------------------------------
async createAuthenticationToken ({userId}) { async createAuthenticationToken ({
expiresIn = ONE_MONTH,
userId
}) {
const token = new Token({ const token = new Token({
id: await generateToken(), id: await generateToken(),
user_id: userId, user_id: userId,
expiration: Date.now() + ONE_MONTH expiration: Date.now() + (
typeof expiresIn === 'string'
? ms(expiresIn)
: expiresIn
)
}) })
await this._tokens.add(token) await this._tokens.add(token)