JSON Web Token

{{Short description|Proposed web cryptography standard}}

{{Use American English|date=July 2019}}

{{Use mdy dates|date=July 2019}}

{{Infobox technology standard

| status = Proposed Standard

| first_published = {{Start date|2010|12|28}}

| version = {{IETF RFC|7519}}

| version_date = May 2015

| organization = IETF

| abbreviation = JWT

| native_name =

| native_name_lang =

| year_started =

| preview =

| preview_date =

| committee = IEGS

| series =

| editors =

| authors = {{Plainlist|

}}

| base_standards = {{Plainlist|

}}

| related_standards =

| domain = Data exchange

| license =

| copyright =

| website = {{URL|https://datatracker.ietf.org/doc/html/rfc7519}}

}}

JSON Web Token (JWT, suggested pronunciation {{IPAc-en|dʒ|ɒ|t}}, same as the word "jot"{{Cite IETF|title=JSON Web Token (JWT)|rfc=7519|last1=Jones|first1=Michael B.|last2=Bradley|first2=Bradley|last3=Sakimura|first3=Sakimura|date=May 2015|publisher=IETF|doi=10.17487/RFC7519|issn=2070-1721}}) is a proposed Internet standard for creating data with optional signature and/or optional encryption whose payload holds JSON that asserts some number of claims. The tokens are signed either using a private secret or a public/private key.

For example, a server could generate a token that has the claim "logged in as administrator" and provide that to a client. The client could then use that token to prove that it is logged in as admin. The tokens can be signed by one party's private key (usually the server's) so that any party can subsequently verify whether the token is legitimate. If the other party, by some suitable and trustworthy means, is in possession of the corresponding public key, they too are able to verify the token's legitimacy. The tokens are designed to be compact,{{cite book |last1=Nickel |first1=Jochen |title=Mastering Identity and Access Management with Microsoft Azure |date=2016 |isbn=9781785887888 |page=84 |publisher=Packt Publishing |url=https://books.google.com/books?id=Q4dcDgAAQBAJ&pg=PA84 |accessdate=20 July 2018}} URL-safe, and usable, especially in a web-browser single-sign-on (SSO) context. JWT claims can typically be used to pass identity of authenticated users between an identity provider and a service provider, or any other type of claims as required by business processes.{{Cite web|title = The Anatomy of a JSON Web Token|url = https://scotch.io/tutorials/the-anatomy-of-a-json-web-token|accessdate = 2015-05-08|first = Chris|last = Sevilleja}}{{Cite web|title = Atlassian Connect Documentation|url = https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html|website = developer.atlassian.com|accessdate = 2015-05-08|archive-date = May 18, 2015|archive-url = https://web.archive.org/web/20150518082002/https://developer.atlassian.com/static/connect/docs/latest/concepts/understanding-jwt.html|url-status = dead}}

JWT relies on other JSON-based standards: JSON Web Signature and JSON Web Encryption.{{Cite journal|title = draft-ietf-jose-json-web-signature-41 - JSON Web Signature (JWS)|url = https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41|website = tools.ietf.org| date=May 2015 |accessdate = 2015-05-08 | last1=Jones | first1=Michael B. | last2=Bradley | first2=John | last3=Sakimura | first3=Nat }}{{Cite journal|title = draft-ietf-jose-json-web-encryption-40 - JSON Web Encryption (JWE)|url = https://tools.ietf.org/html/draft-ietf-jose-json-web-encryption-40|website = tools.ietf.org| date=May 2015 |accessdate = 2015-05-08 | last1=Jones | first1=Michael B. | last2=Hildebrand | first2=Joe }}

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.{{Cite journal|title = draft-ietf-jose-json-web-algorithms-40 - JSON Web Algorithms (JWA)|url = https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40|website = tools.ietf.org| date=May 2015 |accessdate = 2015-05-08 | last1=Jones | first1=Michael B. }}

:{

"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. 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 {{IETF 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 are encoded separately using Base64url Encoding {{IETF 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.{{cite web |title=JWT.IO - JSON Web Tokens Introduction |url=https://jwt.io/introduction/ |website=jwt.io |accessdate=20 July 2018}}

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

class="wikitable"

! Code

! Name

! Description

colspan="2" {{rh}} | Standard claim fields

| The internet drafts define the following standard fields ("claims") that can be used inside a JWT claim set.

iss

|Issuer

|Identifies principal that issued the JWT.

sub

|Subject

|Identifies the subject of the JWT.

aud

|Audience

|Identifies 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.

exp

|Expiration Time

|Identifies the expiration time on and after which the JWT must not be accepted for processing. The value must be a NumericDate:{{Cite IETF|title=JSON Web Token (JWT)|rfc=7519|sectionname="exp" (Expiration Time) Claim|section=4.1.4|last1=Jones|first1=Michael B.|last2=Bradley|first2=Bradley|last3=Sakimura|first3=Sakimura|date=May 2015|publisher=IETF|doi=10.17487/RFC7519|issn=2070-1721}} either an integer or decimal, representing seconds past 1970-01-01 00:00:00Z.

nbf

|Not Before

|Identifies the time on which the JWT will start to be accepted for processing. The value must be a NumericDate.

iat

|Issued at

|Identifies the time at which the JWT was issued. The value must be a NumericDate.

jti

|JWT ID

|Case-sensitive unique identifier of the token even among different issuers.

colspan="2" {{rh}} | Commonly-used header fields

|The following fields are commonly used in the header of a JWT

typ

|Token type

|If present, it must be set to a registered [https://www.iana.org/assignments/media-types/media-types.xhtml IANA Media Type].

cty

|Content type

|If nested signing or encryption is employed, it is recommended to set this to JWT; otherwise, omit this field.

alg

|Message authentication code algorithm

|The issuer can freely set an algorithm to verify the signature on the token. However, some supported algorithms are insecure.

kid

|Key ID

|A 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.

x5c

|x.509 Certificate Chain

|A 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.

x5u

|x.509 Certificate Chain URL

|A 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.

crit

|Critical

|A list of headers that must be understood by the server in order to accept the token as valid

Code

! Name

! Description

List of currently registered claim names can be obtained from IANA JSON Web Token Claims Registry.{{Cite web|date=2015-01-23 |title=JSON Web Token (JWT) |url=https://www.iana.org/assignments/jwt/jwt.xhtml/ |website=IANA|language=en-US|access-date=2024-12-05}}

Implementations

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

{{colbegin|colwidth=20em}}

  • .NET (C# VB.Net etc.)[https://github.com/jwt-dotnet/jwt jwt-dotnet] on github.com
  • C[https://github.com/benmcollins/libjwt libjwt] on github.com
  • Clojure{{Cite web|url=https://github.com/liquidz/clj-jwt|title=liquidz/clj-jwt|website=GitHub|language=en|access-date=2018-05-07}}
  • Common Lisp[https://github.com/gschjetne/cljwt cljwt] on github.com
  • Dart[https://github.com/deftomat/JustJWT JustJWT] on github.com
  • Elixir{{Cite web|url=https://github.com/bryanjos/joken|title=bryanjos/joken|website=GitHub|language=en|access-date=2018-05-07}}
  • Erlang
  • Go{{Cite web|url=https://github.com/golang-jwt/jwt|title=golang-jwt/jwt|website=GitHub|language=en|access-date=2018-01-08}}
  • Haskell{{Cite web|url=https://hackage.haskell.org/package/jose|title=jose: JSON Object Signing and Encryption (JOSE) and JSON Web Token (JWT) library|website=Hackage|access-date=2022-12-25}}
  • Java[https://github.com/auth0/java-jwt auth0/java-jwt] on github.com
  • JavaScript{{Cite web|url=https://github.com/kjur/jsrsasign|title=kjur/jsrsasign|website=GitHub|language=en|access-date=2018-05-07}}
  • Lua{{Cite web|url=https://github.com/SkyLothar/lua-resty-jwt|title=SkyLothar/lua-resty-jwt|website=GitHub|language=en|access-date=2018-05-07}}
  • Node.js{{Cite web|url=https://www.npmjs.com/package/jsonwebtoken|title=jsonwebtoken|website=npm|access-date=2018-05-07}}
  • OCaml[https://github.com/besport/ocaml-jwt ocaml-jwt] on github.com
  • Perl[https://metacpan.org/pod/Crypt::JWT Crypt::JWT] on cpan.org
  • PHP[https://github.com/lcobucci/jwt lcobucci/jwt] on github.com
  • PL/SQL{{Citation|last=Egan|first=Morten|title=GitHub - morten-egan/jwt_ninja: PLSQL Implementation of JSON Web Tokens.|date=2019-02-07|url=https://github.com/morten-egan/jwt_ninja|access-date=2019-03-14}}
  • PowerShell{{Cite web|url=https://github.com/SP3269/posh-jwt|title=SP3269/posh-jwt|website=GitHub|access-date=2018-08-01}}
  • Python{{Cite web|url=https://github.com/jpadilla/pyjwt|title=jpadilla/pyjwt|website=GitHub|language=en|access-date=2017-03-21}}
  • Racket[https://pkgs.racket-lang.org/package/net-jwt net-jwt] on pkgs.racket-lang.org
  • Raku[https://github.com/jamesalbert/JSON-WebToken JSON-WebToken] on github.com
  • Ruby[https://github.com/jwt/ruby-jwt ruby-jwt] on github.com
  • Rust[https://github.com/Keats/jsonwebtoken jsonwebtoken] on github.com[https://github.com/mikkyang/rust-jwt rust-jwt] on github.com
  • Scala[https://github.com/pauldijou/jwt-scala jwt-scala] on github.com
  • Swift[https://github.com/kylef/JSONWebToken.swift] on github.com

{{colend}}

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.{{cite web |last1=Slootweg |first1=Sven |title=Stop using JWT for sessions |url=http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/ |website=joepie91 Ramblings |accessdate=1 August 2018}}

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.{{Cite web|first=Tim |last=McLean | date=March 31, 2015 | title = Critical vulnerabilities in JSON Web Token libraries|url = https://www.chosenplaintext.ca/2015/03/31/jwt-algorithm-confusion.html|publisher=Auth0|accessdate = 2016-03-29}} Still, new alg=none vulnerabilities are still being found in the wild, with four CVEs filed in the 2018-2021 period having this cause.{{cite web |title=CVE - Search Results |url=https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword=jwt+none |website=cve.mitre.org}}{{Better reference needed|date=January 2024}}

With proper design, developers can address algorithm vulnerabilities by taking precautions:{{Cite web|url=https://connect2id.com/products/nimbus-jose-jwt/vulnerabilities|language=en|access-date=2018-05-14|title=Common JWT security vulnerabilities and how to avoid them}}{{cite web |last1=Andreas |first1=Happe |title=JWT: Signature vs MAC attacks |url=https://snikt.net/blog/2019/05/16/jwt-signature-vs-mac-attacks/ |website=snikt.net |accessdate=27 May 2019}}

  1. Never let the JWT header alone drive verification
  2. Know the algorithms (avoid depending on the {{code|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.{{Cite web |title=Critical Vulnerability in JSON Web Encryption |url=https://auth0.com/blog/critical-vulnerability-in-json-web-encryption/ |access-date=2023-10-14 |website=Auth0 - Blog |language=en}}

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 frontends{{Cite web |title=No Way, JOSE! Javascript Object Signing and Encryption is a Bad Standard That Everyone Should Avoid - Paragon Initiative Enterprises Blog |url=https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid |access-date=2023-10-13 |website=paragonie.com}} and backends.{{Cite web |title=Pitfalls of JWT Authorization | url=https://authzed.com/blog/pitfalls-of-jwt-authorization | access-date=2023-11-16 | website=authzed.com}}

See also

References

{{Reflist|30em}}

  • {{IETF RFC|7519}}
  • [https://jwt.io/ jwt.io] – specialized website about JWT with tools and documentation, maintained by Auth0

{{Data exchange}}

Category:Computer access control

Category:Identity management

Category:Federated identity

Category:Identity management systems

Category:Metadata standards

Category:JSON