2021-08-26 14:37:55 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} attributes
|
|
|
|
* @param {string} attributes.name
|
|
|
|
* @param {string} [attributes.description]
|
|
|
|
*
|
2021-08-27 08:47:27 +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 = {}) => {
|
|
|
|
return (await strapi.query('strapi::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]
|
|
|
|
*
|
|
|
|
* @returns {Promise<Record<'id'|'name'|'description'|'type'|'accessKey', string>>}
|
|
|
|
*/
|
|
|
|
const create = async attributes => {
|
|
|
|
const accessKey = crypto.randomBytes(128).toString('hex');
|
|
|
|
|
|
|
|
return strapi.query('strapi::api-token').create({
|
|
|
|
data: {
|
|
|
|
...attributes,
|
|
|
|
accessKey,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
create,
|
|
|
|
exists,
|
|
|
|
};
|