2019-02-03 19:16:02 +00:00
|
|
|
const crypto = require('crypto')
|
|
|
|
|
2019-02-24 21:27:24 +00:00
|
|
|
function generateToken ({ bytes, base }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-02-03 19:16:02 +00:00
|
|
|
crypto.randomBytes(bytes, (err, buf) => {
|
2019-02-24 21:27:24 +00:00
|
|
|
if (err) reject(err)
|
|
|
|
else resolve(buf.toString(base || 'base64'))
|
2019-02-03 19:16:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
attributes: {
|
|
|
|
id: {
|
|
|
|
type: 'number',
|
|
|
|
unique: true,
|
|
|
|
autoIncrement: true
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
model: 'User',
|
|
|
|
required: true
|
|
|
|
},
|
2019-02-26 03:29:18 +00:00
|
|
|
name: {
|
2019-02-03 19:16:02 +00:00
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
2019-02-26 03:29:18 +00:00
|
|
|
url: 'string',
|
2019-02-24 21:27:24 +00:00
|
|
|
whitelisted: 'boolean',
|
2019-02-26 03:29:18 +00:00
|
|
|
verified: 'boolean',
|
|
|
|
verification_key: 'string',
|
2019-02-24 21:27:24 +00:00
|
|
|
appid: {
|
|
|
|
type: 'string'
|
2019-02-03 19:16:02 +00:00
|
|
|
},
|
|
|
|
secret: {
|
2019-02-24 21:27:24 +00:00
|
|
|
type: 'string'
|
2019-02-03 19:16:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeCreate: async function (key, next) {
|
2019-02-24 21:27:24 +00:00
|
|
|
key.appid = await generateToken({ bytes: 12 })
|
2019-02-03 19:16:02 +00:00
|
|
|
key.secret = await generateToken({ bytes: 48 })
|
2019-02-26 22:17:30 +00:00
|
|
|
key.verification_key = await generateToken({ bytes: 24, base: 'hex' })
|
2019-02-03 19:16:02 +00:00
|
|
|
next()
|
|
|
|
},
|
|
|
|
beforeUpdate: async function (key, next) {
|
|
|
|
key.secret = await generateToken({ bytes: 48 })
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|