mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 15:13:21 +00:00
fix regeneration
This commit is contained in:
parent
7098ae95be
commit
9941198dac
@ -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 });
|
||||
},
|
||||
|
||||
|
||||
@ -57,7 +57,7 @@ module.exports = [
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'PUT',
|
||||
method: 'POST',
|
||||
path: '/api-tokens/:id/regenerate',
|
||||
handler: 'api-token.regenerate',
|
||||
config: {
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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),
|
||||
},
|
||||
|
||||
@ -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),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user