50 lines
1.0 KiB
JavaScript
Raw Normal View History

'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
* @property {'read-only'|'full-access'} id
*/
/**
* @param {Object} attributes
* @param {string} attributes.name
* @param {string} [attributes.description]
*
2021-08-27 08:47:27 +02:00
* @returns Promise<boolean>
*/
2021-08-27 08:47:27 +02:00
const exists = async (attributes = {}) => {
return (await strapi.query('strapi::api-token').count({ where: attributes })) > 0;
};
/**
* @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>}
*/
const create = async attributes => {
const accessKey = crypto.randomBytes(128).toString('hex');
return strapi.query('strapi::api-token').create({
2021-08-27 09:44:29 +02:00
select: ['id', 'name', 'description', 'type', 'accessKey'],
data: {
...attributes,
accessKey,
},
});
};
module.exports = {
create,
exists,
};