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

Rework auth to use jwts #2087

Merged
merged 15 commits into from
May 8, 2020
Merged

Rework auth to use jwts #2087

merged 15 commits into from
May 8, 2020

Conversation

evansdianga
Copy link
Contributor

@evansdianga evansdianga commented Apr 29, 2020

Description


  • Rework Authentication to be stateless using JWTs

Type of Change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Proposed Solution


Use signed JWTs for authentication.

Server cryptographically signs the JWT and send to client. Client stores the JWT and attaches it to every request. The Angular client uses the HTTPInterceptor to attach the token to the request and also to confirm the status of the response of each request.

Whenever the server responds with an unauthorized 401 status, the client automatically logs out the user and requires a new session to be initiated through a new login.

The JWT signing Options are configurable using environment variables.

We make use of the jsonwebtoken library for the server and jwt-decodelibrary for the client side which enable us decode the encoded JWT to get the response properties.

This PR also adds the rxjs-compat library to enable use rxjs

Warn user when session is about to end due to inactivity. The current default time is to check every 10 minutes and warn the user if the session duration left is less than 15 minutes

The secret key for signing JWTs is generated using the crypto module in NodeJS. Theres a good conversation here dwyl/hapi-auth-jwt2#48 Initially added a string as JWT key for proof of concept. This is now replaced by the string generated by the crypto lib

// return next();
if (req.isAuthenticated()) {
return next();
const token = req.headers.authorization;
Copy link
Contributor Author

@evansdianga evansdianga Apr 29, 2020

Choose a reason for hiding this comment

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

Allow for both authorization header and cookie. Ensure cookie is for the same sub-domain.

@evansdianga evansdianga marked this pull request as ready for review May 4, 2020 13:02
…at session validity is checked on Appcomponent init
const jwt = require('jsonwebtoken');
const expiresIn ='5 minutes';
const issuer = 'Tangerine';
const jwtTokenSecret = require('crypto').randomBytes(256).toString('base64');
Copy link
Contributor

Choose a reason for hiding this comment

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

@evansdianga This looks similar to the approach laid out here. Was there another article that you found that talked about this approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @rjsteinert , most approaches coalesced around using a 256 bit cryptographically secure hashes. There is a link in the PR description which links to a discussion on the hapi.js repo as they used a similar approach. The discussion there also lists quite a number of links and gives further info on the approach.

@rjcorwin rjcorwin merged commit 4bb7e73 into next May 8, 2020
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.

2 participants