0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/packages/ui-components/jest/unit/components/__mocks__/token.ts
Ku3mi41 92f1c34ae8
fix: ui expired token (#4007)
* fix: login state when token expired
If there token in the localstorage, the user was always displayed as authenticated, regardless of the token expiration

* chore: added changeset for @verdaccio/ui-components

* tests: JSON error for node versions older than 20
2023-09-24 10:17:53 +02:00

33 lines
916 B
TypeScript

/**
* Token Utility
*/
import dayjs from 'dayjs';
function encodeBase64(string: string) {
return Buffer.from(string).toString('base64');
}
export function generateTokenWithTimeRange(amount = 0) {
const payload = {
username: 'verdaccio',
exp: Number.parseInt(String(dayjs(new Date()).add(amount, 'hour').valueOf() / 1000), 10),
};
return `xxxxxx.${encodeBase64(JSON.stringify(payload))}.xxxxxx`;
}
export function generateTokenWithExpirationAsString() {
const payload = { username: 'verdaccio', exp: 'I am not a number' };
return `xxxxxx.${encodeBase64(JSON.stringify(payload))}.xxxxxx`;
}
export function generateInvalidToken() {
const payload = `invalidtoken`;
return `xxxxxx.${encodeBase64(payload)}.xxxxxx`;
}
export function generateTokenWithOutExpiration() {
const payload = {
username: 'verdaccio',
};
return `xxxxxx.${encodeBase64(JSON.stringify(payload))}.xxxxxx`;
}