JWS (a signed version of JWT) is a great example, as was considered for similar scnearios:
- you have an authentication application: every login goes through this (
signin.domain.com ), and as soon as you check the credentials of the user you issue the token generated using the private keys strong> - each service (
service2.domain.com , service2.domain.com ) can implement middleware, which instead of authorization : all your services will receive a public key and will be able to verify the authenticity of the token through this key. They do not need a database, because what they need to check is that the token is valid, and not the one that exists with the user, etc. Etc.
To clarify my last statement: you should probably issue very short tokens. At this point, say that:
- user X logs in
- its token will be valid for ten minutes.
- user X deletes his account but still has a valid token
- He then turns to
service.domain.com
In service.domain.com you still consider it registered until, for example, you need to interact with an API that actually gets into the database (i.e. adds a new user address). At this point, the service responsible for writing to the database throws an exception saying that the user does not exist, and you can probably trap him and take the user out of the system. All of this can be tweaked or tweaked, but you'll get a rough idea of ββhow it can work.
Returning to JWT and its use, I do not know if you are familiar with PHP, but this is a pretty simple example .
If you want a fantasy, you can use nginx as middleware and have something like an auth module for authorization for you.
And last, but not least, here we examined only authentication: for authorization, you probably want either in each service or read the userβs roles from the token (if you saved them there as soon as the user logs in) - but this is a little incorrect as if the user had lost the role, then his token listed it anyway) or simply call signin.domain.com/users/me from each service to get an updated list of user roles, and then check if he is allowed to perform certain operations over it th specific service.
Oh, and remember that you should never put sensitive data in JWT / JWS, as it can be decoded. So yes, you can add user roles in JWT, but for example, never save passwords or other plaintext tokens.
Hope this helps!
odino source share