Some statements regarding JWT in the context of web client-server authentication:
- JWT are not safe against man in the middle attacks. Sending JWT from client to server security wise equals to sending a hashed password.
- JWT can carry user details as payload. Using this data without accessing actual data in the DB is cited as one JWT feature. However this JWT data will not invalidate / update if the DB data changes.
- Following from 2. the JWT payload in some situations should be verified against the DB and / or a timestamp should be set wisely to invalidate the JWT after some time.
A real world example where a client has to make several calls to APIs to complete just one workflow: a user wants to know the price of the shortest route from A to B. We are using two types of JWTs an "authJWT" & a "normal JWT".
- IF client has an authJWT: client requests API0 (auth API) with authJWT. API0 checks authJWT signature & user data payload against DB & timestamp < 2 days. Returns new "normal" JWT.
ELSE: client requests API0 (auth API) with password & login for JWTs with timestamp. API0 checks password & login against DB and returns authJWT & "normal" JWT.
In both cases: All subsequent APIs will be called with "normal" JWT and verify validity only via signature and timestamp but not against the user DB. - Client requests API1 twice to get best match for search string for place A and place B. Server checks JWT signature & timestamp < 10s and uses JWT user data when needed.
- Client requests API2 to get shortest route from place A to place B. Server checks JWT signature & timestamp < 10s and uses JWT user data when needed.
- Client requests API3 to get price for shortes route. Server checks JWT signature & timestamp < 10s and uses JWT user data when needed.
This means that a man in the middle has to catch the call to API0 to get real access. Catching a "normal" JWT has little effect as it expires after 10s. Probably calls to APIs 1-3 could even go over plain HTTP without SSL encryption - but this of course depends on your use case. In all cases the user data in the JWT should better be encrypted separately.
What flaws does this design have? What could be improved?
node.js
enough to recommend a library. In jwt.io you have library links for all languages. Checknode.js
section (only one – Balduin