Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
JSON Web Token
JSON-based standard for passing claims between parties in web application environments

JSON Web Token (JWT) is a proposed Internet standard for creating compact, URL-safe tokens that carry a JSON payload asserting claims, such as user identity. Tokens are signed using either a private secret or public/private key, enabling recipients to verify legitimacy, often in a web-browser single-sign-on context. JWT facilitates transferring claims between an identity provider and a service provider, or other processes. It builds on related JSON standards like JSON Web Signature and JSON Web Encryption, supporting optional signature and encryption.

We don't have any images related to JSON Web Token yet.
We don't have any YouTube videos related to JSON Web Token yet.
We don't have any PDF documents related to JSON Web Token yet.
We don't have any Books related to JSON Web Token yet.
We don't have any archived web articles related to JSON Web Token yet.

Structure

Header Identifies which algorithm is used to generate the signature. In the below example, HS256 indicates that this token is signed using HMAC-SHA256. Typical cryptographic algorithms used are HMAC with SHA-256 (HS256) and RSA signature with SHA-256 (RS256). JWA (JSON Web Algorithms) RFC 7518 introduces many more for both authentication and encryption.9 { "alg": "HS256", "typ": "JWT" } Payload Contains a set of claims. The JWT specification defines seven Registered Claim Names, which are the standard fields commonly included in tokens.10 Custom claims are usually also included, depending on the purpose of the token. This example has the standard Issued At Time claim (iat) and a custom claim (loggedInAs). { "loggedInAs": "admin", "iat": 1422779638 } Signature Securely validates the token. The signature is calculated by encoding the header and payload using Base64url Encoding RFC 4648 and concatenating the two together with a period separator. That string is then run through the cryptographic algorithm specified in the header. This example uses HMAC-SHA256 with a shared secret (public key algorithms are also defined). The Base64url Encoding is similar to base64, but uses different non-alphanumeric characters and omits padding. HMAC_SHA256( secret, base64urlEncoding(header) + '.' + base64urlEncoding(payload) )

The three parts are encoded separately using Base64url Encoding RFC 4648, and concatenated using periods to produce the JWT:

const token = base64urlEncoding(header) + '.' + base64urlEncoding(payload) + '.' + base64urlEncoding(signature)

The above data and the secret of "secretkey" creates the token:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN _oWnFSRgCzcmJmMjLiuyu5CSpyHI=

(The above json strings are formatted without newlines or spaces, into utf-8 byte arrays. This is important as even slight changes in the data will affect the resulting token)

This resulting token can be easily passed into HTML and HTTP.11

Use

In authentication, when a user successfully logs in, a JSON Web Token (JWT) is often returned. This token should be sent to the client using a secure mechanism like an HTTP-only cookie. Storing the JWT locally in browser storage mechanisms like local or session storage is discouraged. This is because JavaScript running on the client-side (including browser extensions) can access these storage mechanisms, exposing the JWT and compromising security. For unattended processes, the client may also authenticate directly by generating and signing its own JWT with a pre-shared secret and pass it to a OAuth compliant service like so:

POST /oauth2/token Content-type: application/x-www-form-urlencoded grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=eyJhb...

If the client passes a valid JWT assertion the server will generate an access_token valid for making calls to the application and pass it back to the client:

{ "access_token": "eyJhb...", "token_type": "Bearer", "expires_in": 3600 }

When the client wants to access a protected route or resource, the user agent should send the JWT, typically in the Authorization HTTP header using the Bearer schema. The content of the header might look like the following:

Authorization: Bearer eyJhbGci...<snip>...yu5CSpyHI

This is a stateless authentication mechanism as the user state is never saved in server memory. The server's protected routes will check for a valid JWT in the Authorization header, and if it is present, the user will be allowed to access protected resources. As JWTs are self-contained, all the necessary information is there, reducing the need to query the database multiple times.

Standard fields

