Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

JSON Web Token

From Wikipedia, the free encyclopedia
Proposed web cryptography standard

JSON Web Token
AbbreviationJWT
StatusProposed Standard
First publishedDecember 28, 2010 (2010-12-28)
Latest versionRFC 7519
May 2015
OrganizationIETF
CommitteeIEGS
Authors
Base standards
DomainData exchange
Websitedatatracker.ietf.org/doc/html/rfc7519

JSON Web Token (JWT, suggested pronunciation/ɒt/, same as the word "jot"[1]) is aproposed Internet standard for creating data with optionalsignature and/or optionalencryption whosepayload holdsJSON that asserts some number ofclaims. The tokens are signed either using aprivate secret or apublic/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. Thetokens are designed to be compact,[2]URL-safe,[3] and usable, especially in aweb-browsersingle-sign-on (SSO) context. JWT claims can typically be used to pass identity of authenticated users between anidentity provider and aservice provider, or any other type of claims as required by business processes.[4][5]

JWT relies on other JSON-based standards:JSON Web Signature andJSON Web Encryption.[1][6][7]

Structure

[edit]
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 areHMAC withSHA-256 (HS256) andRSA signature with SHA-256 (RS256). JWA (JSON Web Algorithms) RFC 7518 introduces many more for both authentication and encryption.[8]
{"alg":"HS256","typ":"JWT"}
Payload
Contains a set of claims. The JWT specification defines seven Registered Claim Names, which are thestandard fields commonly included in tokens.[1] 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 usingBase64url EncodingRFC 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). TheBase64url Encoding is similar tobase64, but uses different non-alphanumeric characters and omits padding.
HMAC_SHA256(secret,base64urlEncoding(header)+'.'+base64urlEncoding(payload))

The three are encoded separately usingBase64url EncodingRFC 4648, and concatenated using periods to produce the JWT:

consttoken=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 intoHTML andHTTP.[3]

Use

[edit]

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 anHTTP-only cookie. Storing the JWT locally in browser storage mechanisms likelocal 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. To make use of the HTTP-only cookie, as you might need it to authenticate with cross-origin APIs, the best approach is to use the credentials property to tell the browser to automatically send the cookies to the external APIs via a Fetch call like so:

