2021-08-26 14:37:55 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2021-08-27 09:44:29 +02:00
|
|
|
/**
|
|
|
|
* @typedef ApiToken
|
|
|
|
*
|
|
|
|
* @property {number} id
|
|
|
|
* @property {string} name
|
|
|
|
* @property {string} [description]
|
|
|
|
* @property {string} accessKey
|
2021-08-27 10:30:18 +02:00
|
|
|
* @property {'read-only'|'full-access'} type
|
2021-08-27 09:44:29 +02:00
|
|
|
*/
|
|
|
|
|
2021-08-26 14:37:55 +02:00
|
|
|
/**
|
|
|
|
* @param {Object} attributes
|
|
|
|
* @param {string} attributes.name
|
|
|
|
* @param {string} [attributes.description]
|
|
|
|
*
|
2021-08-27 10:30:18 +02:00
|
|
|
* @returns {Promise<boolean>}
|
2021-08-26 14:37:55 +02:00
|
|
|
*/
|
2021-08-27 08:47:27 +02:00
|
|
|
const exists = async (attributes = {}) => {
|
2021-08-27 10:30:18 +02:00
|
|
|
return (await strapi.query('admin::api-token').count({ where: attributes })) > 0;
|
2021-08-26 14:37:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} attributes
|
|
|
|
* @param {'read-only'|'full-access'} attributes.type
|
|
|
|
* @param {string} attributes.name
|
|
|
|
* @param {string} [attributes.description]
|
|
|
|
*
|
2021-08-27 09:44:29 +02:00
|
|
|
* @returns {Promise<ApiToken>}
|
2021-08-26 14:37:55 +02:00
|
|
|
*/
|
|
|
|
const create = async attributes => {
|
|
|
|
const accessKey = crypto.randomBytes(128).toString('hex');
|
|
|
|
|
2021-08-27 10:30:18 +02:00
|
|
|
return strapi.query('admin::api-token').create({
|
2021-08-27 09:44:29 +02:00
|
|
|
select: ['id', 'name', 'description', 'type', 'accessKey'],
|
2021-08-26 14:37:55 +02:00
|
|
|
data: {
|
|
|
|
...attributes,
|
|
|
|
accessKey,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
create,
|
|
|
|
exists,
|
|
|
|
};
|