CodeNameDescription
Standard claim fieldsThe internet drafts define the following standard fields ("claims") that can be used inside a JWT claim set.
issIssuerIdentifies principal that issued the JWT.
subSubjectIdentifies the subject of the JWT.
audAudienceIdentifies the recipients that the JWT is intended for. Each principal intended to process the JWT must identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the aud claim when this claim is present, then the JWT must be rejected.
expExpiration TimeIdentifies the expiration time on and after which the JWT must not be accepted for processing. The value must be a NumericDate:12 either an integer or decimal, representing seconds past 1970-01-01 00:00:00Z.
nbfNot BeforeIdentifies the time on which the JWT will start to be accepted for processing. The value must be a NumericDate.
iatIssued atIdentifies the time at which the JWT was issued. The value must be a NumericDate.
jtiJWT IDCase-sensitive unique identifier of the token even among different issuers.
Commonly-used header fieldsThe following fields are commonly used in the header of a JWT
typToken typeIf present, it must be set to a registered IANA Media Type.
ctyContent typeIf nested signing or encryption is employed, it is recommended to set this to JWT; otherwise, omit this field.13
algMessage authentication code algorithmThe issuer can freely set an algorithm to verify the signature on the token. However, some supported algorithms are insecure.14
kidKey IDA hint indicating which key the client used to generate the token signature. The server will match this value to a key on file in order to verify that the signature is valid and the token is authentic.
x5cx.509 Certificate ChainA certificate chain in RFC4945 format corresponding to the private key used to generate the token signature. The server will use this information to verify that the signature is valid and the token is authentic.
x5ux.509 Certificate Chain URLA URL where the server can retrieve a certificate chain corresponding to the private key used to generate the token signature. The server will retrieve and use this information to verify that the signature is authentic.
critCriticalA list of headers that must be understood by the server in order to accept the token as valid
CodeNameDescription

List of currently registered claim names can be obtained from IANA JSON Web Token Claims Registry.15

Implementations

JWT implementations exist for many languages and frameworks, including but not limited to:

Vulnerabilities

JSON web tokens may contain session state. But if project requirements allow session invalidation before JWT expiration, services can no longer trust token assertions by the token alone. To validate that the session stored in the token is not revoked, token assertions must be checked against a data store. This renders the tokens no longer stateless, undermining the primary advantage of JWTs.41

Security consultant Tim McLean reported vulnerabilities in some JWT libraries that used the alg field to incorrectly validate tokens, most commonly by accepting a alg=none token. While these vulnerabilities were patched, McLean suggested deprecating the alg field altogether to prevent similar implementation confusion.42 Still, new alg=none vulnerabilities are still being found in the wild, with four CVEs filed in the 2018-2021 period having this cause.43[better source needed]

With proper design, developers can address algorithm vulnerabilities by taking precautions:4445

  1. Never let the JWT header alone drive verification
  2. Know the algorithms (avoid depending on the alg field alone)
  3. Use an appropriate key size

Several JWT libraries were found to be vulnerable to an invalid Elliptic-curve attack in 2017.46

Some have argued that JSON web tokens are difficult to use securely due to the many different encryption algorithms and options available in the standard, and that alternate standards should be used instead for both web frontends47 and backends.48

See also

  • RFC 7519
  • jwt.io – specialized website about JWT with tools and documentation, maintained by Auth0

References

  1. Jones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015). JSON Web Token (JWT). IETF. doi:10.17487/RFC7519. ISSN 2070-1721. RFC 7519. https://datatracker.ietf.org/doc/html/rfc7519

  2. Nickel, Jochen (2016). Mastering Identity and Access Management with Microsoft Azure. Packt Publishing. p. 84. ISBN 9781785887888. Retrieved July 20, 2018. 9781785887888

  3. "JWT.IO - JSON Web Tokens Introduction". jwt.io. Retrieved July 20, 2018. https://jwt.io/introduction/

  4. Sevilleja, Chris. "The Anatomy of a JSON Web Token". Retrieved May 8, 2015. https://scotch.io/tutorials/the-anatomy-of-a-json-web-token

  5. "Atlassian Connect Documentation". developer.atlassian.com. Archived from the original on May 18, 2015. Retrieved May 8, 2015. https://web.archive.org/web/20150518082002/https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html

  6. Jones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015). JSON Web Token (JWT). IETF. doi:10.17487/RFC7519. ISSN 2070-1721. RFC 7519. https://datatracker.ietf.org/doc/html/rfc7519

  7. Jones, Michael B.; Bradley, John; Sakimura, Nat (May 2015). "draft-ietf-jose-json-web-signature-41 - JSON Web Signature (JWS)". tools.ietf.org. Retrieved May 8, 2015. https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41

  8. Jones, Michael B.; Hildebrand, Joe (May 2015). "draft-ietf-jose-json-web-encryption-40 - JSON Web Encryption (JWE)". tools.ietf.org. Retrieved May 8, 2015. https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-40

  9. Jones, Michael B. (May 2015). "draft-ietf-jose-json-web-algorithms-40 - JSON Web Algorithms (JWA)". tools.ietf.org. Retrieved May 8, 2015. https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40

  10. Jones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015). JSON Web Token (JWT). IETF. doi:10.17487/RFC7519. ISSN 2070-1721. RFC 7519. https://datatracker.ietf.org/doc/html/rfc7519

  11. "JWT.IO - JSON Web Tokens Introduction". jwt.io. Retrieved July 20, 2018. https://jwt.io/introduction/

  12. Jones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015). ""exp" (Expiration Time) Claim". JSON Web Token (JWT). IETF. sec. 4.1.4. doi:10.17487/RFC7519. ISSN 2070-1721. RFC 7519. https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4

  13. Jones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015). JSON Web Token (JWT). IETF. doi:10.17487/RFC7519. ISSN 2070-1721. RFC 7519. https://datatracker.ietf.org/doc/html/rfc7519

  14. McLean, Tim (March 31, 2015). "Critical vulnerabilities in JSON Web Token libraries". Auth0. Retrieved March 29, 2016. https://www.chosenplaintext.ca/2015/03/31/jwt-algorithm-confusion.html

  15. "JSON Web Token (JWT)". IANA. January 23, 2015. Retrieved December 5, 2024. https://www.iana.org/assignments/jwt/jwt.xhtml/

  16. jwt-dotnet on github.com https://github.com/jwt-dotnet/jwt

  17. libjwt on github.com https://github.com/benmcollins/libjwt

  18. "liquidz/clj-jwt". GitHub. Retrieved May 7, 2018. https://github.com/liquidz/clj-jwt

  19. cljwt on github.com https://github.com/gschjetne/cljwt

  20. JustJWT on github.com https://github.com/deftomat/JustJWT

  21. "bryanjos/joken". GitHub. Retrieved May 7, 2018. https://github.com/bryanjos/joken

  22. "golang-jwt/jwt". GitHub. Retrieved January 8, 2018. https://github.com/golang-jwt/jwt

  23. "jose: JSON Object Signing and Encryption (JOSE) and JSON Web Token (JWT) library". Hackage. Retrieved December 25, 2022. https://hackage.haskell.org/package/jose

  24. auth0/java-jwt on github.com https://github.com/auth0/java-jwt

  25. "kjur/jsrsasign". GitHub. Retrieved May 7, 2018. https://github.com/kjur/jsrsasign

  26. "SkyLothar/lua-resty-jwt". GitHub. Retrieved May 7, 2018. https://github.com/SkyLothar/lua-resty-jwt

  27. "jsonwebtoken". npm. Retrieved May 7, 2018. https://www.npmjs.com/package/jsonwebtoken

  28. ocaml-jwt on github.com https://github.com/besport/ocaml-jwt

  29. Crypt::JWT on cpan.org https://metacpan.org/pod/Crypt::JWT

  30. lcobucci/jwt on github.com https://github.com/lcobucci/jwt

  31. Egan, Morten (February 7, 2019), GitHub - morten-egan/jwt_ninja: PLSQL Implementation of JSON Web Tokens., retrieved March 14, 2019 https://github.com/morten-egan/jwt_ninja

  32. "SP3269/posh-jwt". GitHub. Retrieved August 1, 2018. https://github.com/SP3269/posh-jwt

  33. "jpadilla/pyjwt". GitHub. Retrieved March 21, 2017. https://github.com/jpadilla/pyjwt

  34. net-jwt on pkgs.racket-lang.org https://pkgs.racket-lang.org/package/net-jwt

  35. JSON-WebToken on github.com https://github.com/jamesalbert/JSON-WebToken

  36. ruby-jwt on github.com https://github.com/jwt/ruby-jwt

  37. jsonwebtoken on github.com https://github.com/Keats/jsonwebtoken

  38. rust-jwt on github.com https://github.com/mikkyang/rust-jwt

  39. jwt-scala on github.com https://github.com/pauldijou/jwt-scala

  40. [1] on github.com https://github.com/kylef/JSONWebToken.swift

  41. Slootweg, Sven. "Stop using JWT for sessions". joepie91 Ramblings. Retrieved August 1, 2018. http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/

  42. McLean, Tim (March 31, 2015). "Critical vulnerabilities in JSON Web Token libraries". Auth0. Retrieved March 29, 2016. https://www.chosenplaintext.ca/2015/03/31/jwt-algorithm-confusion.html

  43. "CVE - Search Results". cve.mitre.org. https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=jwt+none

  44. "Common JWT security vulnerabilities and how to avoid them". Retrieved May 14, 2018. https://connect2id.com/products/nimbus-jose-jwt/vulnerabilities

  45. Andreas, Happe. "JWT: Signature vs MAC attacks". snikt.net. Retrieved May 27, 2019. https://snikt.net/blog/2019/05/16/jwt-signature-vs-mac-attacks/

  46. "Critical Vulnerability in JSON Web Encryption". Auth0 - Blog. Retrieved October 14, 2023. https://auth0.com/blog/critical-vulnerability-in-json-web-encryption/

  47. "No Way, JOSE! Javascript Object Signing and Encryption is a Bad Standard That Everyone Should Avoid - Paragon Initiative Enterprises Blog". paragonie.com. Retrieved October 13, 2023. https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid

  48. "Pitfalls of JWT Authorization". authzed.com. Retrieved November 16, 2023. https://authzed.com/blog/pitfalls-of-jwt-authorization