river-of-ebooks/api/models/PublishKey.js

40 lines
833 B
JavaScript
Raw Normal View History

2019-03-05 23:55:31 +00:00
const { generateToken } = require('../util')
module.exports = {
attributes: {
id: {
type: 'number',
unique: true,
autoIncrement: true
},
user: {
model: 'User',
required: true
},
2019-02-26 03:29:18 +00:00
name: {
type: 'string',
required: true
},
2019-02-26 03:29:18 +00:00
url: 'string',
whitelisted: 'boolean',
2019-02-26 03:29:18 +00:00
verified: 'boolean',
verification_key: 'string',
appid: {
type: 'string'
},
secret: {
type: 'string'
}
},
beforeCreate: async function (key, next) {
key.appid = await generateToken({ bytes: 12 })
key.secret = await generateToken({ bytes: 48 })
2019-02-26 22:17:30 +00:00
key.verification_key = await generateToken({ bytes: 24, base: 'hex' })
next()
},
beforeUpdate: async function (key, next) {
key.secret = await generateToken({ bytes: 48 })
next()
}
}