Skip to content

Commit

Permalink
Merge pull request #2874 from Hyperkid123/last-visited-debugging
Browse files Browse the repository at this point in the history
Relog user after JWT token expired.
  • Loading branch information
Hyperkid123 authored Jun 28, 2024
2 parents d17fb3d + 28987ee commit d624293
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/ErrorComponents/DefaultErrorComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type DefaultErrorComponentProps = {
errorInfo?: {
componentStack?: string;
};
signIn?: () => Promise<void>;
};

const DefaultErrorComponent = (props: DefaultErrorComponentProps) => {
Expand Down Expand Up @@ -66,7 +67,7 @@ const DefaultErrorComponent = (props: DefaultErrorComponentProps) => {
}, [props.error, activeModule]);

// second level of error capture if xhr/fetch interceptor fails
const gatewayError = get3scaleError(props.error as any);
const gatewayError = get3scaleError(props.error as any, props.signIn);
if (gatewayError) {
return <GatewayErrorComponent error={gatewayError} />;
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/ErrorComponents/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ErrorBoundaryState = {
class ErrorBoundary extends React.Component<
{
children: React.ReactNode;
singIn?: () => Promise<void>;
},
ErrorBoundaryState
> {
Expand All @@ -33,7 +34,7 @@ class ErrorBoundary extends React.Component<

render() {
if (this.state.hasError) {
return <ErrorComponent error={this.state.error} errorInfo={this.state.errorInfo} />;
return <ErrorComponent error={this.state.error} errorInfo={this.state.errorInfo} signIn={this.props.singIn} />;
}

return this.props.children;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/iqeEnablement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function init(chromeStore: ReturnType<typeof createStore>, authRef: React
}
this.onload = function () {
if (this.status >= 400) {
const gatewayError = get3scaleError(this.response);
const gatewayError = get3scaleError(this.response, authRef.current.signinRedirect);
if (this.status === 403 && this.responseText.includes(DENIED_CROSS_CHECK)) {
crossAccountBouncer();
// check for 3scale error
Expand Down Expand Up @@ -178,7 +178,7 @@ export function init(chromeStore: ReturnType<typeof createStore>, authRef: React
try {
const isJson = resCloned?.headers?.get('content-type')?.includes('application/json');
const data = isJson ? await resCloned.json() : await resCloned.text();
const gatewayError = get3scaleError(data);
const gatewayError = get3scaleError(data, authRef.current.signinRedirect);
if (gatewayError) {
chromeStore.set(gatewayErrorAtom, gatewayError);
}
Expand Down
25 changes: 23 additions & 2 deletions src/utils/responseInterceptors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
export type ThreeScaleError = { complianceError?: boolean; status: number; source?: string; detail: string; meta?: { response_by: string } };
// eslint-disable-next-line no-restricted-imports
import { AuthContextProps } from 'react-oidc-context';

export type ThreeScaleError = {
data?: string;
complianceError?: boolean;
status: number;
source?: string;
detail: string;
meta?: { response_by: string };
};

export const COMPLIACE_ERROR_CODES = ['ERROR_OFAC', 'ERROR_T5', 'ERROR_EXPORT_CONTROL'];
const errorCodeRegexp = new RegExp(`(${COMPLIACE_ERROR_CODES.join('|')})`);

export function get3scaleError(response: string | { errors: ThreeScaleError[] }) {
export function get3scaleError(response: string | { errors: ThreeScaleError[] }, signIn?: AuthContextProps['signinRedirect']) {
if (signIn && typeof response !== 'string' && isTokenExpiredError(response)) {
signIn();
return;
}

// attempt to parse XHR response
let parsedResponse: ThreeScaleError[];
try {
Expand Down Expand Up @@ -40,3 +55,9 @@ export function get3scaleError(response: string | { errors: ThreeScaleError[] })
function isComplianceError(response = '') {
return !!response.match(errorCodeRegexp);
}

const TOKEN_EXPIRED_MATCHER = `Invalid JWT token - 'exp' claim expired at`;

function isTokenExpiredError(error?: { errors?: ThreeScaleError[] }) {
return error?.errors?.find(({ status, detail }) => status === 401 && detail?.includes(TOKEN_EXPIRED_MATCHER));
}

0 comments on commit d624293

Please sign in to comment.