parent
cdd9eed3d8
commit
28172607c6
@ -1,12 +1,21 @@
|
||||
// TODO: Prevent token connections from creating tokens.
|
||||
// TODO: Token permission.
|
||||
export async function create () {
|
||||
const userId = this.session.get('user_id')
|
||||
return (await this.createAuthenticationToken({userId})).id
|
||||
export async function create ({ expiresIn }) {
|
||||
return (await this.createAuthenticationToken({
|
||||
expiresIn,
|
||||
userId: this.session.get('user_id')
|
||||
})).id
|
||||
}
|
||||
|
||||
create.description = 'create a new authentication token'
|
||||
|
||||
create.params = {
|
||||
expiresIn: {
|
||||
optional: true,
|
||||
type: [ 'number', 'string' ]
|
||||
}
|
||||
}
|
||||
|
||||
create.permission = '' // sign in
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
@ -1,3 +1,4 @@
|
||||
import ms from 'ms'
|
||||
import { noSuchObject } from 'xo-common/api-errors'
|
||||
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({
|
||||
id: await generateToken(),
|
||||
user_id: userId,
|
||||
expiration: Date.now() + ONE_MONTH
|
||||
expiration: Date.now() + (
|
||||
typeof expiresIn === 'string'
|
||||
? ms(expiresIn)
|
||||
: expiresIn
|
||||
)
|
||||
})
|
||||
|
||||
await this._tokens.add(token)
|
||||
|
Loading…
Reference in New Issue
Block a user