Sobes.tech
Middle+

Tell me about the JWT token: what parts does it consist of, and where is it safer to store?

sobes.tech AI

Answer from AI

JWT (JSON Web Token) consists of three parts separated by dots:

  1. Header — contains the token type (usually "JWT") and the signing algorithm (e.g., HS256).
  2. Payload — contains claims, such as user information, token expiration, and other data.
  3. Signature — created by hashing the encoded header and payload using a secret key or a private key (in case of asymmetric cryptography).

Example JWT:

xxxxx.yyyyy.zzzzz

where xxxxx — base64url(header), yyyyy — base64url(payload), zzzzz — signature.

Where is it safer to store JWT:

  • HttpOnly cookie — the safest way, as such cookies are inaccessible via JavaScript, reducing the risk of XSS attacks.
  • LocalStorage or SessionStorage — convenient but vulnerable to XSS, as scripts can access the token.

It is recommended to store JWT in an HttpOnly cookie with Secure and SameSite flags to protect against XSS and CSRF attacks. The server should correctly handle cookies and ensure secure transmission (HTTPS).

Tell me about the JWT token: what parts does it… - sobes.tech