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

Jwt authentication #1042

Merged
merged 40 commits into from
Jan 12, 2023
Merged

Jwt authentication #1042

merged 40 commits into from
Jan 12, 2023

Conversation

CDimonaco
Copy link
Member

@CDimonaco CDimonaco commented Dec 7, 2022

Description

This pr change the authentication method of the web project from stateful cookie auth to stateless jwt auth.

Changes the web auth logic and implement on the frontend a jwt auth flow with the all the client side authentication flow handled within the single page application. Route guard on the frontend will be added.

The application will use a access/refresh token flow, with the SPA responsible for all the api interaction, including the login and the token refresh part. Here some diagrams

Login flow

flowchart_trento_login

Refresh token success flow

flow_chart_trento_refresh_ok

Refresh token failure flow

flowchart_trento_refresh_bad

The frontend routes are protected with a react router route guard. The guard will check that the user is logged in, and the token is valid. The route uses the /api/me endpoint.

The backend jwts, both access and refresh token are signed with a HS256 (HMAC with SHA-256) key, on runtime the key should be provided as config variable.

The AUTHENTICATION IS ENABLED IN THE DEV ENVIRONMENT

How was this tested?

Automatic tests.

The e2e tests are refactored to use cypress version 12 with proper session support across tests, we need that in order to ensure that the authentication mechanism is present in e2e tests and well tested.

@CDimonaco CDimonaco added enhancement New feature or request javascript Pull requests that update Javascript code elixir Pull requests that update Elixir code labels Dec 7, 2022
@CDimonaco CDimonaco self-assigned this Dec 7, 2022
@CDimonaco CDimonaco force-pushed the jwt_auth branch 2 times, most recently from e7980d5 to 5ecc70c Compare December 28, 2022 17:17
@CDimonaco CDimonaco force-pushed the jwt_auth branch 3 times, most recently from d42f8a4 to 29dfd90 Compare January 4, 2023 18:15
@CDimonaco CDimonaco marked this pull request as ready for review January 5, 2023 17:32
@CDimonaco
Copy link
Member Author

DO NOT MERGE, BEFORE THE HELM CHART IS UPDATED

nelsonkopliku
nelsonkopliku previously approved these changes Jan 11, 2023
Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

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

Amazing.

Copy link
Member

@fabriziosestito fabriziosestito left a comment

Choose a reason for hiding this comment

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

Great work overall!

I did a first pass of the review looking only at the elixir code, I will take the time to look at the FE part soonish.

lib/trento_web/api_auth_error_handler.ex Outdated Show resolved Hide resolved
lib/trento_web/auth/access_token.ex Outdated Show resolved Hide resolved
lib/trento_web/auth/refresh_token.ex Outdated Show resolved Hide resolved
lib/trento_web/auth/jwt_auth_plug.ex Outdated Show resolved Hide resolved
lib/trento_web/auth/jwt_auth_plug.ex Outdated Show resolved Hide resolved
lib/trento_web/templates/session/new.html.eex Outdated Show resolved Hide resolved
test/trento_web/auth/access_token_test.exs Outdated Show resolved Hide resolved
test/trento_web/auth/jwt_auth_plug_test.exs Show resolved Hide resolved
test/trento_web/auth/refresh_token_test.exs Outdated Show resolved Hide resolved
@nelsonkopliku nelsonkopliku self-requested a review January 11, 2023 11:48
@nelsonkopliku nelsonkopliku dismissed their stale review January 11, 2023 11:49

some doubts raised

Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Hey @CDimonaco ,
It looks great. I couldn't do a really deep review as it is huge.
Besides some cosmetic comments, 2 things:

  • The routes in trento.jsx look duplicated
  • The login page component html template now is duplicated in the frontend and backend, is this needed?

PD: I have tested the happy path of the branch, and it works great. And really appreciated for the massive amount of tests you added, this give a lot of confidence to the change

assets/js/lib/auth/index.js Show resolved Hide resolved
lib/trento_web/templates/session/new.html.eex Outdated Show resolved Hide resolved
assets/js/trento.jsx Show resolved Hide resolved
assets/js/Guard.jsx Outdated Show resolved Hide resolved
assets/js/state/sagas/index.js Outdated Show resolved Hide resolved
assets/js/state/user.js Show resolved Hide resolved
assets/js/state/sagas/user.test.js Outdated Show resolved Hide resolved
Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

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

Still amazing, but with some comments 😄

test/e2e/cypress/support/commands.js Outdated Show resolved Hide resolved
@@ -2,7 +2,7 @@ import { getValue } from '../support/common';

describe('User account page', () => {
before(() => {
cy.navigateToItem('About');
cy.visit('/about');
cy.url().should('include', '/about');
Copy link
Member

Choose a reason for hiding this comment

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

Is this assertion cy.url().should('include', '/about'); needed when we do cy.visit('/about');

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about that, but since we have some route guards that can change the route url, I just added as a defensive movement to debug the e2e tests in case of failing, but we can remove

test/e2e/cypress/e2e/checks_catalog.cy.js Show resolved Hide resolved
test/e2e/cypress/e2e/sap_systems_overview.cy.js Outdated Show resolved Hide resolved
Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

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

Some changes but overall really a good job

assets/js/components/Layout/Layout.jsx Outdated Show resolved Hide resolved
})),
});
});
it('should redirect to the / path, when the user is already logged in', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

newline here?

Copy link
Member Author

Choose a reason for hiding this comment

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

idk formatter goes this way 🤣

Comment on lines +100 to +107
const usernameField = screen.getByTestId('login-username');
const passwordField = screen.getByTestId('login-password');

await user.type(usernameField, 'admin');
await user.type(passwordField, 'admin');

const submitButton = screen.getByTestId('login-submit');
await user.click(submitButton);
Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure you can't get those fields by any other query?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I can get them with Id certainly, do you think it's better?

Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

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

Here we go! Thanks

assets/js/trento.jsx Show resolved Hide resolved
Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

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

@CDimonaco CDimonaco merged commit 46262a0 into main Jan 12, 2023
@CDimonaco CDimonaco deleted the jwt_auth branch January 12, 2023 11:26
@stefanotorresi
Copy link
Member

excellent work, @CDimonaco !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elixir Pull requests that update Elixir code enhancement New feature or request javascript Pull requests that update Javascript code
Development

Successfully merging this pull request may close these issues.

6 participants