Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workflow of JWT authentication

I'm tasked with creating a service-oriented ecosystem for a client. The whole thing is going to be REST based and built in ASP.NET, but my question is technology-agnostic. We want to have a centralized authentication service that issues JWT tokens and claims that are trusted by the other services in the environment.

My issue is this - what's the first thing that a web client (browser) requests? All of the diagrams I've seen (I'll try to add a couple of example links) make it seems as if the client needs to be self-aware and realize that they're going to need a token before they make the first request to the functional REST service, which seems, well, janky to me.

The way I want it to work is that they just attempt to access the secured resource, but there's no auth token with the request my REST service challenge them for user/password, but then delegate the authentication to my auth service. So:

  1. Browser requests restricted resource on REST service
  2. REST service returns 401
  3. Browser gathers credentials, sends to same web service
  4. REST service connects to the authentication service, passing along the Auth header from the client's request
  5. Auth service creates the JWT token and returns it to the REST service
  6. REST service validates the JWT and replaces the Auth header with the JWT token
  7. JWT token is persisted for subsequent requests, up to expy setting

...am I completely off about this? Does the web client need to know that there's a separate auth service involved and make one request there to get their JWT, and then a second request for the REST resource passing the JWT? That seems clunky to me, I hope that's not the idea.

Also, another n00b question - is the JWT token automagically kept by the web clients and re-sent with every request so I don't have to go through the auth service step each time? Is that what the expiration setting is for?

TIA.

See figure 1 here for an example of what I mean: http://msdn.microsoft.com/en-us/library/hh446531.aspx

like image 299
tntwyckoff Avatar asked Dec 06 '25 05:12

tntwyckoff


1 Answers

Starting with your last question will make the rest of the answers clearer:

  • "...is the JWT token automagically kept by the web clients and re-sent with every request.." - The idea is to issue JWT once, send it to the client so client can save it and send it on each subsequent request. This way your front-end app will send username and password just once and then use JWT for authentication. You will have to store the JWT using browser storage (local or session) or cookies (common fallback for older browsers).
  • "...Does the web client need to know that there's a separate auth service involved..." - You will need to send the username and password to a service in order to have the JWT issued. You could implement it with just one request, but you need to send credentials to the service (provided by the user), receive JWT as part of response and store it (as above). It might be easier to do it on a separate request, depending on requirements and implementation.
like image 141
Michal M. Avatar answered Dec 08 '25 20:12

Michal M.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!