What is the proper way to verify a token on server?
I am using jwt token mechanism in my app and here's how its working as of now:
- Client logs in with username and password
- Server checks the username and password and creates token with RSA public and private key and sends a new token to client with payload containing users email address and then stores the same token in database.
- Any subsequent requests for resources on server, client sends along jwt token Server then checks the token with token available in database based on email address a user is logged in with.
- Then provides the resource to client.
I think I am missing something here. Reading other blog post, I see that token should be verified with secret key.
Few questions I have:
- I am not sure whether token should be checked against public key or private key
- While sending a jwt token to client do I have to send public key to client in payload as I am using RSA mechanism? So for each request for resource, I have to decode the token and check public key against private key?
- And in which scenarios, I need to store token in database? or it is not required to store token in database at all?
- What happens when a malicious gets hold of the token on client-side and use it for login?
Note: I am using vanilla java and jax-rs(rest) for back-end and angularjs for front-end. Thanks