From a5a3d55bdc0bd65a765d45f2bf92e3fd47a6079c Mon Sep 17 00:00:00 2001 From: Fernando Cardenas Date: Sat, 19 Jul 2025 19:05:41 -0500 Subject: [PATCH] Update PAT token rotation expirary --- .../access_tokens/components/access_token_table.vue | 2 +- .../vue_shared/access_tokens/stores/access_tokens.js | 6 +++--- app/assets/javascripts/vue_shared/access_tokens/utils.js | 7 +++++++ .../vue_shared/access_tokens/stores/access_tokens_spec.js | 4 ++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/vue_shared/access_tokens/components/access_token_table.vue b/app/assets/javascripts/vue_shared/access_tokens/components/access_token_table.vue index 9489e927a1bddd..939c073ca42e17 100644 --- a/app/assets/javascripts/vue_shared/access_tokens/components/access_token_table.vue +++ b/app/assets/javascripts/vue_shared/access_tokens/components/access_token_table.vue @@ -80,7 +80,7 @@ export default { if (this.action === REVOKE) { this.revokeToken(this.selectedToken.id); } else if (this.action === ROTATE) { - this.rotateToken(this.selectedToken.id, this.selectedToken.expiresAt); + this.rotateToken(this.selectedToken.id); } }, isExpiring(expiresAt) { diff --git a/app/assets/javascripts/vue_shared/access_tokens/stores/access_tokens.js b/app/assets/javascripts/vue_shared/access_tokens/stores/access_tokens.js index e6e3d2ff49b33a..92489025fdf32e 100644 --- a/app/assets/javascripts/vue_shared/access_tokens/stores/access_tokens.js +++ b/app/assets/javascripts/vue_shared/access_tokens/stores/access_tokens.js @@ -10,7 +10,7 @@ import { import { joinPaths } from '~/lib/utils/url_utility'; import { s__ } from '~/locale'; import { SORT_OPTIONS, DEFAULT_SORT } from '~/access_tokens/constants'; -import { serializeParams, update15DaysFromNow } from '../utils'; +import { serializeParams, update15DaysFromNow, oneYearFromNow } from '../utils'; /** * @typedef {{type: string, value: {data: string, operator: string}}} Filter @@ -183,7 +183,7 @@ export const useAccessTokens = defineStore('accessTokens', { * @param {number} tokenId * @param {string} expiresAt */ - async rotateToken(tokenId, expiresAt) { + async rotateToken(tokenId) { this.alert?.dismiss(); this.alert = null; this.busy = true; @@ -191,7 +191,7 @@ export const useAccessTokens = defineStore('accessTokens', { try { const url = this.urlRotate.replace(':id', this.id); const { data } = await axios.post(joinPaths(url, `${tokenId}`, 'rotate'), { - expires_at: expiresAt, + expires_at: oneYearFromNow(), }); this.token = data.token; // Reset pagination because after rotation the token may appear on a different page. diff --git a/app/assets/javascripts/vue_shared/access_tokens/utils.js b/app/assets/javascripts/vue_shared/access_tokens/utils.js index 5e58a2857a2f9a..722bc7230bae74 100644 --- a/app/assets/javascripts/vue_shared/access_tokens/utils.js +++ b/app/assets/javascripts/vue_shared/access_tokens/utils.js @@ -47,6 +47,13 @@ export function fifteenDaysFromNow() { return toISODateFormat(nDaysAfter(new Date(), 15)); } +/** + * Returns a date that is 365 days in the future based on current time in ISO format ('YYYY-MM-DD') + */ +export function oneYearFromNow() { + return toISODateFormat(nDaysAfter(new Date(), 365)); +} + /** * Replace the 'DATE_HOLDER' string with a date 15 days in the future based on current time. */ diff --git a/spec/frontend/vue_shared/access_tokens/stores/access_tokens_spec.js b/spec/frontend/vue_shared/access_tokens/stores/access_tokens_spec.js index e08b25cc9457da..5252943dbe4a8b 100644 --- a/spec/frontend/vue_shared/access_tokens/stores/access_tokens_spec.js +++ b/spec/frontend/vue_shared/access_tokens/stores/access_tokens_spec.js @@ -445,12 +445,12 @@ describe('useAccessTokens store', () => { }); it('rotates the token', async () => { - await store.rotateToken(1, '2025-01-01'); + await store.rotateToken(1); expect(mockAxios.history.post).toHaveLength(1); expect(mockAxios.history.post[0]).toEqual( expect.objectContaining({ - data: '{"expires_at":"2025-01-01"}', + data: '{"expires_at":"2021-07-06"}', url: 'http://localhost/api/v4/groups/3/service_accounts/235/personal_access_tokens/1/rotate', }), ); -- GitLab