Skip to content

Commit

Permalink
Custom message for unanticipated 401 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thomheymann committed Sep 29, 2021
1 parent fd9dd2c commit 19be40b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ const messageMap = new Map([
}),
},
],
[
'AUTHENTICATION_ERROR',
{
type: LoginFormMessageType.Info,
content: i18n.translate('xpack.security.login.authenticationErrorDescription', {
defaultMessage: 'An unexpected authentication error occured. Please log in again.',
}),
},
],
[
'LOGGED_OUT',
{
Expand Down Expand Up @@ -77,7 +86,7 @@ export class LoginPage extends Component<Props, State> {
try {
this.setState({ loginState: await this.props.http.get('/internal/security/login_state') });
} catch (err) {
this.props.fatalErrors.add(err);
this.props.fatalErrors.add(err as Error);
}

loadingCount$.next(0);
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/security/public/session/session_expired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ describe('#logout', () => {
);
});

it(`redirects user to the logout URL with custom reason 'msg'`, async () => {
const sessionExpired = new SessionExpired(LOGOUT_URL, TENANT);
sessionExpired.logout('CUSTOM_REASON');

const next = `&next=${encodeURIComponent(CURRENT_URL)}`;
await expect(window.location.assign).toHaveBeenCalledWith(
`${LOGOUT_URL}?msg=CUSTOM_REASON${next}`
);
});

it(`adds 'provider' parameter when sessionStorage contains the provider name for this tenant`, async () => {
const providerName = 'basic';
mockGetItem.mockReturnValueOnce(providerName);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/security/public/session/session_expired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ const getProviderParameter = (tenant: string) => {
export class SessionExpired {
constructor(private logoutUrl: string, private tenant: string) {}

logout() {
logout(reason = 'SESSION_EXPIRED') {
const next = getNextParameter();
const provider = getProviderParameter(this.tenant);
window.location.assign(
`${this.logoutUrl}?${LOGOUT_REASON_QUERY_STRING_PARAMETER}=SESSION_EXPIRED${next}${provider}`
`${this.logoutUrl}?${LOGOUT_REASON_QUERY_STRING_PARAMETER}=${reason}${next}${provider}`
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ it(`logs out 401 responses`, async () => {
await drainPromiseQueue();
expect(fetchResolved).toBe(false);
expect(fetchRejected).toBe(false);
expect(sessionExpired.logout).toHaveBeenCalledWith('AUTHENTICATION_ERROR');
});

it(`ignores anonymous paths`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class UnauthorizedResponseHttpInterceptor implements HttpInterceptor {
}

if (response.status === 401) {
this.sessionExpired.logout();
this.sessionExpired.logout('AUTHENTICATION_ERROR');
controller.halt();
}
}
Expand Down

0 comments on commit 19be40b

Please sign in to comment.