Wizard: fix token not having expiration fails

This commit is contained in:
Anna Vítová 2025-05-15 16:16:06 +02:00 committed by Klara Simickova
parent 854ff93594
commit 2692b75432
3 changed files with 25 additions and 13 deletions

View file

@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { CheckCircleIcon } from '@patternfly/react-icons';
import { jwtDecode } from 'jwt-decode';
import { jwtDecode, JwtPayload } from 'jwt-decode';
import { UNIQUE_VALIDATION_DELAY } from '../../../constants';
import { useLazyGetBlueprintsQuery } from '../../../store/backendApi';
@ -116,10 +116,10 @@ function tokenValidityRemaining(expireTimeInSeconds: number): number {
return expireTimeInSeconds - currentTimeSeconds;
}
function getTokenExpirationTime(token: string): number | undefined {
function decodeToken(token: string): JwtPayload | undefined {
try {
const decoded = jwtDecode(token) as { exp?: number };
return decoded.exp;
return decoded;
} catch {
return undefined;
}
@ -152,19 +152,22 @@ export function validateSatelliteToken(
errors.command = 'Invalid or missing token';
return errors;
}
const expiresInSeconds = getTokenExpirationTime(match[1]);
if (expiresInSeconds === undefined) {
const satelliteToken = decodeToken(match[1]);
if (satelliteToken === undefined) {
errors.command = 'Invalid or missing token';
return errors;
}
const tokenRemainingS = tokenValidityRemaining(expiresInSeconds);
if (tokenRemainingS <= 0) {
errors.command = `The token is expired. Check out the Satellite documentation to extend the token lifetime.`;
return errors;
}
const expirationString = getExpirationString(tokenRemainingS);
if (expirationString !== undefined) {
errors.expired = `The token expires in ${expirationString}. Check out the Satellite documentation to extend the token lifetime.`;
if (satelliteToken.exp) {
const tokenRemainingS = tokenValidityRemaining(satelliteToken.exp);
if (tokenRemainingS <= 0) {
errors.command = `The token is expired. Check out the Satellite documentation to extend the token lifetime.`;
return errors;
}
const expirationString = getExpirationString(tokenRemainingS);
if (expirationString !== undefined) {
errors.expired = `The token expires in ${expirationString}. Check out the Satellite documentation to extend the token lifetime.`;
}
}
return errors;

View file

@ -18,6 +18,7 @@ import {
CERTIFICATE,
SATELLITE_COMMAND,
SATELLITE_COMMAND_EXPIRED_TOKEN,
SATELLITE_COMMAND_NO_EXPIRATION,
} from '../../../../fixtures/customizations';
import { registrationCreateBlueprintRequest } from '../../../../fixtures/editMode';
import { server } from '../../../../mocks/server';
@ -443,6 +444,10 @@ describe('Registration request generated correctly', () => {
await addSatelliteRegistrationCommandViaKeyDown(SATELLITE_COMMAND);
expect(nextButton).toBeEnabled();
await addSatelliteRegistrationCommandViaKeyDown(
SATELLITE_COMMAND_NO_EXPIRATION
);
expect(nextButton).toBeEnabled();
});
});

View file

@ -73,6 +73,10 @@ set -o pipefail && curl -sS 'https://ec2-107-23-89.compute-1.amazonaws.com/regis
// -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1LCJpYXQiOjE3MjM4Mzc3MjIsImp0aSI6IjBhNTU3MDM3ZDIyNzUyMDYwM2M3MWIzZDI4NGYwZjQ1NmFjYjE5NzEyNmFmNTk5NzU0NWJmODcwZDczM2RhY2YiLCJleHAiOjE3MjM4NTIxMjIsInNjb3BlIjoicmVnaXN0cmF0aW9uI2dsb2JhbCByZWdpc3RyYXRpb24jaG9zdCJ9.HsSnZEqq--MIJfP3_awn6SflEruoEm77iSWh0Pi6EW4'
// | bash`; // notsecret
export const SATELLITE_COMMAND_NO_EXPIRATION = `
set -o pipefail && curl -sS 'https://ec2-107-23-89.compute-1.amazonaws.com/register?activation_keys=ak&location_id=2&organization_id=1&update_packages=false'
// -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' | bash`; // notsecret
export const CERTIFICATE = `-----BEGIN CERTIFICATE-----
MIIDUDCCAjigAwIBAgIUIE7ftn9J9krO6TRJg8M3plAZGgEwDQYJKoZIhvcNAQEL
BQAwYjELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcM