Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID-1113, ID-1115 Separate Logout Code and Redirect on Logout #4726

Merged
merged 13 commits into from
Mar 25, 2024

Conversation

tlangs
Copy link
Contributor

@tlangs tlangs commented Mar 18, 2024

Jira Ticket: https://broadworkbench.atlassian.net/browse/ID-1114

Summary of changes:

Pulling logout code out of auth.ts and into its own file. This allows for much easier testing, as we don't have to manage all the subscriptions defined in auth.ts.

Splitting signOut into two functions is the start of a change where we hit B2C during sign out. Before we hit B2C, we might have a valid token. Afterwards, the token will be invalid.

signOut now also redirects to the B2C end_session_endpoint (well, actually Orch's new logout endpoint), which clears server-side HTTP Only cookies. The user is then redirected back to Terra UI at /#logout, where post-logout code is executed.

I was able to completely unit test this functionality!

What

  • Split out signOut code and unit test it. Get code ready for calling out to B2C on logout.

Why

  • We need to tell B2C when a session ends so that it can clear HTTP-only cookies that have caused users to be unable to log in.

Testing strategy

  • Unit Tests!
  • Testing locally
  • Testing in a BEE

@tlangs tlangs marked this pull request as ready for review March 20, 2024 16:42
@tlangs tlangs requested review from a team as code owners March 20, 2024 16:42
@tlangs tlangs changed the title ID-1113 Separate Logout Code and Create Logout Handler ID-1113, ID-1115 Separate Logout Code and Redirect on Logout Mar 20, 2024
src/auth/auth-events/signout.test.ts Outdated Show resolved Hide resolved
src/auth/auth-events/signout.test.ts Outdated Show resolved Hide resolved
Comment on lines 145 to 146
const consoleErrorFn = jest.fn();
jest.spyOn(console, 'error').mockImplementation(consoleErrorFn);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.spyOn creates and returns a mock, so there's no need to mock it's implementation with a second mock.

Instead, it could be

jest.spyOn(console, 'error').mockImplementation(() => {});
...
expect(console.error).toHaveBeenCalledWith(...);

or

const consoleErrorFn = jest.spyOn(console, 'error').mockImplementation(() => {});
...
expect(consoleErrorFn).toHaveBeenCalledWith(...);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll admit it. Copilot wrote this one 😅


export const signOut = (cause: SignOutCause = 'unspecified'): void => {
// TODO: invalidate runtime cookies https://broadworkbench.atlassian.net/browse/IA-3498
sendSignOutMetrics(cause);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we're here, since this has caused problems before, would it be worth adding a comment that sendSignOutMetrics should not be awaited, since it will fail if the sign out is due to session expiration?

import React from 'react';
import { signOut } from 'src/auth/auth-events/signout';

export const Disabled: React.FC = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: convention

Suggested change
export const Disabled: React.FC = () => {
export const Disabled = (): ReactNode => {

Comment on lines 6 to 13
export const Logout = () => {
try {
userSignedOut();
} catch (e) {
console.error(e);
}
Nav.goToPath('root');
return <div />;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side effects should go in effect hooks. Also, since we're only interested in the side effect, this doesn't need to render an element.

Suggested change
export const Logout = () => {
try {
userSignedOut();
} catch (e) {
console.error(e);
}
Nav.goToPath('root');
return <div />;
export const Logout = (): ReactNode => {
useEffect(() => {
try {
userSignedOut();
} catch (e) {
console.error(e);
}
Nav.goToPath('root');
}, []);
return null;

@tlangs
Copy link
Contributor Author

tlangs commented Mar 20, 2024

Now that Orch is deployed to dev, I pointed my local branch to Dev. It works!

@tlangs
Copy link
Contributor Author

tlangs commented Mar 20, 2024

I'll wait for Orch to go to prod tomorrow before merging this.

authStore.update((state) => ({
...state,
signInStatus: 'signedOut',
// TODO: If allowed, this should be moved to the cookie store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a ticket for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samanehsan
Copy link
Contributor

I thought we needed to pass in the policy as a query param, like the token endpoint does:

extraTokenParams: { p: getConfig().b2cBillingPolicy },
. Is that not required for signing out? 🤔

@tlangs
Copy link
Contributor Author

tlangs commented Mar 22, 2024

@samanehsan That policy is specific for billing. We need some additional scopes to get GCP Billing info, so we make them sign in again with a different policy. Its unused to normal sign-in/sign-out.

Orchestration inserts the policy when it returns a redirect to Terra-UI, so for sign-in/sign-out, the UI doesn't need to do anything.

Copy link

sonarcloud bot commented Mar 25, 2024

@tlangs tlangs added this pull request to the merge queue Mar 25, 2024
Merged via the queue into dev with commit 005c460 Mar 25, 2024
9 checks passed
@tlangs tlangs deleted the tl_ID-1113_split_out_logout_code branch March 25, 2024 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants