123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /* global WIKI */
- import { Model } from 'objection'
- import { DateTime } from 'luxon'
- import ms from 'ms'
- import jwt from 'jsonwebtoken'
- /**
- * Users model
- */
- export class ApiKey extends Model {
- static get tableName() { return 'apiKeys' }
- static get jsonSchema () {
- return {
- type: 'object',
- required: ['name', 'key'],
- properties: {
- id: {type: 'string'},
- 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)
- this.updatedAt = new Date().toISOString()
- }
- async $beforeInsert(context) {
- await super.$beforeInsert(context)
- this.createdAt = new Date().toISOString()
- this.updatedAt = new Date().toISOString()
- }
- static async createNewKey ({ name, expiration, groups }) {
- console.info(DateTime.utc().plus(ms(expiration)).toISO())
- const entry = await WIKI.db.apiKeys.query().insert({
- name,
- key: 'pending',
- expiration: DateTime.utc().plus(ms(expiration)).toISO(),
- isRevoked: true
- })
- console.info(entry)
- const key = jwt.sign({
- api: entry.id,
- grp: groups
- }, {
- key: WIKI.config.auth.certs.private,
- passphrase: WIKI.config.auth.secret
- }, {
- algorithm: 'RS256',
- expiresIn: expiration,
- audience: WIKI.config.auth.audience,
- issuer: 'urn:wiki.js'
- })
- await WIKI.db.apiKeys.query().findById(entry.id).patch({
- key,
- isRevoked: false
- })
- return key
- }
- }
|