mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.6 KiB
76 lines
1.6 KiB
5 years ago
|
/* global WIKI */
|
||
|
|
||
2 years ago
|
import { Model } from 'objection'
|
||
|
import { DateTime } from 'luxon'
|
||
|
import ms from 'ms'
|
||
|
import jwt from 'jsonwebtoken'
|
||
5 years ago
|
|
||
|
/**
|
||
|
* Users model
|
||
|
*/
|
||
2 years ago
|
export class ApiKey extends Model {
|
||
5 years ago
|
static get tableName() { return 'apiKeys' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
|
required: ['name', 'key'],
|
||
|
|
||
|
properties: {
|
||
2 years ago
|
id: {type: 'string'},
|
||
5 years ago
|
name: {type: 'string'},
|
||
|
key: {type: 'string'},
|
||
|
expiration: {type: 'string'},
|
||
|
isRevoked: {type: 'boolean'},
|
||
|
createdAt: {type: 'string'},
|
||
|
validUntil: {type: 'string'}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async $beforeUpdate(opt, context) {
|
||
|
await super.$beforeUpdate(opt, context)
|
||
|
|
||
2 years ago
|
this.updatedAt = new Date().toISOString()
|
||
5 years ago
|
}
|
||
|
async $beforeInsert(context) {
|
||
|
await super.$beforeInsert(context)
|
||
|
|
||
2 years ago
|
this.createdAt = new Date().toISOString()
|
||
|
this.updatedAt = new Date().toISOString()
|
||
5 years ago
|
}
|
||
|
|
||
2 years ago
|
static async createNewKey ({ name, expiration, groups }) {
|
||
|
console.info(DateTime.utc().plus(ms(expiration)).toISO())
|
||
|
|
||
2 years ago
|
const entry = await WIKI.db.apiKeys.query().insert({
|
||
5 years ago
|
name,
|
||
|
key: 'pending',
|
||
2 years ago
|
expiration: DateTime.utc().plus(ms(expiration)).toISO(),
|
||
5 years ago
|
isRevoked: true
|
||
|
})
|
||
|
|
||
2 years ago
|
console.info(entry)
|
||
|
|
||
5 years ago
|
const key = jwt.sign({
|
||
|
api: entry.id,
|
||
2 years ago
|
grp: groups
|
||
5 years ago
|
}, {
|
||
2 years ago
|
key: WIKI.config.auth.certs.private,
|
||
|
passphrase: WIKI.config.auth.secret
|
||
5 years ago
|
}, {
|
||
|
algorithm: 'RS256',
|
||
|
expiresIn: expiration,
|
||
|
audience: WIKI.config.auth.audience,
|
||
|
issuer: 'urn:wiki.js'
|
||
|
})
|
||
|
|
||
2 years ago
|
await WIKI.db.apiKeys.query().findById(entry.id).patch({
|
||
5 years ago
|
key,
|
||
|
isRevoked: false
|
||
|
})
|
||
|
|
||
|
return key
|
||
|
}
|
||
|
}
|