fix regeneration

This commit is contained in:
Ben Irvin 2022-08-18 14:03:59 +02:00
parent 7098ae95be
commit 9941198dac
5 changed files with 24 additions and 7 deletions

View File

@ -39,16 +39,17 @@ module.exports = {
},
async regenerate(ctx) {
const { body } = ctx.request;
const { id } = ctx.params;
const apiTokenService = getService('api-token');
const alreadyExists = await apiTokenService.exists({ name: body.id });
if (!alreadyExists) {
const apiTokenExists = await apiTokenService.getById(id);
if (!apiTokenExists) {
ctx.notFound('API Token not found');
return;
}
const accessToken = await apiTokenService.regenerate(body.id);
const accessToken = await apiTokenService.regenerate(id);
ctx.created({ data: accessToken });
},

View File

@ -57,7 +57,7 @@ module.exports = [
},
},
{
method: 'PUT',
method: 'POST',
path: '/api-tokens/:id/regenerate',
handler: 'api-token.regenerate',
config: {

View File

@ -258,7 +258,8 @@ describe('API Token', () => {
const id = 1;
const res = await apiTokenService.regenerate(id);
expect(update).toHaveBeenCalledWith(id, {
expect(update).toHaveBeenCalledWith({
where: { id },
select: ['id', 'accessKey'],
data: {
accessKey: apiTokenService.hash(mockedApiToken.hexedString),

View File

@ -138,8 +138,9 @@ const create = async (attributes) => {
const regenerate = async (id) => {
const accessKey = crypto.randomBytes(128).toString('hex');
const apiToken = await strapi.query('admin::api-token').update(id, {
const apiToken = await strapi.query('admin::api-token').update({
select: ['id', 'accessKey'],
where: { id },
data: {
accessKey: hash(accessKey),
},

View File

@ -608,4 +608,18 @@ describe('Admin API Token v2 CRUD (e2e)', () => {
updatedAt: expect.any(String),
});
});
test('Regenerates an api token access key)', async () => {
const token = await createValidToken();
const res = await rq({
url: `/admin/api-tokens/${token.id}/regenerate`,
method: 'POST',
});
expect(res.statusCode).toBe(200);
expect(res.body.data).toMatchObject({
accessKey: expect.any(String),
});
});
});