fetch('https://api.example.com/data',{method:'GET',credentials:'include'// This tells the browser to include cookies, etc.}).then(response=>response.json()).then(data=>console.log(data)).catch(error=>console.error('Error:',error));

By using this method, the JWT is never exposed to client-side JavaScript, this is the best approach to make use of your JWT while maintaining security best practices. 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 aOAuth compliant service like so:

POST /oauth2/tokenContent-type:application/x-www-form-urlencodedgrant_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 theAuthorizationHTTP header using theBearer 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

[edit]
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, e.g. the name of an organization or a URL of a website.
subSubjectIdentifies the subject of the JWT, e.g. a username or account number.
audAudienceIdentifies the recipients that the JWT is intended for. Each principal intended to process the JWTmust identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in theaud claim when this claim is present, then the JWTmust be rejected.
expExpiration TimeIdentifies the expiration time on and after which the JWTmust not be accepted for processing. The value must be a NumericDate:[9] either an integer or decimal, representing seconds past1970-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 registeredIANA Media Type.
ctyContent typeIf nested signing or encryption is employed, it is recommended to set this toJWT; otherwise, omit this field.[1]
algMessage authentication code algorithmThe issuer can freely set an algorithm to verify the signature on the token. However, some supported algorithms are insecure.[10]
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 fromIANA JSON Web Token Claims Registry.[11]

Implementations

[edit]

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

Vulnerabilities

[edit]

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 adata store. This renders the tokens no longer stateless, undermining the primary advantage of JWTs.[38]

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

With proper design, developers can address algorithm vulnerabilities by taking precautions:[40][41]

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

Several JWT libraries were found to be vulnerable to aninvalid Elliptic-curve attack in 2017.[42]

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[43] and backends.[44]

See also

[edit]

References

[edit]
  1. ^abcdJones, Michael B.; Bradley, Bradley; Sakimura, Sakimura (May 2015).JSON Web Token (JWT).IETF.doi:10.17487/RFC7519.ISSN 2070-1721.RFC7519.
  2. ^Nickel, Jochen (2016).Mastering Identity and Access Management with Microsoft Azure. Packt Publishing. p. 84.ISBN 9781785887888. RetrievedJuly 20, 2018.
  3. ^ab"JWT.IO - JSON Web Tokens Introduction".jwt.io. RetrievedJuly 20, 2018.
  4. ^Sevilleja, Chris."The Anatomy of a JSON Web Token". RetrievedMay 8, 2015.
  5. ^"Atlassian Connect Documentation".developer.atlassian.com. Archived fromthe original on May 18, 2015. RetrievedMay 8, 2015.
  6. ^Jones, Michael B.; Bradley, John; Sakimura, Nat (May 2015)."draft-ietf-jose-json-web-signature-41 - JSON Web Signature (JWS)".tools.ietf.org. RetrievedMay 8, 2015.
  7. ^Jones, Michael B.; Hildebrand, Joe (May 2015)."draft-ietf-jose-json-web-encryption-40 - JSON Web Encryption (JWE)".tools.ietf.org. RetrievedMay 8, 2015.
  8. ^Jones, Michael B. (May 2015)."draft-ietf-jose-json-web-algorithms-40 - JSON Web Algorithms (JWA)".tools.ietf.org. RetrievedMay 8, 2015.
  9. ^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.RFC7519.
  10. ^abMcLean, Tim (March 31, 2015)."Critical vulnerabilities in JSON Web Token libraries". Auth0. RetrievedMarch 29, 2016.
  11. ^"JSON Web Token (JWT)".IANA. January 23, 2015. RetrievedDecember 5, 2024.
  12. ^jwt-dotnet ongithub.com
  13. ^libjwt ongithub.com
  14. ^"liquidz/clj-jwt".GitHub. RetrievedMay 7, 2018.
  15. ^cljwt ongithub.com
  16. ^JustJWT ongithub.com
  17. ^"bryanjos/joken".GitHub. RetrievedMay 7, 2018.
  18. ^"golang-jwt/jwt".GitHub. RetrievedJanuary 8, 2018.
  19. ^"jose: JSON Object Signing and Encryption (JOSE) and JSON Web Token (JWT) library".Hackage. RetrievedDecember 25, 2022.
  20. ^auth0/java-jwt ongithub.com
  21. ^"kjur/jsrsasign".GitHub. RetrievedMay 7, 2018.
  22. ^"JWTs.jl".GitHub. RetrievedOctober 31, 2025.
  23. ^"SkyLothar/lua-resty-jwt".GitHub. RetrievedMay 7, 2018.
  24. ^"jsonwebtoken".npm. RetrievedMay 7, 2018.
  25. ^ocaml-jwt ongithub.com
  26. ^Crypt::JWT oncpan.org
  27. ^lcobucci/jwt ongithub.com
  28. ^Egan, Morten (February 7, 2019),GitHub - morten-egan/jwt_ninja: PLSQL Implementation of JSON Web Tokens., retrievedMarch 14, 2019
  29. ^"SP3269/posh-jwt".GitHub. RetrievedAugust 1, 2018.
  30. ^"jpadilla/pyjwt".GitHub. RetrievedMarch 21, 2017.
  31. ^net-jwt onpkgs.racket-lang.org
  32. ^JSON-WebToken ongithub.com
  33. ^ruby-jwt ongithub.com
  34. ^jsonwebtoken ongithub.com
  35. ^rust-jwt ongithub.com
  36. ^jwt-scala ongithub.com
  37. ^[1] ongithub.com
  38. ^Slootweg, Sven."Stop using JWT for sessions".joepie91 Ramblings. RetrievedAugust 1, 2018.
  39. ^"CVE - Search Results".cve.mitre.org.
  40. ^"Common JWT security vulnerabilities and how to avoid them". RetrievedMay 14, 2018.
  41. ^Andreas, Happe."JWT: Signature vs MAC attacks".snikt.net. RetrievedMay 27, 2019.
  42. ^"Critical Vulnerability in JSON Web Encryption".Auth0 - Blog. RetrievedOctober 14, 2023.
  43. ^"No Way, JOSE! Javascript Object Signing and Encryption is a Bad Standard That Everyone Should Avoid - Paragon Initiative Enterprises Blog".paragonie.com. RetrievedOctober 13, 2023.
  44. ^"Pitfalls of JWT Authorization".authzed.com. RetrievedNovember 16, 2023.
  • RFC 7519
  • jwt.io – specialized website about JWT with tools and documentation, maintained by Auth0
Human
readable
Binary
Retrieved from "https://en.wikipedia.org/w/index.php?title=JSON_Web_Token&oldid=1323296072"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp