<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tokens &amp; JWT · Brennenstuhl on Security</title><link>https://www.janbrennenstuhl.eu/topics/tokens-jwt/</link><description>JSON Web Tokens and the JOSE family: what tokens are for, how they are signed, encrypted and verified, and where they go wrong.</description><language>en-GB</language><copyright>Original writing CC BY 4.0, Jan Brennenstuhl</copyright><atom:link href="https://www.janbrennenstuhl.eu/topics/tokens-jwt/index.xml" rel="self" type="application/rss+xml"/><item><title>What is inside a JWT?</title><link>https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/</guid><pubDate>Tue, 13 Oct 2026 00:00:00 +0200</pubDate><description>&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; A signed JWT is 3 base64url-encoded parts joined by dots: a header that names the algorithm, a payload of claims, and a signature over both. The payload is encoded, not encrypted, so anyone holding the token can read it; only a signature check with a key you trust tells you whether to believe it.&lt;/p&gt;&lt;h2 id="key-findings"&gt;Key findings &lt;a class="heading-anchor" href="#key-findings" aria-label="Link to section: Key findings"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;figure class="finding" id="f-jwt-claims-in-jws-or-jwe"&gt;
&lt;blockquote&gt;&lt;p&gt;A JWT is a set of claims encoded as a JSON object inside a JWS or a JWE, so the same token format can be signed, encrypted, or both.&lt;/p&gt;&lt;/blockquote&gt;
&lt;figcaption&gt;&lt;span class="finding-source"&gt;Source: &lt;a href="https://www.rfc-editor.org/rfc/rfc7519#section-2" class="ext"&gt;&lt;cite&gt;RFC 7519, JSON Web Token (JWT), Section 2&lt;/cite&gt;&lt;/a&gt;&lt;/span&gt;&lt;p class="finding-tools"&gt;&lt;a class="finding-id" href="#f-jwt-claims-in-jws-or-jwe" title="#f-jwt-claims-in-jws-or-jwe"&gt;Link&lt;span class="sr-only"&gt; to this finding&lt;/span&gt;&lt;/a&gt;&lt;button type="button" class="text-button" data-quote="A JWT is a set of claims encoded as a JSON object inside a JWS or a JWE, so the same token format can be signed, encrypted, or both." data-quote-url="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/#f-jwt-claims-in-jws-or-jwe" hidden&gt;Copy quote&lt;/button&gt;&lt;a class="finding-card" href="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/f-jwt-claims-in-jws-or-jwe.png" download&gt;Save as image&lt;span class="sr-only"&gt; (PNG, 1200 by 630 pixels)&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure class="finding" id="f-jwt-payload-is-readable"&gt;
&lt;blockquote&gt;&lt;p&gt;The payload of a signed JWT is only base64url-encoded, so a token that carries privacy-sensitive claims needs encryption or must not reach parties who should not read it.&lt;/p&gt;&lt;/blockquote&gt;
&lt;figcaption&gt;&lt;span class="finding-source"&gt;Source: &lt;a href="https://www.rfc-editor.org/rfc/rfc7519#section-12" class="ext"&gt;&lt;cite&gt;RFC 7519, Section 12, Privacy Considerations&lt;/cite&gt;&lt;/a&gt;&lt;/span&gt;&lt;p class="finding-tools"&gt;&lt;a class="finding-id" href="#f-jwt-payload-is-readable" title="#f-jwt-payload-is-readable"&gt;Link&lt;span class="sr-only"&gt; to this finding&lt;/span&gt;&lt;/a&gt;&lt;button type="button" class="text-button" data-quote="The payload of a signed JWT is only base64url-encoded, so a token that carries privacy-sensitive claims needs encryption or must not reach parties who should not read it." data-quote-url="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/#f-jwt-payload-is-readable" hidden&gt;Copy quote&lt;/button&gt;&lt;a class="finding-card" href="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/f-jwt-payload-is-readable.png" download&gt;Save as image&lt;span class="sr-only"&gt; (PNG, 1200 by 630 pixels)&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure class="finding" id="f-jwt-registered-claims-optional"&gt;
&lt;blockquote&gt;&lt;p&gt;RFC 7519 registers 7 claim names, iss, sub, aud, exp, nbf, iat and jti, and none of them is mandatory in every case; each application profile decides which it requires.&lt;/p&gt;&lt;/blockquote&gt;
&lt;figcaption&gt;&lt;span class="finding-source"&gt;Source: &lt;a href="https://www.rfc-editor.org/rfc/rfc7519#section-4.1" class="ext"&gt;&lt;cite&gt;RFC 7519, Section 4.1&lt;/cite&gt;&lt;/a&gt;&lt;/span&gt;&lt;p class="finding-tools"&gt;&lt;a class="finding-id" href="#f-jwt-registered-claims-optional" title="#f-jwt-registered-claims-optional"&gt;Link&lt;span class="sr-only"&gt; to this finding&lt;/span&gt;&lt;/a&gt;&lt;button type="button" class="text-button" data-quote="RFC 7519 registers 7 claim names, iss, sub, aud, exp, nbf, iat and jti, and none of them is mandatory in every case; each application profile decides which it requires." data-quote-url="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/#f-jwt-registered-claims-optional" hidden&gt;Copy quote&lt;/button&gt;&lt;a class="finding-card" href="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/f-jwt-registered-claims-optional.png" download&gt;Save as image&lt;span class="sr-only"&gt; (PNG, 1200 by 630 pixels)&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;figure class="finding" id="f-jwt-allowlist-algorithms"&gt;
&lt;blockquote&gt;&lt;p&gt;JWT libraries must let the caller specify the supported algorithms and must not use any other algorithm, which closes the door on tokens whose header was changed to &amp;ldquo;none&amp;rdquo;.&lt;/p&gt;&lt;/blockquote&gt;
&lt;figcaption&gt;&lt;span class="finding-source"&gt;Source: &lt;a href="https://www.rfc-editor.org/rfc/rfc8725#section-3.1" class="ext"&gt;&lt;cite&gt;RFC 8725, JSON Web Token Best Current Practices, Section 3.1&lt;/cite&gt;&lt;/a&gt;&lt;/span&gt;&lt;p class="finding-tools"&gt;&lt;a class="finding-id" href="#f-jwt-allowlist-algorithms" title="#f-jwt-allowlist-algorithms"&gt;Link&lt;span class="sr-only"&gt; to this finding&lt;/span&gt;&lt;/a&gt;&lt;button type="button" class="text-button" data-quote="JWT libraries must let the caller specify the supported algorithms and must not use any other algorithm, which closes the door on tokens whose header was changed to &amp;ldquo;none&amp;rdquo;." data-quote-url="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/#f-jwt-allowlist-algorithms" hidden&gt;Copy quote&lt;/button&gt;&lt;a class="finding-card" href="https://www.janbrennenstuhl.eu/knowledge-base/oidc/jwt-anatomy/f-jwt-allowlist-algorithms.png" download&gt;Save as image&lt;span class="sr-only"&gt; (PNG, 1200 by 630 pixels)&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id="evidence"&gt;Evidence &lt;a class="heading-anchor" href="#evidence" aria-label="Link to section: Evidence"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;div class="table-scroll studies" tabindex="0" role="region" aria-label="Specifications"&gt;&lt;table&gt;
&lt;caption&gt;Specifications&lt;/caption&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope="col"&gt;Study&lt;/th&gt;&lt;th scope="col"&gt;Year&lt;/th&gt;&lt;th scope="col"&gt;Sample&lt;/th&gt;&lt;th scope="col"&gt;Method&lt;/th&gt;&lt;th scope="col"&gt;Finding&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;th scope="row"&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc7519" class="ext"&gt;RFC 7519: JSON Web Token (JWT)&lt;/a&gt;&lt;span class="org"&gt;IETF&lt;/span&gt;&lt;/th&gt;&lt;td&gt;2015&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Specification&lt;/td&gt;&lt;td&gt;Defines the JWT format, the registered claim names and the privacy considerations for claims.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope="row"&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc7515" class="ext"&gt;RFC 7515: JSON Web Signature (JWS)&lt;/a&gt;&lt;span class="org"&gt;IETF&lt;/span&gt;&lt;/th&gt;&lt;td&gt;2015&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Specification&lt;/td&gt;&lt;td&gt;Defines the compact serialization: base64url header, payload and signature joined by dots.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th scope="row"&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc8725" class="ext"&gt;RFC 8725: JSON Web Token Best Current Practices&lt;/a&gt;&lt;span class="org"&gt;IETF (BCP 225)&lt;/span&gt;&lt;/th&gt;&lt;td&gt;2020&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Best current practice&lt;/td&gt;&lt;td&gt;Requires algorithm allowlists in libraries and audience, issuer and key validation in applications.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;h2 id="what-to-do"&gt;What to do &lt;a class="heading-anchor" href="#what-to-do" aria-label="Link to section: What to do"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="for-engineers"&gt;For engineers &lt;a class="heading-anchor" href="#for-engineers" aria-label="Link to section: For engineers"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Treat a decoded payload as untrusted input until the signature verifies. Pin the algorithms you accept instead of reading &lt;code&gt;alg&lt;/code&gt; from the token, and resolve the key by &lt;code&gt;kid&lt;/code&gt; only from a key set you trust, such as the issuer&amp;rsquo;s &lt;a href="https://www.janbrennenstuhl.eu/jwks/"&gt;JWKS&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;After the signature, check &lt;code&gt;exp&lt;/code&gt;, &lt;code&gt;nbf&lt;/code&gt;, &lt;code&gt;iss&lt;/code&gt; and &lt;code&gt;aud&lt;/code&gt;. RFC 8725 makes audience validation a must whenever one issuer serves several relying parties.&lt;/li&gt;
&lt;li&gt;Keep secrets and personal data out of signed tokens. If a token has to carry them, use &lt;a href="https://www.janbrennenstuhl.eu/jwe/"&gt;JWE&lt;/a&gt; or keep the data on the server and put a reference in the token.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="for-product-and-ux"&gt;For product and UX &lt;a class="heading-anchor" href="#for-product-and-ux" aria-label="Link to section: For product and UX"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Tokens end up in logs, browser storage and support tickets. Anything in the payload should be safe to show to whoever might see it there.&lt;/li&gt;
&lt;li&gt;Short token lifetimes are a product decision as much as a security one: they decide how often a session has to be refreshed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="try-it"&gt;Try it &lt;a class="heading-anchor" href="#try-it" aria-label="Link to section: Try it"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;section class="explainer" data-explainer="jwt" aria-labelledby="ex-jwt"&gt;
&lt;h3 id="ex-jwt"&gt;Decode a token in your browser&lt;/h3&gt;
&lt;div class="explainer-static"&gt;&lt;p&gt;The example token below has 3 parts. Paste your own token into the decoder to split and decode it. Decoding happens only in your browser; nothing is sent anywhere, and the signature is not verified.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjIwMjYtMTAifQ.eyJpc3MiOiJodHRwczovL2lkLmV4YW1wbGUuY29tIiwic3ViIjoidXNlci0xMjMiLCJhdWQiOiJodHRwczovL2FwaS5leGFtcGxlLmNvbSIsImV4cCI6MTc5MTkwMDAwMCwiaWF0IjoxNzkxODk2NDAwLCJqdGkiOiI3ZjNjOWEifQ.cGxhY2Vob2xkZXIgc2lnbmF0dXJlLCBub3QgdmFsaWQ&lt;/code&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Header&lt;/strong&gt; decodes to &lt;code&gt;{&amp;quot;alg&amp;quot;:&amp;quot;ES256&amp;quot;,&amp;quot;typ&amp;quot;:&amp;quot;JWT&amp;quot;,&amp;quot;kid&amp;quot;:&amp;quot;2026-10&amp;quot;}&lt;/code&gt;: an ECDSA P-256 signature, and the ID of the key that made it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Payload&lt;/strong&gt; decodes to &lt;code&gt;{&amp;quot;iss&amp;quot;:&amp;quot;https://id.example.com&amp;quot;,&amp;quot;sub&amp;quot;:&amp;quot;user-123&amp;quot;,&amp;quot;aud&amp;quot;:&amp;quot;https://api.example.com&amp;quot;,&amp;quot;exp&amp;quot;:1791900000,&amp;quot;iat&amp;quot;:1791896400,&amp;quot;jti&amp;quot;:&amp;quot;7f3c9a&amp;quot;}&lt;/code&gt;: issued at 13:00 UTC on 13 October 2026, expiring 1 hour later.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Signature&lt;/strong&gt; is a placeholder in this example. In a real token it is the ES256 signature over the encoded header and payload, checked with the issuer&amp;rsquo;s public key from its JWKS.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="explainer-app" hidden&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;h2 id="how-the-parts-fit-together"&gt;How the parts fit together &lt;a class="heading-anchor" href="#how-the-parts-fit-together" aria-label="Link to section: How the parts fit together"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The header and payload are JSON objects. Each is encoded with base64url, a URL-safe variant of base64 without padding, and the two results are joined with a dot. The signer signs that string with the algorithm named in the header and appends the base64url-encoded signature after a second dot. That is the JWS compact serialization from RFC 7515.&lt;/p&gt;
&lt;p&gt;A verifier reverses the steps: split at the dots, decode the header, pick the key and algorithm it allows, and verify the signature over the first 2 parts exactly as received. Only then does it decode the payload and evaluate the claims. The essays on &lt;a href="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/"&gt;the purpose of JWT&lt;/a&gt;, &lt;a href="https://www.janbrennenstuhl.eu/jwa/"&gt;JSON Web Algorithms&lt;/a&gt; and &lt;a href="https://www.janbrennenstuhl.eu/jwt-vs-oauth/"&gt;JWT versus OAuth 2.0&lt;/a&gt; go deeper.&lt;/p&gt;
&lt;h2 id="related"&gt;Related &lt;a class="heading-anchor" href="#related" aria-label="Link to section: Related"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.janbrennenstuhl.eu/knowledge-base/agent-identity/token-exchange/"&gt;How does OAuth 2.0 Token Exchange keep provider tokens away from agents?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.janbrennenstuhl.eu/knowledge-base/oidc/openid-connect-security/"&gt;Where do OpenID Connect implementations break?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>What is JWA? A Deep Dive into JSON Web Algorithms</title><link>https://www.janbrennenstuhl.eu/jwa/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/jwa/</guid><pubDate>Thu, 08 Jun 2023 00:00:00 +0200</pubDate><description>&lt;h2 id="definition-of-jwa"&gt;Definition of JWA &lt;a class="heading-anchor" href="#definition-of-jwa" aria-label="Link to section: Definition of JWA"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;JSON Web Algorithms (JWA)&lt;/em&gt; is a specification providing a list of cryptographic algorithms. It&amp;rsquo;s an integral part of the &lt;em&gt;JSON Object Signing and Encryption (JOSE)&lt;/em&gt; family of standards. JOSE includes, for example, &lt;em&gt;JSON Web Tokens (JWT)&lt;/em&gt;, &lt;em&gt;JSON Web Signature (JWS)&lt;/em&gt;, and &lt;em&gt;&lt;a href="https://www.janbrennenstuhl.eu/jwe/" title="An Exploration into JSON Web Encryption Standards (JWE)"&gt;JSON Web Encryption (JWE)&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;JWA provides definitions for several cryptographic algorithms. These algorithms enable various purposes.&lt;/p&gt;
&lt;p&gt;In this guide, you’ll learn the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;mark&gt;How to decode JWA short names&lt;/mark&gt;&lt;/li&gt;
&lt;li&gt;&lt;mark&gt;What JWAs there are&lt;/mark&gt;&lt;/li&gt;
&lt;li&gt;&lt;mark&gt;Why JWA matters for OpenID Connect&lt;/mark&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="anatomy-of-json-web-algorithms"&gt;Anatomy of JSON Web Algorithms &lt;a class="heading-anchor" href="#anatomy-of-json-web-algorithms" aria-label="Link to section: Anatomy of JSON Web Algorithms"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The JWA specification defines a unique name for every algorithm. These names are short strings encoding the particular algorithm family and hashing algorithm. Variants for SHA-256, SHA-384, and SHA-512 exist for most signing algorithms.&lt;/p&gt;
&lt;p&gt;The most common algorithm names defined in the JWA specification include the following:&lt;/p&gt;
&lt;div class="table-scroll" tabindex="0" role="region" aria-label="Table 1"&gt;&lt;table class="table-striped"&gt;
&lt;thead&gt;&lt;tr&gt;&lt;th scope="col"&gt;Algorithm Name&lt;/th&gt;&lt;th scope="col"&gt;Algorithm Description&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;HS256&lt;/td&gt;&lt;td&gt;HMAC with SHA-256&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RS256&lt;/td&gt;&lt;td&gt;RSA signature with SHA-256&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ES256&lt;/td&gt;&lt;td&gt;ECDSA using P-256 and SHA-256&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;RSA-OAEP&lt;/td&gt;&lt;td&gt;RSAES OAEP using default parameters&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;A128KW&lt;/td&gt;&lt;td&gt;AES key wrap with 128-bit key&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc7518" title="RFC: JSON Web Algorithms (JWA)" class="ext"&gt;RFC 7518&lt;/a&gt; defines the fundamental base JSON Web Algorithms. The &lt;a href="https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms" title="JSON Web Signature and Encryption Algorithms" class="ext"&gt;JOSE IANA registry&lt;/a&gt; provides a complete list of supported algorithms. These unique references allow receivers to identify the correct cryptographical procedure. They are part of the JOSE header.&lt;/p&gt;
&lt;h2 id="different-types-of-json-web-algorithms"&gt;Different Types of JSON Web Algorithms &lt;a class="heading-anchor" href="#different-types-of-json-web-algorithms" aria-label="Link to section: Different Types of JSON Web Algorithms"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The JWA specification defines many cryptographic algorithms powering various use cases, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Signing with JSON Web Signature (JWS)&lt;/strong&gt; ensures a token&amp;rsquo;s integrity during transit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encryption with JSON Web Encryption (JWE)&lt;/strong&gt; to safeguard the confidentiality of token payload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Distribution with JSON Web Keys (JWK)&lt;/strong&gt; to enable distributed JWT validation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All JSON Web Algorithms are either symmetric or asymmetric.&lt;/p&gt;
&lt;h3 id="understanding-the-symmetric-key-algorithms"&gt;Understanding the Symmetric Key Algorithms &lt;a class="heading-anchor" href="#understanding-the-symmetric-key-algorithms" aria-label="Link to section: Understanding the Symmetric Key Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Hash-Based Message Authentication Codes (HMAC) are a popular algorithm class for signed JWTs. HMACs like HS256 provide a way of signing messages using a shared key. They belong to the family of symmetric cryptographic algorithms.&lt;/p&gt;
&lt;p&gt;Symmetric algorithms need all involved parties to know the same key. They are helpful when all clients can guarantee the secrecy of the shared key. Their permissive nature allows all key possessors to create and verify JWTs. HMACs are best used in setups with one central issuer/ verifier.&lt;/p&gt;
&lt;h3 id="delving-into-asymmetric-key-algorithms"&gt;Delving into Asymmetric Key Algorithms &lt;a class="heading-anchor" href="#delving-into-asymmetric-key-algorithms" aria-label="Link to section: Delving into Asymmetric Key Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWA specifies two asymmetric encryption and digital signature algorithm families – RSA and ECDSA. Public-private key pairs form the foundation of asymmetric algorithms. They enable decoupling verification or decryption of a token from being able to forge a new one. This is essential for distributed token validation scenarios. It allows decentralized authorization decision-making in OAuth 2.0 and OpenID Connect (OIDC).&lt;/p&gt;
&lt;h2 id="practical-applications-of-jwa"&gt;Practical Applications of JWA &lt;a class="heading-anchor" href="#practical-applications-of-jwa" aria-label="Link to section: Practical Applications of JWA"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A widespread use case is decentralized token signature verification for asymmetrically signed JWT. It&amp;rsquo;s a key enabler for OpenID Connect.&lt;/p&gt;
&lt;p&gt;At the forefront of this practical application is a central authorization server. This token issuer holds a private signing key and exposes its public key material. Clients in need to verify tokens fetch the public keys from the &lt;a href="https://www.janbrennenstuhl.eu/jwks/" title="JSON Web Key Sets Explained"&gt;well-known JWK endpoint&lt;/a&gt;. Based on cryptographic meta information carried in the JWT header, clients identify the matching key and JWA. This way, relying parties can verify a token&amp;rsquo;s integrity without the need for centralized introspection.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion &lt;a class="heading-anchor" href="#conclusion" aria-label="Link to section: Conclusion"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;JWA is part of the JSON Web Token (JWT) suite of technologies. It&amp;rsquo;s a collection of cryptographic algorithms. Being a vital ingredient of every JWT, JWA powers the entire JOSE toolbox. These JOSE technologies build secure systems that can interoperate easily. OpenID Connect is a famous example of such a framework.&lt;/p&gt;
&lt;p&gt;Please note that each JSON Web Algorithm has its security implications. Appropriate use cases depend on the specifics of the implementation. It is paramount to choose algorithms that best meet your specific security requirements.&lt;/p&gt;</description></item><item><title>Mastering JWKS: JSON Web Key Sets Explained</title><link>https://www.janbrennenstuhl.eu/jwks/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/jwks/</guid><pubDate>Fri, 02 Jun 2023 00:00:00 +0200</pubDate><description>&lt;h2 id="tldr"&gt;TL;DR &lt;a class="heading-anchor" href="#tldr" aria-label="Link to section: TL;DR"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;JWKS (JSON Web Key Set) is a tool for standardizing the representation and management of cryptographic keys via a JSON object.&lt;/li&gt;
&lt;li&gt;The technology, defined in RFC 7517, enables client-local &lt;a href="https://www.janbrennenstuhl.eu/topics/tokens-jwt/" title="JWT Security: Best practices and when to use JSON Web Token"&gt;JWT (JSON Web Tokens)&lt;/a&gt; verification and is integral for decentralized authentication protocols such as OpenID Connect and &lt;a href="https://www.janbrennenstuhl.eu/topics/identity-authentication/" title="OAuth 2.0 in Action"&gt;OAuth2&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Implementing JWKS simplifies key management, eases key rotation, and secures key distribution across multiple servers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="technical-specifications-of-jwk-and-jwks"&gt;Technical Specifications of JWK and JWKS &lt;a class="heading-anchor" href="#technical-specifications-of-jwk-and-jwks" aria-label="Link to section: Technical Specifications of JWK and JWKS"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;JWKS is an acronym for JSON Web Key Set. It is a collection of JSON Web Keys (JWKs) used to verify JSON Web Tokens (JWTs). Both constructs are defined in &lt;a href="https://datatracker.ietf.org/doc/html/rfc7517" title="RFC 7517: JSON Web Key (JWK)" class="ext"&gt;RFC 7517&lt;/a&gt;, yet another integral member of the famous JOSE standards family. JWK Sets provide a standardized way and format for servers to obtain the public keys required to verify asymmetrically signed JWTs locally.&lt;/p&gt;
&lt;h3 id="what-is-json-web-key-jwk"&gt;What is JSON Web Key (JWK)? &lt;a class="heading-anchor" href="#what-is-json-web-key-jwk" aria-label="Link to section: What is JSON Web Key (JWK)?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A JWK is a JSON object data structure that represents a cryptographic key. It contains the key type, key operations, algorithm, and all relevant material to reconstruct the signature verification key. All officially supported &lt;a href="https://www.janbrennenstuhl.eu/jwa/" title="What is JWA?"&gt;JSON Web Algorithms (JWA)&lt;/a&gt; are defined in &lt;a href="https://datatracker.ietf.org/doc/html/rfc7518" title="RFC 7518: JSON Web Algorithms (JWA)" class="ext"&gt;RFC 7518&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The JSON data structure of the JWK format allows for easy, web-native exchange of public keys. The representation of an Elliptic Curve signature key of curve P-256 could look like this:&lt;/p&gt;
&lt;figure class="code" data-lang="json"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" aria-label="Code (json)" class="chroma"&gt;&lt;code data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;kty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;EC&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;use&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;sig&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;crv&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;P-256&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;kid&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;01H1SG7BX197N040C0MHTDV1HR&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;x&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;SEfcECpwqQg-vZ6Lv99RyV0Qkatngz1RV25nI5SOrPg&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;y&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;YU2PRA6pXZ62OW_XuzjJqplqmBUtBwT2pKUZUVxUYfc&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;alg&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;ES256&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/figure&gt;
&lt;h3 id="what-is-json-web-key-set-jwks"&gt;What is JSON Web Key Set (JWKS)? &lt;a class="heading-anchor" href="#what-is-json-web-key-set-jwks" aria-label="Link to section: What is JSON Web Key Set (JWKS)?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A JWKS is a JSON object that represents a set of JWKs. It must contain a &amp;ldquo;keys&amp;rdquo; member and an array of JWKs. JSON Web Key Sets enable identity providers to support and expose more than one public key under a well-known resource. The following example JWK Set format contains two public cryptographic keys – one using an Elliptic Curve (EC) algorithm and a second one using an RSA algorithm:&lt;/p&gt;
&lt;figure class="code" data-lang="text"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" aria-label="Code" class="chroma"&gt;&lt;code data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;{&amp;#34;keys&amp;#34;:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; [{
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;kty&amp;#34;: &amp;#34;EC&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;use&amp;#34;: &amp;#34;sig&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;crv&amp;#34;: &amp;#34;P-256&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;kid&amp;#34;: &amp;#34;01H1SG7BX197N040C0MHTDV1HR&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;x&amp;#34;: &amp;#34;SEfcECpwqQg-vZ6Lv99RyV0Qkatngz1RV25nI5SOrPg&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;y&amp;#34;: &amp;#34;YU2PRA6pXZ62OW_XuzjJqplqmBUtBwT2pKUZUVxUYfc&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;alg&amp;#34;: &amp;#34;ES256&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }, {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;kty&amp;#34;:&amp;#34;RSA&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;n&amp;#34;: &amp;#34;1qrQCTst3RF04aMC9Ye_kGbsE0sftL4FOtB_WrzBDOFdrfVwLfflQuPX5kJ-0iYv9r2mjD5YIDy8b-iJKwevb69ISeoOrmL3tj6MStJesbbRRLVyFIm_6L7alHhZVyqHQtMKX7IaNndrfebnLReGntuNk76XCFxBBnRaIzAWnzr3WN4UPBt84A0KF74pei17dlqHZJ2HB2CsYbE9Ort8m7Vf6hwxYzFtCvMCnZil0fCtk2OQ73l6egcvYO65DkAJibFsC9xAgZaF-9GYRlSjMPd0SMQ8yU9i3W7beT00Xw6C0FYA9JAYaGaOvbT87l_6ZkAksOMuvIPD_jNVfTCPLQ&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;e&amp;#34;:&amp;#34;AQAB&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;alg&amp;#34;:&amp;#34;RS256&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &amp;#34;kid&amp;#34;:&amp;#34;01H1SGVCX7GKBGE2J2QMQREAGN&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }]
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/figure&gt;
&lt;h3 id="locate-json-web-key-sets"&gt;Locate JSON Web Key Sets &lt;a class="heading-anchor" href="#locate-json-web-key-sets" aria-label="Link to section: Locate JSON Web Key Sets"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Many modern identity providers like Google expose their JWK Sets via the &lt;code&gt;jwks_uri&lt;/code&gt; attribute within the &lt;a href="https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig" title="OpenID Connect Discovery Spec" class="ext"&gt;well-known discovery endpoint&lt;/a&gt; of OpenID Connect (OIDC) located at &lt;code&gt;https://{identity-provider}/.well-known/openid-configuration&lt;/code&gt; which often points to &lt;code&gt;https://{identity-provider}/.well-known/jwks.json&lt;/code&gt; or a similar resource.&lt;/p&gt;
&lt;h2 id="benefits-of-using-jwks"&gt;Benefits of Using JWKS &lt;a class="heading-anchor" href="#benefits-of-using-jwks" aria-label="Link to section: Benefits of Using JWKS"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="easy-management-of-keys-with-jwk-sets"&gt;Easy Management of Keys with JWK Sets &lt;a class="heading-anchor" href="#easy-management-of-keys-with-jwk-sets" aria-label="Link to section: Easy Management of Keys with JWK Sets"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One of the main benefits of using JWKs is that they provide an easy way to manage and distribute keys across multiple servers or locations. Instead of hardcoding public keys into application code, token issuers (authorization servers) only need to provide their clients with an URL for their JSON Web Key Set.&lt;/p&gt;
&lt;p&gt;Any application that needs access to the keys can retrieve them from that remote location. This eliminates the need for manual key distribution, resolves, and ensures all applications use the same set of keys. This makes it easier to manage multiple keys and rotate them when necessary.&lt;/p&gt;
&lt;h3 id="simplified-key-rotation-process-using-jwks"&gt;Simplified Key Rotation Process Using JWKs &lt;a class="heading-anchor" href="#simplified-key-rotation-process-using-jwks" aria-label="Link to section: Simplified Key Rotation Process Using JWKs"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Rotating keys is essential for maintaining security in any system that uses cryptography. However, manually replacing these keys can take time and effort. JWKS can simplify this process by automating key rotation.&lt;/p&gt;
&lt;p&gt;As JWKs decouple the authorization server from relying party applications, token issuers can generate new asymmetric key pairs whenever necessary. Setting up regular intervals for key rotation, publishing the new public key material, and using the latest private key automatically after some grace period, can ensure that the amount of keys signed with a particular key has a natural upper limit.&lt;/p&gt;
&lt;h3 id="compatibility-with-various-protocols-and-applications"&gt;Compatibility with Various Protocols and Applications &lt;a class="heading-anchor" href="#compatibility-with-various-protocols-and-applications" aria-label="Link to section: Compatibility with Various Protocols and Applications"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Finally, the JSON Web Key Set standard is compatible with various protocols and applications. This means it can enable local token validation in the context of web applications, APIs, mobile apps, and more. Popular authentication protocols such as OAuth 2.0 and OpenID Connect support JSON Web Key Set.&lt;/p&gt;
&lt;h2 id="best-practices--security-considerations"&gt;Best Practices &amp;amp; Security Considerations &lt;a class="heading-anchor" href="#best-practices--security-considerations" aria-label="Link to section: Best Practices &amp;amp; Security Considerations"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Dealing with cryptographic and primarily private keys is a highly sensitive undertaking. The security issues relevant to any cryptographic application must also be considered when dealing with JSON Web Token (JWT) and JSON Web Signature (JWS). It is essential to implement JSON Web Key Set (JWKS) correctly to ensure the security and integrity of the entire infrastructure. Here are some best practices for implementing JWKS.&lt;/p&gt;
&lt;h3 id="use-transport-layer-security"&gt;Use Transport Layer Security &lt;a class="heading-anchor" href="#use-transport-layer-security" aria-label="Link to section: Use Transport Layer Security"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Serving a JSON Web Key over HTTP poses significant security risks as it can be intercepted or modified in transit. Therefore, it is crucial to use HTTPS also when serving JWKS. HTTPS ensures that all communication between the client and server is encrypted, preventing unauthorized access or in-transit modification.&lt;/p&gt;
&lt;h3 id="make-sure-jwks-is-correct-and-current"&gt;Make sure JWKS is correct and current &lt;a class="heading-anchor" href="#make-sure-jwks-is-correct-and-current" aria-label="Link to section: Make sure JWKS is correct and current"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The entire system&amp;rsquo;s security relies on having up-to-date and accurate JWKS. If any cryptographic keys in the set expire or become invalid, it could result in rejected tokens or, worse yet, accepted but fraudulent tokens being used throughout the system. It&amp;rsquo;s essential to have processes in place to monitor and update JSON Web Keys regularly. This includes rotating keys, removing EOL keys, and adding new keys as necessary. Cryptographic key management should be automated to ensure that all cryptographic keys are always up-to-date. Symmetric keys must not be exposed!&lt;/p&gt;
&lt;h3 id="validate-the-signatures-of-incoming-jwts-using-jwks"&gt;Validate the signatures of incoming JWTs using JWKs &lt;a class="heading-anchor" href="#validate-the-signatures-of-incoming-jwts-using-jwks" aria-label="Link to section: Validate the signatures of incoming JWTs using JWKs"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One of the primary purposes of implementing JWKS is to verify the authenticity of incoming JWTs – be it access token or ID token. To do this, all applications that perform authorization decisions based on JSON &lt;a href="https://www.janbrennenstuhl.eu/single-use-jwt/" title="Single-Use JWT: The Power of Stateless One Time Token"&gt;Web Token&lt;/a&gt;, should fetch the entire JSON Web Key Set per the corresponding cache-control headers.&lt;/p&gt;
&lt;p&gt;Validating a JWT starts with fetching the &lt;code&gt;kid&lt;/code&gt; (key ID) parameter from its header. This key identifier specifies which cryptographic key in the JWKS was used by the authorization server to sign the token. The identified key can then be used to verify the signature by comparing it against the one included in the token&amp;rsquo;s signature.&lt;/p&gt;
&lt;p&gt;If the signature is valid, the token was issued by a trusted party and can be processed accordingly. An invalid token must be rejected immediately.&lt;/p&gt;
&lt;h3 id="consider-rotating-your-jwks-keys-periodically"&gt;Consider rotating your JWKS keys periodically &lt;a class="heading-anchor" href="#consider-rotating-your-jwks-keys-periodically" aria-label="Link to section: Consider rotating your JWKS keys periodically"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Rotating JSON Web Keys periodically is an essential best practice for maintaining a proper security posture. Doing so reduces the risk of compromised keys being used maliciously throughout the system. Rotating keys effectively requires creating a new set before retiring any old ones. This will ensure no gaps in coverage during the transition period.&lt;/p&gt;
&lt;h2 id="conclusion-the-importance-of-using-json-web-key-sets"&gt;Conclusion: The Importance of Using JSON Web Key Sets &lt;a class="heading-anchor" href="#conclusion-the-importance-of-using-json-web-key-sets" aria-label="Link to section: Conclusion: The Importance of Using JSON Web Key Sets"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Implementing JSON Web Key Sets (JWKS) is crucial for organizations that leverage decentralized authentication protocols like OpenID Connect or authorization delegation protocols like plain OAuth2. Truly benefiting from public-key cryptography requires client applications to verify issued tokens locally, without indispensable synchronous HTTP requests.&lt;/p&gt;
&lt;p&gt;Using a data interchange format like JSON Web Key (JWK), which is based on JavaScript Object Notation (JSON), and hosting the cryptographic keys on publicly accessible, standardized discovery endpoints allows maintaining decoupled service architectures while shifting effort to the client-side following a pull principle.&lt;/p&gt;</description></item><item><title>JSON Web Encryption: Exploring JWE Token</title><link>https://www.janbrennenstuhl.eu/jwe/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/jwe/</guid><pubDate>Mon, 29 May 2023 00:00:00 +0200</pubDate><description>&lt;h2 id="tldr"&gt;TL;DR &lt;a class="heading-anchor" href="#tldr" aria-label="Link to section: TL;DR"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;JSON Web Encryption (JWE) is a standard for securing data transmission between parties, providing confidentiality, integrity, and authentication. It uses encryption to protect the payload of a &lt;a href="https://www.janbrennenstuhl.eu/topics/tokens-jwt/" title="JWT Security: Best practices and when to use JSON Web Token"&gt;JSON Web Token (JWT)&lt;/a&gt; and supports both symmetric and asymmetric encryption techniques. It also allows creating nested JWTs, combining the advantages of JWE and JSON Web Signature (JWS).&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;JWE token&lt;/strong&gt; consists of five parts: the Protected Header, the Content Encryption Key, the Initialization Vector, the Ciphertext, and the Authentication Tag. It supports various encryption algorithms like RSA-OAEP, AES-GCM, and ECDH-ES and requires using authenticated encryption to ensure the token&amp;rsquo;s integrity and confidentiality.&lt;/li&gt;
&lt;li&gt;Modern authentication and authorization frameworks like OAuth2 and &lt;a href="https://www.janbrennenstuhl.eu/topics/identity-authentication/" title="OpenID Connect: User Authentication &amp;amp; Identity"&gt;OpenID Connect (OIDC)&lt;/a&gt; use &lt;strong&gt;JWE tokens&lt;/strong&gt; as bearer tokens. Proper key management is crucial to maintain the security guarantees of JWE.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="json-web-encryption-jwe-token-a-comprehensive-guide"&gt;JSON Web Encryption (JWE) Token: A Comprehensive Guide &lt;a class="heading-anchor" href="#json-web-encryption-jwe-token-a-comprehensive-guide" aria-label="Link to section: JSON Web Encryption (JWE) Token: A Comprehensive Guide"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;JWE is a widely-used standard for &lt;strong&gt;securing data transmission&lt;/strong&gt; between parties. The industry standard describes a JSON Web Token (JWT) type that uses encryption to protect its &lt;strong&gt;payload&lt;/strong&gt;. It provides &lt;strong&gt;confidentiality&lt;/strong&gt; by encrypting the transmitted data, ensuring only &lt;strong&gt;authorized parties&lt;/strong&gt; can access and read it.&lt;/p&gt;
&lt;p&gt;In addition to providing confidentiality, &lt;strong&gt;JWE tokens&lt;/strong&gt; offer &lt;strong&gt;integrity&lt;/strong&gt; protection through message authentication codes (MACs). These codes ensure the receiver can detect tampering with the &lt;strong&gt;encrypted payload&lt;/strong&gt;. Depending on the key management mode, it can also provide authentication.&lt;/p&gt;
&lt;p&gt;JWE is relevant for modern AAA concepts in distributed, stateless environments as it enables an exchange of &lt;strong&gt;sensitive information&lt;/strong&gt; statelessly.&lt;/p&gt;
&lt;h3 id="the-structure-of-an-encrypted-jwt"&gt;The Structure of an Encrypted JWT &lt;a class="heading-anchor" href="#the-structure-of-an-encrypted-jwt" aria-label="Link to section: The Structure of an Encrypted JWT"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The JWE standard (&lt;a href="https://datatracker.ietf.org/doc/html/rfc7516" title="JSON Web Encryption (JWE)" class="ext"&gt;RFC 7516&lt;/a&gt;) is the go-to solution for developers who need to transmit JSON objects over the internet securely. JWE is a standard that defines how to encrypt and decrypt data in &lt;strong&gt;JSON format&lt;/strong&gt; using various algorithms. In this section, I will discuss some of the core concepts of JWE tokens.&lt;/p&gt;
&lt;p&gt;A typical, compact serialized, &lt;strong&gt;encrypted JWT&lt;/strong&gt; is a dot (.) separated string that contains five parts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The JWE Protected Header is a base64url-encoded string representing the token metadata. It describes the JWE, how it was encrypted, and the media type of the encrypted payload. Protected JWE headers can’t be encrypted as they carry all information necessary for decryption.&lt;/li&gt;
&lt;li&gt;The JWE Encrypted Key (CEK) is another base64url-encoded string representing the encrypted, symmetric key used to encrypt the payload.&lt;/li&gt;
&lt;li&gt;The JWE Initialization Vector (IV) is a base64-encoded unique, random value string used when encrypting the plaintext and helps prevent attackers from exploiting patterns in plaintext or ciphertext.&lt;/li&gt;
&lt;li&gt;The JWE Ciphertext, the actual encrypted payload, also base64url-encoded, which was encrypted using the CEK, along with the IV and the additional authenticated data (AAD).&lt;/li&gt;
&lt;li&gt;The JWE Authentication Tag (MAC) results from performing &lt;strong&gt;authenticated encryption&lt;/strong&gt; and allows a verifier to prove the integrity of the ciphertext and the AAD (the encoded protected header when using compact serialization). It ensures integrity during decryption.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="support-for-symmetric-and-asymmetric-encryption-algorithms"&gt;Support for Symmetric and Asymmetric Encryption Algorithms &lt;a class="heading-anchor" href="#support-for-symmetric-and-asymmetric-encryption-algorithms" aria-label="Link to section: Support for Symmetric and Asymmetric Encryption Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Encrypted JWTs support asymmetric and &lt;strong&gt;symmetric encryption&lt;/strong&gt; techniques to provide &lt;strong&gt;end-to-end security&lt;/strong&gt; for the transmitted data. Symmetric &lt;strong&gt;encryption algorithms&lt;/strong&gt; use the same key for encryption and decryption, while asymmetric algorithms use a pair of keys.&lt;/p&gt;
&lt;p&gt;The sender encrypts the plaintext payload using a randomly generated symmetric CEK, then encrypts the key using the recipient&amp;rsquo;s public key or a shared secret key (a long-lived key encryption key). The recipient can then decrypt the encrypted key using their private secret key and use it to decrypt the payload.&lt;/p&gt;
&lt;p&gt;As they are self-contained and carry their corresponding symmetric key (CEK), encrypted JWTs also support hybrid encryption, which allows combining the performance of symmetric encryption with the assurances of public-key cryptography. This flexibility lets developers choose between different encryption algorithms based on their needs.&lt;/p&gt;
&lt;h3 id="nested-jwts"&gt;Nested JWTs &lt;a class="heading-anchor" href="#nested-jwts" aria-label="Link to section: Nested JWTs"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Another exciting feature of JWE is the ability to create nested JWTs. As the JWE standard allows arbitrary data to be packaged into the payload, it also allows nesting a signed JWT (JWS) inside an encrypted JWT (JWE). Wrapping a JWS into a JWE can be helpful when dealing with asymmetrically encrypted JWE, as these alone do not allow to verify that the JWT was produced by a specific token issuer, merely that someone who possessed the issuer&amp;rsquo;s public key forged the token. Nested JWTs combine the advantages of JWE and JWS. Symmetrically encrypted JWEs provide all the above security properties without creating nested tokens.&lt;/p&gt;
&lt;h2 id="working-with-different-jwe-algorithms"&gt;Working with Different JWE Algorithms &lt;a class="heading-anchor" href="#working-with-different-jwe-algorithms" aria-label="Link to section: Working with Different JWE Algorithms"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;a href="https://www.janbrennenstuhl.eu/jwa/" title="What is JWA?"&gt;JSON Web Algorithms (JWA)&lt;/a&gt; standard registers all cryptographic algorithms supported by the JWT ecosystem in &lt;a href="https://datatracker.ietf.org/doc/html/rfc7518" title="JSON Web Algorithms (JWA)" class="ext"&gt;RFC 7518&lt;/a&gt;. The most commonly recommended option for asymmetric key encryption is ECDH-ES, while AES-GCM (with key wrapping) is the recommended alternative for symmetric encryption setups.&lt;/p&gt;
&lt;h3 id="jwe-with-aes-algorithms"&gt;JWE with AES Algorithms &lt;a class="heading-anchor" href="#jwe-with-aes-algorithms" aria-label="Link to section: JWE with AES Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;AES (Advanced Encryption Standard) algorithm is still the de-facto standard of a symmetric algorithm that can be used with JWE. It involves using the same secret key to encrypt and decrypt data. The &lt;strong&gt;JWE token can be encrypted using&lt;/strong&gt; AES-GCM (Galois/Counter Mode), which provides authenticated encryption with associated data (AEAD). AES128 should be fine for &amp;ldquo;short-lived&amp;rdquo; tokens (&lt;a href="https://crypto.stackexchange.com/questions/76738/has-aes-128-been-fully-broken/76746#76746" title="Has AES-128 been fully broken?" class="ext"&gt;&amp;lt;34 million years&lt;/a&gt;).&lt;/p&gt;
&lt;h3 id="jwe-with-rsa-algorithms"&gt;JWE with RSA Algorithms &lt;a class="heading-anchor" href="#jwe-with-rsa-algorithms" aria-label="Link to section: JWE with RSA Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;RSA (Rivest-Shamir-Adleman) is an example of an asymmetric encryption algorithm supported by JWE. It involves generating a key pair where the public key encrypts data while the private key allows to decrypt it. Commonly used in JWE token contexts is RSA-OAEP (Optimal Asymmetric Encryption Padding).&lt;/p&gt;
&lt;h3 id="jwe-with-elliptic-curve-algorithms"&gt;JWE with Elliptic Curve Algorithms &lt;a class="heading-anchor" href="#jwe-with-elliptic-curve-algorithms" aria-label="Link to section: JWE with Elliptic Curve Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Elliptic Curve Cryptography (ECC) is another example of an asymmetric cryptography system supported by JWE. It involves generating a key pair based on elliptic curves rather than prime numbers. The JWE token can be encrypted using ECDH-ES (Elliptic Curve Diffie-Hellman Key Agreement with Elliptic Curve Cryptography) or ECDH-ES+A128KW (ECDH-ES with AES key wrap).&lt;/p&gt;
&lt;h3 id="authenticated-encryption-algorithms"&gt;Authenticated Encryption Algorithms &lt;a class="heading-anchor" href="#authenticated-encryption-algorithms" aria-label="Link to section: Authenticated Encryption Algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWE requires using authenticated encryption (AE) to ensure that the token is encrypted and integrity protected, supporting multiple AE algorithms. Two distinct classes exist, authenticated encryption with AES/CBC/HMAC/SHA and AES/GCM. The latter also supports Authenticated Encryption with Associated Data (AEAD), which provides confidentiality, integrity, and &lt;strong&gt;authenticity&lt;/strong&gt; of the encrypted message and additional authenticated data. It ensures that the message has not been tampered with during transmission.&lt;/p&gt;
&lt;h2 id="jwe-token-generation-process"&gt;JWE Token Generation Process &lt;a class="heading-anchor" href="#jwe-token-generation-process" aria-label="Link to section: JWE Token Generation Process"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="encryption-algorithm-choosing-the-right-one-for-your-needs"&gt;Encryption Algorithm: Choosing the Right One for Your Needs &lt;a class="heading-anchor" href="#encryption-algorithm-choosing-the-right-one-for-your-needs" aria-label="Link to section: Encryption Algorithm: Choosing the Right One for Your Needs"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Choosing a suitable algorithm is crucial when generating a JWE token. There are several options available, including AES-GCM, AES-CBC-HMAC-SHA2, and RSA-OAEP. Each algorithm has strengths and weaknesses, so choosing one that aligns with specific needs and requirements is essential.&lt;/p&gt;
&lt;h3 id="generating-keys-private-key-vs-secret-key"&gt;Generating Keys: Private Key vs Secret Key &lt;a class="heading-anchor" href="#generating-keys-private-key-vs-secret-key" aria-label="Link to section: Generating Keys: Private Key vs Secret Key"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The process of generating keys for JWE tokens differs depending on the form of encryption. Applying symmetric encryption requires generating a random secret key that both the sender and receiver can use to encrypt and decrypt data. Asymmetric encryption involves creating a public-private key pair.&lt;/p&gt;
&lt;h3 id="complete-jose-header"&gt;Complete JOSE Header &lt;a class="heading-anchor" href="#complete-jose-header" aria-label="Link to section: Complete JOSE Header"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The process of generating a JWE token is complex and involves several steps. The first step is the creation of a complete JOSE header that contains information about the encryption algorithm, &lt;strong&gt;key management&lt;/strong&gt; algorithm, key ID (kid), and other critical details. This header is essential because it provides the foundation for creating a secure and valid JWE token.&lt;/p&gt;
&lt;h3 id="encryption--serialization-creating-valid-jwe-tokens"&gt;Encryption &amp;amp; Serialization: Creating Valid JWE Tokens &lt;a class="heading-anchor" href="#encryption--serialization-creating-valid-jwe-tokens" aria-label="Link to section: Encryption &amp;amp; Serialization: Creating Valid JWE Tokens"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Once a suitable encryption algorithm is chosen and keys generated, it&amp;rsquo;s time to encrypt the payload involving an appropriate initialization vector (IV). After successfully calculating the ciphertext and the corresponding authentication tag, all individual parts are appropriately encoded and joined into their compact serialization format. As JWE deals with various cryptographic algorithms, is somewhat complex, and can become confusing, I recommend leveraging standard libraries to create encrypted and properly serialized JWT.&lt;/p&gt;
&lt;h2 id="jwe-token-decryption-process"&gt;JWE Token Decryption Process &lt;a class="heading-anchor" href="#jwe-token-decryption-process" aria-label="Link to section: JWE Token Decryption Process"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="decoding-the-jwe-compact-serialization"&gt;Decoding the JWE Compact Serialization &lt;a class="heading-anchor" href="#decoding-the-jwe-compact-serialization" aria-label="Link to section: Decoding the JWE Compact Serialization"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decrypting a JWE token starts with decoding the JWE compact serialization. The JWE compact serialization is a string comprising five parts separated by dots: the JWE header, the encrypted key, the initialization vector, the JWE ciphertext, and the authentication tag. It is the most commonly used type compared to the less popular JWE JSON serialization.&lt;/p&gt;
&lt;p&gt;The protected header contains information about the encryption and key management algorithms used to encrypt the payload. This information is required to decrypt the token. Once being (base64url) decoded, these claims are accessible for introspection.&lt;/p&gt;
&lt;h3 id="key-decryption"&gt;Key Decryption &lt;a class="heading-anchor" href="#key-decryption" aria-label="Link to section: Key Decryption"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Access to the private secret key initially used to encrypt is required to decrypt the JWE token. The &lt;strong&gt;decryption algorithm&lt;/strong&gt; referenced in the JOSE header defines the cryptographic procedure for the CEK. Only after decryption can it unlock the actual payload. In the case of public key cryptography, the associated private key must be available. Single-key encryption requires the corresponding secret shared between both parties to be present.&lt;/p&gt;
&lt;h3 id="payload-decryption"&gt;Payload Decryption &lt;a class="heading-anchor" href="#payload-decryption" aria-label="Link to section: Payload Decryption"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The IV value initializes the cipher instance with an appropriate mode like CBC, GCM, etc. The previously obtained JWE encrypted key (CEK) can decrypt the JWE ciphertext value, yielding a plain text (often a JSON object) after successful decryption. The integrity of the ciphertext and its additional authenticated data (AAD), in this case, the JWE header elements, is verified using the authentication tag value provided in the last part of the serialized JWE object.&lt;/p&gt;
&lt;h2 id="security-considerations"&gt;Security Considerations &lt;a class="heading-anchor" href="#security-considerations" aria-label="Link to section: Security Considerations"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="authentication-tag-and-additional-authenticated-data"&gt;Authentication Tag and Additional Authenticated Data &lt;a class="heading-anchor" href="#authentication-tag-and-additional-authenticated-data" aria-label="Link to section: Authentication Tag and Additional Authenticated Data"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One key feature that makes JWE tokens secure is using an authentication tag and additional authenticated data (AAD). The authentication tag is a small piece of data at the end of the encrypted JWT, which allows recipients to verify that the message has not been tampered with during transmission. AAD provides an extra layer of security by enabling the sender to include additional data in the encryption process that the recipient will verify.&lt;/p&gt;
&lt;p&gt;However, not all JWE tokens have an authentication tag or AAD. There are JWA encryption algorithms that do not use an authentication tag, in which case the value is supposed to be an empty octet sequence.&lt;/p&gt;
&lt;h3 id="leaky-headers"&gt;Leaky Headers &lt;a class="heading-anchor" href="#leaky-headers" aria-label="Link to section: Leaky Headers"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Another potential security concern when using JWE tokens is custom header elements. JWT headers include public claims by design that are integrity protected at max. Using customer header claims means they can potentially leak sensitive information if mismanaged. It&amp;rsquo;s crucial to handle custom header claims carefully and only include necessary information. If confidentiality is needed, custom fields belong in the payload. If there is a need for public hints, the header metadata is just fine.&lt;/p&gt;
&lt;h3 id="key-management-and-token-expiry"&gt;Key Management and Token Expiry &lt;a class="heading-anchor" href="#key-management-and-token-expiry" aria-label="Link to section: Key Management and Token Expiry"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Proper key management is essential for maintaining the security of JWE tokens. Identity providers must store the long-lived key management keys securely and rotate them regularly to prevent unauthorized access, as compromise might result in disclosing all contents protected with that key.&lt;/p&gt;
&lt;p&gt;Similarly, successful attacks on the CEK will strip all confidentiality properties from the corresponding token. Implementing token expiry to limit the amount of time token usage before it becomes invalid is necessary for all everyday use cases.&lt;/p&gt;
&lt;h3 id="weak-algorithms"&gt;Weak algorithms &lt;a class="heading-anchor" href="#weak-algorithms" aria-label="Link to section: Weak algorithms"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The JWE specification includes a limited range of supported encryption algorithms. And also, the officially supported ones rarely convince security experts: The ECDH-ES algorithm &lt;a href="https://web.archive.org/web/20201130103442/https://blogs.adobe.com/security/2017/03/critical-vulnerability-uncovered-in-json-encryption.html" title="Critical Vulnerability Uncovered in JSON Encryption" class="ext"&gt;allows an attacker to recover the private key&lt;/a&gt; if not adequately implemented. &lt;a href="https://rwc.iacr.org/2017/Slides/nguyen.quan.pdf" title="Practical Cryptanalysis of Json Web Token and Galois Counter Mode’s Implementations" class="ext"&gt;GCM is likely fragile&lt;/a&gt;, as Google researchers claim. And &lt;a href="https://archiv.infsec.ethz.ch/education/fs08/secsem/bleichenbacher98.pdf" title="Chosen Ciphertext Attacks Against Protocols Based on the RSA Encryption Standard PKCS #1" class="ext"&gt;RSA encryption with PKCS1v1.5&lt;/a&gt; padding is broken.&lt;/p&gt;
&lt;p&gt;When leveraging JWE, I recommend supporting short notice and timely transitioning to alternative encryption algorithms.&lt;/p&gt;
&lt;h2 id="comparison-of-jwe-and-other-json-security-standards"&gt;Comparison of JWE and Other JSON Security Standards &lt;a class="heading-anchor" href="#comparison-of-jwe-and-other-json-security-standards" aria-label="Link to section: Comparison of JWE and Other JSON Security Standards"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="jwe-vs-jwt"&gt;JWE vs JWT &lt;a class="heading-anchor" href="#jwe-vs-jwt" aria-label="Link to section: JWE vs JWT"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;While both JWE and JWT are part of the JSON Object Signing and Encryption (JOSE) security framework, they &lt;a href="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/" title="The Purpose of JWT: Stateless Authentication"&gt;serve different purposes&lt;/a&gt;. As mentioned, JWEs provide JSON data encryption, while unsecured JWTs (&lt;a href="https://datatracker.ietf.org/doc/html/rfc7519" title="JSON Web Token (JWT)" class="ext"&gt;RFC 7519&lt;/a&gt;) provide no particular security properties. In identity and access management (IAM) scenarios where a client sends a token to a resource server as proof of identity, JSON Web Tokens commonly appear in their signed JWS form.&lt;/p&gt;
&lt;h3 id="jwe-vs-jws-json-web-signature"&gt;JWE vs JWS (JSON Web Signature) &lt;a class="heading-anchor" href="#jwe-vs-jws-json-web-signature" aria-label="Link to section: JWE vs JWS (JSON Web Signature)"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWS is a JSON-based format for representing digital signatures (&lt;a href="https://datatracker.ietf.org/doc/html/rfc7515" title="JSON Web Signature (JWS)" class="ext"&gt;RFC 7515&lt;/a&gt;). JWS assures integrity and authentication, but it does not provide confidentiality. JWS tokens contain three elements: the JOSE header, payload, and signature. On the other hand, JSON Web Encryption (JWE) is a standard for encrypting JSON data. JWE tokens protect sensitive data by also providing confidentiality.&lt;/p&gt;
&lt;h3 id="jwe-vs-jwk-json-web-key"&gt;JWE vs JWK (JSON Web Key) &lt;a class="heading-anchor" href="#jwe-vs-jwk-json-web-key" aria-label="Link to section: JWE vs JWK (JSON Web Key)"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWK is another standard in the JOSE family (RFC 7517) that defines JSON format for cryptographic keys. A &lt;a href="https://www.janbrennenstuhl.eu/jwks/" title="Mastering JWKS: JSON Web Key Sets Explained"&gt;JWK Key Set (JWKS)&lt;/a&gt; can contain keys used for either encryption or signing, depending on their type. JWKs can be used with both JWS and JWE standards to provide cryptographic operations such as decryption or verification of digital signatures.&lt;/p&gt;
&lt;h2 id="practical-applications-of-jwe-tokens"&gt;Practical Applications of JWE Tokens &lt;a class="heading-anchor" href="#practical-applications-of-jwe-tokens" aria-label="Link to section: Practical Applications of JWE Tokens"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The nature of JWE tokens makes them ideal for enabling stateless information exchange in modern AAA processes or exotic &lt;a href="https://www.janbrennenstuhl.eu/single-use-jwt/" title="Single-Use JWT: The Power of Stateless One Time Token"&gt;one-time-token&lt;/a&gt; (OTT) scenarios. In this section, I will discuss some practical applications of JWEs.&lt;/p&gt;
&lt;h3 id="bearer-token-a-powerful-authentication-and-authorization-tool"&gt;Bearer Token: A Powerful Authentication and Authorization Tool &lt;a class="heading-anchor" href="#bearer-token-a-powerful-authentication-and-authorization-tool" aria-label="Link to section: Bearer Token: A Powerful Authentication and Authorization Tool"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWEs often appear as bearer tokens to authenticate and authorize access to protected resources. Bearer tokens are powerful tools because they allow users to access resources without repeatedly providing credentials. Instead, the bearer token serves as proof of identity and authorization.&lt;/p&gt;
&lt;h3 id="jwe-in-oauth2-and-oidc"&gt;JWE in OAuth2 and OIDC &lt;a class="heading-anchor" href="#jwe-in-oauth2-and-oidc" aria-label="Link to section: JWE in OAuth2 and OIDC"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://www.janbrennenstuhl.eu/topics/identity-authentication/" title="Delegated Authorization with OAuth 2.0"&gt;OAuth2&lt;/a&gt; and OpenID Connect (OIDC) are widely used authentication/ authorization protocols, often used for securing web APIs. They allow users to grant access to their resources without sharing their credentials with third-party applications. These protocols can use JWE Tokens to provide an additional layer of security by encrypting the access token. Enforcing clients to treat them as genuinely opaque tokens allows the token issuer (authorization server) to transport sensitive information like PII statelessly. At the same time, it also ensures that even if an attacker intercepts the token, they cannot read its contents without the &lt;strong&gt;decryption key&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="jwe-for-data-at-rest"&gt;JWE for Data at Rest &lt;a class="heading-anchor" href="#jwe-for-data-at-rest" aria-label="Link to section: JWE for Data at Rest"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Data at rest refers to data stored on disk or other storage media. This data is vulnerable to attacks if it is not adequately secured. Although not common, JWE tokens can encrypt sensitive data before storing it on disk, ensuring that even if an attacker gains access to the storage media, they cannot read its contents without the decryption key.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion &lt;a class="heading-anchor" href="#conclusion" aria-label="Link to section: Conclusion"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;JSON Web Encryption (JWE) is a powerful tool for ensuring the confidentiality and security of data transmissions. JWE tokens carry an encrypted payload between multiple parties. The secured data can only be read with the proper decryption key, even if intercepted. Authenticated encryption ensures that sensitive information remains confidential and tamper-proof, providing both senders and receivers peace of mind.&lt;/p&gt;
&lt;p&gt;The use cases for JWE tokens are varied and versatile. Use JWE in situations where confidential data exchange is necessary. When used as a bearer token within modern authorization and authentication frameworks like OpenID Connect (OIDC), JWE tokens transport sensitive information often as access tokens.&lt;/p&gt;
&lt;h2 id="frequently-asked-questions-faqs"&gt;Frequently Asked Questions (FAQs) &lt;a class="heading-anchor" href="#frequently-asked-questions-faqs" aria-label="Link to section: Frequently Asked Questions (FAQs)"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;What is the purpose of JWE?&lt;/b&gt; – JWE tokens securely transmit data between parties in a JSON format. They provide confidentiality, integrity, and authenticity to the transmitted data.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;How does JWE differ from JWT?&lt;/b&gt; JWE focuses on encrypting the payload, while plain JWT provides no additional security properties.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;What are standard encryption algorithms used in JWE?&lt;/b&gt; Some common encryption algorithms include RSA-OAEP, AES-GCM, and ECDH-ES.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Is it safe to use JWE tokens for sensitive data?&lt;/b&gt; Yes, as long as proper security measures are taken, such as using robust encryption algorithms and protecting the encryption and decryption keys.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Can JWE be used with other JSON security standards?&lt;/b&gt; JWE can be combined with other JOSE, such as JWS, to support even more complex scenarios with nested JWTs.&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>JWT vs OAuth 2.0: Understanding the Key Differences</title><link>https://www.janbrennenstuhl.eu/jwt-vs-oauth/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/jwt-vs-oauth/</guid><pubDate>Wed, 24 May 2023 00:00:00 +0200</pubDate><description>&lt;h2 id="tldr"&gt;TL;DR &lt;a class="heading-anchor" href="#tldr" aria-label="Link to section: TL;DR"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;OAuth 2.0&lt;/strong&gt; is an open standard that describes an authorization framework. It allows a third-party application to access a user’s resources without user credentials.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JWT&lt;/strong&gt; (JSON Web Tokens) is a standard that describes a JSON-based token format used to securely transfer information between two parties across an insecure environment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Combining OAuth 2.0 and JWT:&lt;/strong&gt; JWT can be integral to the &lt;a href="https://www.janbrennenstuhl.eu/topics/identity-authentication/" title="OAuth 2.0 in Action"&gt;OAuth 2.0 framework&lt;/a&gt; . In fact, JWT has often been used as the bearer token in OAuth 2.0 implementations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OpenID Connect (OIDC):&lt;/strong&gt; Modern authentication and authorization mechanisms leverage the OAuth 2.0 fundamentals with JWT tokens by following &lt;a href="https://www.janbrennenstuhl.eu/topics/identity-authentication/" title="OpenID Connect: Streamlining User Authentication &amp;amp; Identity"&gt;OpenID Connect&lt;/a&gt; protocols.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="introduction-to-jwt-vs-oauth-20"&gt;Introduction to JWT vs OAuth 2.0 &lt;a class="heading-anchor" href="#introduction-to-jwt-vs-oauth-20" aria-label="Link to section: Introduction to JWT vs OAuth 2.0"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Most people encounter OAuth2 and JSON Web Tokens (JWT) in the context of authentication and authorization for web applications. While both concepts nowadays get used together, comparing &lt;strong&gt;OAuth2 vs JWT&lt;/strong&gt; is like comparing apples to kittens.&lt;/p&gt;
&lt;p&gt;10 years after the last update to the OAuth 2.0 Authorization Framework standard (&lt;a href="https://datatracker.ietf.org/doc/html/rfc6749" title="The OAuth 2.0 Authorization Framework" class="ext"&gt;RFC 6749&lt;/a&gt;) and over eight years after JSON Web Tokens got unleashed to the world, there is still a lot of confusion around these two widespread open standards among software engineers and security engineering novices. In this article, I will explain why comparisons like &lt;em&gt;&amp;ldquo;JWT vs OAuth 2.0&amp;rdquo;&lt;/em&gt; or questions like &lt;em&gt;&amp;ldquo;OAuth vs JWT which is best&amp;rdquo;&lt;/em&gt; make little sense.&lt;/p&gt;
&lt;h3 id="what-is-oauth2"&gt;What is OAuth2? &lt;a class="heading-anchor" href="#what-is-oauth2" aria-label="Link to section: What is OAuth2?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;OAuth2 is an open standard describing an authorization framework that allows a third-party application to access a user&amp;rsquo;s resources without giving them the user&amp;rsquo;s credentials. The keywords here are &lt;strong&gt;authorization protocol&lt;/strong&gt; and &lt;strong&gt;delegated access&lt;/strong&gt;. It does this by providing the concept of &lt;strong&gt;access tokens&lt;/strong&gt; that can be used by a client application to access protected resources (like an API) exposed by a resource server on behalf of the resource owner (a user). These opaque tokens are issued by an authorization server after the user grants permission to the third-party application.&lt;/p&gt;
&lt;h3 id="what-is-jwt"&gt;What is JWT? &lt;a class="heading-anchor" href="#what-is-jwt" aria-label="Link to section: What is JWT?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;On the other hand, the JWT standards describe a &lt;strong&gt;JSON-based token format&lt;/strong&gt; and how it can &lt;strong&gt;securely transfer information&lt;/strong&gt; between two parties across an insecure environment (like HTTP or HTTPS). Their multipart JSON format structure allows JWTs to carry &lt;strong&gt;custom claims&lt;/strong&gt; as payloads and additional cryptographic accessories. They are self-contained and usually occur in their JSON Web Signature form, signed with a secret key or public/private key pair. I wrote about &amp;ldquo;&lt;a href="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/" title="The Purpose of JWT: Stateless Authentication"&gt;The Purpose of JWT&lt;/a&gt;&amp;rdquo; a few years ago.&lt;/p&gt;
&lt;h2 id="when-to-use-oauth-20-vs-when-to-use-jwt"&gt;When to Use OAuth 2.0 vs When to Use JWT &lt;a class="heading-anchor" href="#when-to-use-oauth-20-vs-when-to-use-jwt" aria-label="Link to section: When to Use OAuth 2.0 vs When to Use JWT"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Use OAuth 2.0 with HTTPS when authorizing third-party access.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;OAuth 2.0 is a protocol that allows users to grant limited access to resources on one site, such as Facebook, to another location or application, without sharing their credentials or giving them complete control over their account. It provides a secure way of authorizing third-party access to your resources by using access tokens in the form of bearer tokens.&lt;/p&gt;
&lt;p&gt;After the resource owner grants permission for the client application to access their resources using OAuth2, the responsible authorization server issues the proof of authentication. These tokens are then sent with each request made by the client application to the resource server, verifying them and granting or denying access based on the services requested and claims provided.&lt;/p&gt;
&lt;p&gt;Bearer tokens are a type of access token that can be used by arbitrary parties in possession of them to gain access to the protected resources they represent. They do not contain any cryptographic proof of authenticity other than possession itself, so they must be kept secret and transmitted securely.&lt;/p&gt;
&lt;p&gt;OAuth 2.0 is an excellent choice when you must permit controlled interaction between third-party applications or services managed by different entities or individuals with your system without compromising sensitive information or risking unauthorized access. This is possible through various security mechanisms such as opaque tokens, bearer tokens, identity servers, and refresh tokens.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Use JWT when securely transmitting information between parties in a stateless form.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;JSON Web Tokens are basically use cases agnostic and can generally be used for securely transmitting information between parties in stateless form. It provides a simple way of representing claims (e.g., user identity, permissions) as JSON objects that can be signed and/or encrypted using industry-standard algorithms.&lt;/p&gt;
&lt;p&gt;JWTs are often used for transporting proof of authenticity across multiple domains or applications without storing session data on the server side, significantly contributing to performance. They are also helpful for implementing single sign-on (SSO) functionality, where a user logs in once and gains access to multiple applications or services without claiming their identity again. And this is basically where OAuth 2.0 and JWT touch on each other.&lt;/p&gt;
&lt;h2 id="combining-oauth-20-and-jwt-how-to-use-them-together"&gt;Combining OAuth 2.0 and JWT: How to Use Them Together &lt;a class="heading-anchor" href="#combining-oauth-20-and-jwt-how-to-use-them-together" aria-label="Link to section: Combining OAuth 2.0 and JWT: How to Use Them Together"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The apparent overlap between JWT and OAuth 2.0 springs from a misunderstanding of their distinct roles in digital communications. JWT can be an integral part of the OAuth 2.0 framework. In fact, JWT often serves as the bearer token in OAuth 2.0 implementations, becoming the vehicle by which authorization information is conveyed.&lt;/p&gt;
&lt;h3 id="jwt-as-bearer-token"&gt;JWT as Bearer Token &lt;a class="heading-anchor" href="#jwt-as-bearer-token" aria-label="Link to section: JWT as Bearer Token"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWTs can be used by an app as &lt;a href="https://www.rfc-editor.org/rfc/rfc6750" title="The OAuth 2.0 Authorization Framework: Bearer Token Usage (RFC)" class="ext"&gt;OAuth2 bearer tokens&lt;/a&gt; to enable stateless authentication and local authorization by a resource server to grant access to a protected resource.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bearer tokens are a type of token&lt;/strong&gt; that is widely used in authentication protocols like OAuth 2.0. They are used to grant access to protected resources, and they come in two types: access tokens and refresh tokens. While access tokens are short-lived, refresh tokens can be renewed, allowing users to maintain their session for an extended period.&lt;/p&gt;
&lt;p&gt;JWTs are often used in combination with OAuth 2.0 as a means of transmitting state and user data (claims) between the authorization server and the resource server. They can also be used for client-side authentication, where a JWT is issued by an &lt;strong&gt;identity provider&lt;/strong&gt; after successful login and then sent to the client application, which uses it to authenticate itself on behalf of the actual user with other services.&lt;/p&gt;
&lt;h3 id="advantages-of-using-jwt-with-oauth-20"&gt;Advantages of Using JWT with OAuth 2.0 &lt;a class="heading-anchor" href="#advantages-of-using-jwt-with-oauth-20" aria-label="Link to section: Advantages of Using JWT with OAuth 2.0"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One significant advantage of using JWT with OAuth 2.0 is eliminating the need for multiple tokens for different app purposes. One token can contain all the necessary information for authentication and authorization purposes.&lt;/p&gt;
&lt;p&gt;Another benefit can be improved performance through stateless authentication. Since &lt;a href="https://auth0.com/learn/json-web-tokens" title="Get Started with JSON Web Tokens" class="ext"&gt;JWT tokens&lt;/a&gt; are self-contained and signed with a secret key or public/private key pair, there is less reliance on external servers for validation and introspection. JWT is very suitable for stateless applications as they relieve the server side from the need to store any particular session data between requests from the clients.&lt;/p&gt;
&lt;p&gt;Instead, all necessary data is carried around within each JWT token itself, making it easy for clients and servers alike to verify its authenticity without looking up anything outside of it. This reduces the number of database lookups required during token verification, leading to faster response times, increased reliability due to fewer bottlenecks, and a potentially cleaner architecture.&lt;/p&gt;
&lt;h2 id="which-bearer-token-format-is-right-for-you"&gt;Which Bearer Token Format is Right for You? &lt;a class="heading-anchor" href="#which-bearer-token-format-is-right-for-you" aria-label="Link to section: Which Bearer Token Format is Right for You?"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Choosing the suitable OAuth 2.0 access token format can be a daunting task. Both &lt;strong&gt;stateful tokens&lt;/strong&gt; and &lt;strong&gt;stateless JWT&lt;/strong&gt; have their strengths and weaknesses. It ultimately depends on specific use cases, requirements, and infrastructural preconditions.&lt;/p&gt;
&lt;p&gt;Modern authentication and authorization mechanisms leverage the &lt;strong&gt;OAuth 2.0 fundamentals&lt;/strong&gt; together with JWT tokens by following &lt;strong&gt;OpenID Connect&lt;/strong&gt; (OIDC) protocols, which is an extension on top of OAuth 2.0 that introduces the concept of an identity provider (aka identity server) and uses JWTs instead of opaque tokens.&lt;/p&gt;
&lt;h2 id="jwt--oauth2--openid-connect-oidc"&gt;JWT + OAuth2 = OpenID Connect (OIDC) &lt;a class="heading-anchor" href="#jwt--oauth2--openid-connect-oidc" aria-label="Link to section: JWT &amp;#43; OAuth2 = OpenID Connect (OIDC)"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Big tech organizations such as Google, Facebook, and Microsoft are OIDC power users:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developers.google.com/identity/openid-connect/openid-connect" title="Google&amp;#39;s OAuth 2.0 APIs" class="ext"&gt;Google Identity uses OIDC&lt;/a&gt; and its identity token to transport user details.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-oidc" title="OIDC Tokens in Facebook Login" class="ext"&gt;Facebook also uses OIDC with JWTs&lt;/a&gt; in its Graph API authentication mechanism.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/azure/active-directory/develop/access-tokens" title="Microsoft identity platform access tokens" class="ext"&gt;Microsoft&amp;rsquo;s Azure Active Directory&lt;/a&gt; (AAD) also leverages the JWT token to implement OIDC.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Whether you choose classic OAuth 2.0 with opaque, non-JWT tokens, OAuth 2.0 with JWT bearer token, or OIDC, make sure you understand how these frameworks work under the hood, embrace their core concepts and get your implementation externally certified to ensure maximum security for your users&amp;rsquo; data while also providing seamless access to your application&amp;rsquo;s features and resources. The better way, however, is to outsource the authentication to Google, Microsoft, Okta, or someone else and let them deal with all the nifty little details in these specifications.&lt;/p&gt;
&lt;h2 id="conclusion-choosing-between-jwt-and-oauth2"&gt;Conclusion: Choosing Between JWT and OAuth2 &lt;a class="heading-anchor" href="#conclusion-choosing-between-jwt-and-oauth2" aria-label="Link to section: Conclusion: Choosing Between JWT and OAuth2"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Combining OAuth 2.0 and JWT can give you the best of both worlds. OpenID Connect is built on both open standards, embracing OAuth 2.0 concepts to &lt;strong&gt;delegate access with external services&lt;/strong&gt; while using JWT as a bearer to &lt;strong&gt;transport claims from the authorization server&lt;/strong&gt; to resource servers for stateless authorization.&lt;/p&gt;
&lt;p&gt;JWTs allow client-side introspection of ID tokens, enable resource server serving APIs to verify access tokens locally, and even be used as refresh tokens. Although slightly controversial, JSON web tokens are versatile and can be used &lt;a href="https://www.janbrennenstuhl.eu/single-use-jwt/" title="Single-Use JWT: The Power of Stateless One Time Token"&gt;as one-time token&lt;/a&gt; (OTT).&lt;/p&gt;</description></item><item><title>The Purpose of JWT: Stateless Authentication</title><link>https://www.janbrennenstuhl.eu/jwt-stateless-authentication/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/jwt-stateless-authentication/</guid><pubDate>Fri, 28 Apr 2017 00:00:00 +0200</pubDate><description>&lt;p&gt;The demand for reliable and effective authentication methods has increased in the modern, distributed, and fast-paced digital world. Stateless authentication solutions are being used by developers and identity providers to deliver secure and scalable single sign-on solutions. JSON Web Token (JWT) are the new fancy kids around the block when it comes to transporting proofs of identity within an untrusted environment like the web.&lt;/p&gt;
&lt;h2 id="stateless-sessions-thanks-to-json-web-token"&gt;Stateless Sessions Thanks to JSON Web Token &lt;a class="heading-anchor" href="#stateless-sessions-thanks-to-json-web-token" aria-label="Link to section: Stateless Sessions Thanks to JSON Web Token"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While there are many articles that describe specific aspects, &lt;a href="https://dev.to/neilmadden/7-best-practices-for-json-web-tokens" title="7 Best Practices for JSON Web Tokens" class="ext"&gt;best practices&lt;/a&gt; or single use-cases of JWTs, the bigger picture and the actual problem definition is often missing. With JWTs gaining in popularity, that missing knowledge of the fundamental ideas of JSON &lt;a href="https://www.janbrennenstuhl.eu/single-use-jwt/" title="Single-Use JWT: The Power of Stateless One Time Token"&gt;Web Token&lt;/a&gt; leads to questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/21871029/logout-invalidate-a-jwt" class="ext"&gt;How to invalidate a JWT?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/26739167/jwt-json-web-token-automatic-prolongation-of-expiration" class="ext"&gt;How to prolongate a JWTs expiration date?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/41865108/why-should-i-use-jwt-not-simple-hashed-token" class="ext"&gt;Why should I use JWT, not simple hashed token?&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this article, I describe the true purpose of JWT tokens – &lt;strong&gt;Getting rid of stateful authentication!&lt;/strong&gt; – and compare classical, stateful sessions with modern, stateless authentication. Let&amp;rsquo;s start now!&lt;/p&gt;
&lt;h2 id="what-is-jwt"&gt;What is JWT? &lt;a class="heading-anchor" href="#what-is-jwt" aria-label="Link to section: What is JWT?"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="definition"&gt;Definition &lt;a class="heading-anchor" href="#definition" aria-label="Link to section: Definition"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JSON Web Tokens are a self-contained, URL-safe way of conveying data, unique identifier and claims between two parties in an untrusted environment. They are frequently employed in online applications for the purposes of authentication and authorization of users, transporting proof of authenticity between an identity provider and relying parties.&lt;/p&gt;
&lt;h3 id="components-of-jwt-tokens"&gt;Components of JWT tokens &lt;a class="heading-anchor" href="#components-of-jwt-tokens" aria-label="Link to section: Components of JWT tokens"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWTs carry claims that are encoded as JSON object as the payload of a JSON Web Signature (JWS) structure or of a [JSON Web Encryption (JWE)]
(https://www.janbrennenstuhl.eu/jwe/ &amp;ldquo;JWE Token: A Comprehensive Guide&amp;rdquo;) structure, allowing the claims to be encrypted and/or digitally signed in order to protect their integrity. A signed JWT is made up of three components: a header, a payload, and a signature. The header contains metadata, like the type of token and signing algorithm. The claims are stored in the payload and hold details about the particular user and other necessary data. Finally, the signature is used to affirm the token&amp;rsquo;s integrity.&lt;/p&gt;
&lt;p&gt;&lt;figure class="figure"&gt;&lt;picture&gt;&lt;source type="image/avif" srcset="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/jwt_hu_3ec2a5379b49976b.avif 480w, https://www.janbrennenstuhl.eu/jwt-stateless-authentication/jwt_hu_2f84c7cb00261ea9.avif 600w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)"&gt;&lt;img src="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/jwt_hu_9780d59cfa850089.webp" srcset="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/jwt_hu_a84f55b55744eafd.webp 480w, https://www.janbrennenstuhl.eu/jwt-stateless-authentication/jwt_hu_9780d59cfa850089.webp 600w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)" width="600" height="321" alt="A JSON Web Token" loading="lazy" decoding="async"&gt;&lt;/picture&gt;&lt;figcaption&gt;Parts of a signed JWT&lt;/figcaption&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;h2 id="the-need-for-stateless-authentication"&gt;The Need for Stateless Authentication &lt;a class="heading-anchor" href="#the-need-for-stateless-authentication" aria-label="Link to section: The Need for Stateless Authentication"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Stateless authentication is essential for modern web applications because it eliminates the need for the central authentication server to retain session id and session information. Ideally, this minimizes backend load and enables more effective application scaling. By removing bottlenecks, a decentralized authorization approach is said to simplify system landscape.&lt;/p&gt;
&lt;h3 id="stateful-authentication"&gt;Stateful Authentication &lt;a class="heading-anchor" href="#stateful-authentication" aria-label="Link to section: Stateful Authentication"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In the old days of the web, authentication was a purely stateful affair. With a centralized overlord entity being responsible for tokens, the world is fairly simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tokens are issued and stored as stateful session data in a single service for future checking and revocation.&lt;/li&gt;
&lt;li&gt;Clients and resource servers know a single point of truth for token verification and information gathering.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This works rather well in a world of integrated systems, where servers render frontends and dependencies exist on package-level rather than distributed applications.&lt;/p&gt;
&lt;p&gt;In a world where applications are composed by a zoo of microservices however, this stateful authentication approach comes with a couple of serious drawbacks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No service can operate without a synchronous dependency towards the central token store.&lt;/li&gt;
&lt;li&gt;The token overlord, storing session data, becomes an infrastructural bottleneck and a single point of failure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both facts oppose the fundamental ideas of a stateless services landscape. Stateful authentication introduces not just another dependency for all your single-purpose services (network latency!) but also makes them heavily rely on access to it. Without the full session information and data database being available, authorization decisions can&amp;rsquo;t be taken anymore and everything is doomed.&lt;/p&gt;
&lt;h3 id="stateless-authentication"&gt;Stateless Authentication &lt;a class="heading-anchor" href="#stateless-authentication" aria-label="Link to section: Stateless Authentication"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Stateless token-based authentication describes a system or process, that enables its components to decentrally verify and introspect tokens. This ability to delegate session token verification allows to (partially) get rid of the direct coupling to a central session data database and in that way enables state transfer for authentication proves. The benefits are clear:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Less latency through service-local, decentralized token verification and authorization decisions.&lt;/li&gt;
&lt;li&gt;Increased resilience through less network overhead.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Stateless authentication is also able to absolve you from the need to keep track of issued tokens and for that reason removes state (and hence reduces storage) dependencies from your system. The antiquated, heavy-weighted token overlord solution converges to &lt;a href="https://auth0.com/blog/stateless-auth-for-stateful-minds/" title="Auth0: Stateless Sessions for Stateful Minds" class="ext"&gt;to yet another microservice&lt;/a&gt; being mainly responsible for authentication and token issuing.&lt;/p&gt;
&lt;h3 id="stateful-vs-stateless"&gt;Stateful vs. Stateless &lt;a class="heading-anchor" href="#stateful-vs-stateless" aria-label="Link to section: Stateful vs. Stateless"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Today, an authentication system has to support stateless architecture service landscapes mainly characterized by fleets of thousands of services and millions of actors. Central bottlenecks by design become an operational burden. However there are no silver bullets, JWTs ain’t Swiss army knifes and also stateful authentication has its righteous place.&lt;/p&gt;
&lt;p&gt;If you really need a central authentication system to persist session data or if you simply don&amp;rsquo;t trust people &lt;a href="https://www.chosenplaintext.ca/2015/03/31/jwt-algorithm-confusion.html" title="Critical vulnerabilities in JSON Web Token libraries" class="ext"&gt;or library source code&lt;/a&gt; to correctly verify session information in your JWTs, a stateful overlord approach is still the way to go and there is nothing wrong about it.&lt;/p&gt;
&lt;p&gt;&lt;figure class="figure"&gt;&lt;picture&gt;&lt;source type="image/avif" srcset="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/stateful-vs-stateless_hu_5d638925703258d0.avif 480w, https://www.janbrennenstuhl.eu/jwt-stateless-authentication/stateful-vs-stateless_hu_3ea23d89eb8c3197.avif 600w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)"&gt;&lt;img src="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/stateful-vs-stateless_hu_bbe3df90500d504.webp" srcset="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/stateful-vs-stateless_hu_c510f3780ae8d2b8.webp 480w, https://www.janbrennenstuhl.eu/jwt-stateless-authentication/stateful-vs-stateless_hu_bbe3df90500d504.webp 600w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)" width="600" height="356" alt="Google Trends on Stateful vs Stateless" loading="lazy" decoding="async"&gt;&lt;/picture&gt;&lt;figcaption&gt;The World becomes Stateless - Google Trends&lt;/figcaption&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;h2 id="how-jwt-enables-stateless-authentication"&gt;How JWT Enables Stateless Authentication &lt;a class="heading-anchor" href="#how-jwt-enables-stateless-authentication" aria-label="Link to section: How JWT Enables Stateless Authentication"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="token-based-authentication"&gt;Token-based authentication &lt;a class="heading-anchor" href="#token-based-authentication" aria-label="Link to section: Token-based authentication"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Stateless authentication get enabled through the use of stateless tokens (JWT). The authentication server generates these token, which are self-contained and carry all necessary details about the session and the user. In order to avoid the backend from having to keep track of any session ID and session data, the client stores this token and includes it in subsequent requests.&lt;/p&gt;
&lt;h3 id="json-web-signature-jws"&gt;JSON Web Signature (JWS) &lt;a class="heading-anchor" href="#json-web-signature-jws" aria-label="Link to section: JSON Web Signature (JWS)"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To guarantee the authenticity and integrity of the token, JWT session token must always use JWS. Because the private key material used to create this digital signature is only known to the server, it prevents unauthorized modification of the token.&lt;/p&gt;
&lt;h2 id="pros-and-cons-of-stateless-authentication-with-jwt"&gt;Pros and Cons of Stateless Authentication with JWT &lt;a class="heading-anchor" href="#pros-and-cons-of-stateless-authentication-with-jwt" aria-label="Link to section: Pros and Cons of Stateless Authentication with JWT"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="advantages"&gt;Advantages &lt;a class="heading-anchor" href="#advantages" aria-label="Link to section: Advantages"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Scalability: Stateless authentication removes architectural bottlenecks, can reduce server load and allows for easier horizontal scaling.&lt;/li&gt;
&lt;li&gt;Performance: Less server overhead means faster response times.&lt;/li&gt;
&lt;li&gt;Simplified architecture: No need to manage sessions stored server-side.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="disadvantages"&gt;Disadvantages &lt;a class="heading-anchor" href="#disadvantages" aria-label="Link to section: Disadvantages"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Token size: Due to their metadata overhead, JWTs are generally larger than traditional session cookies, increasing bandwidth usage.&lt;/li&gt;
&lt;li&gt;Token theft: If a token is stolen, it can be used until it expires, posing a security risk.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="understanding-jwt-workflow"&gt;Understanding JWT Workflow &lt;a class="heading-anchor" href="#understanding-jwt-workflow" aria-label="Link to section: Understanding JWT Workflow"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One popular way to achieve stateless authentication is defined in &lt;a href="https://tools.ietf.org/html/rfc7523" title="JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants" class="ext"&gt;RFC 7523&lt;/a&gt; and leverages the OAuth 2.0 Authorization Framework (&lt;a href="https://tools.ietf.org/html/rfc6749" title="The OAuth 2.0 Authorization Framework" class="ext"&gt;RFC 6749&lt;/a&gt;) by combining it with signed JWT (&lt;a href="https://tools.ietf.org/html/rfc7519" title="JSON Web Token (JWT)" class="ext"&gt;RFC 7519&lt;/a&gt;, &lt;a href="https://tools.ietf.org/html/rfc7515" title="JSON Web Signature (JWS)" class="ext"&gt;RFC 7515&lt;/a&gt;). Instead of storing the token-to-principal relationship in a database in a stateful manner, cryptographic signatures allow decentralized clients to securely store and grant access, without need to retrieve data from a central system for every inbound request.&lt;/p&gt;
&lt;p&gt;Stateless tokens are also a key component of OpenID Connect (OIDC), an identity layer solution built on top of the OAuth 2.0 protocol. OpenID Connect specifies industry-standard flows that allow clients to access basic profile information about the end-user in an interoperable way and to confirm the end-user&amp;rsquo;s identity based on the authentication carried out by an authorization server.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Using a JWT as a bearer for authorization, you can statelessly verify if the user is authenticated by simply checking if the expiration in the payload hasn&amp;rsquo;t expired and if the signature is valid.”
— &lt;cite&gt;&lt;a href="http://jonatan.nilsson.is/stateless-tokens-with-jwt/" title="Stateless tokens with JWT"&gt;Jonatan Nilsson&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;With tokens not being opaque but locally introspectable, clients can retrieve addition information about the corresponding identity directly from the token without the need of calling another remote API. The following is a simplified, non-normative lifecycle example of a stateless JWT.&lt;/p&gt;
&lt;h3 id="token-creation"&gt;Token Creation &lt;a class="heading-anchor" href="#token-creation" aria-label="Link to section: Token Creation"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;The user submits their credentials (e.g., username and password) to the authentication system.&lt;/li&gt;
&lt;li&gt;The server authenticates the actor by verifying the credentials and generates a JWT containing user-related claims.&lt;/li&gt;
&lt;li&gt;The server signs the token using a private key and returns it to the client side.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="token-verification"&gt;Token Verification &lt;a class="heading-anchor" href="#token-verification" aria-label="Link to section: Token Verification"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;The client includes the JWT as bearer token within the Authorization header in subsequent requests.&lt;/li&gt;
&lt;li&gt;The resource server verifies the token&amp;rsquo;s signature and validates the claims.&lt;/li&gt;
&lt;li&gt;If the token is valid and authorization successful, the resource server processes the request and returns the appropriate response.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="token-expiration"&gt;Token Expiration &lt;a class="heading-anchor" href="#token-expiration" aria-label="Link to section: Token Expiration"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;JWTs typically include an expiration time to limit their validity.&lt;/li&gt;
&lt;li&gt;Once the token expires, the client must request a new token by re-authenticating with the authorization server.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="use-cases-for-jwt"&gt;Use Cases for JWT &lt;a class="heading-anchor" href="#use-cases-for-jwt" aria-label="Link to section: Use Cases for JWT"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Authorization: Securely authorize actors based on user claims like roles and permissions without relying on server-side sessions.&lt;/li&gt;
&lt;li&gt;Information Exchange: Securely transmit information between parties in untrusted environments.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="security-concerns--considerations"&gt;Security Concerns &amp;amp; Considerations &lt;a class="heading-anchor" href="#security-concerns--considerations" aria-label="Link to section: Security Concerns &amp;amp; Considerations"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Token storage: Store tokens in a secure location on the client side, such HttpOnly cookies or other safe places.&lt;/li&gt;
&lt;li&gt;Transport security: Use HTTPS to protect tokens from being intercepted during transmission.&lt;/li&gt;
&lt;li&gt;Token revocation: By design, JWTs are not remotely revokable. To revoke tokens in case of token theft or user logout requires, a stateful mechanism to be in place – which sort of &lt;a href="https://web.archive.org/web/20241106205450/https://www.dinochiesa.net/?p=1388" title="Revoking or Blacklisting JWT" class="ext"&gt;counters the stateless approach&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Token update: There is no way of altering an issued JWT. Prolongating its expiration date for example is not possible.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="best-practices-for-jwt-implementation"&gt;Best Practices for JWT Implementation &lt;a class="heading-anchor" href="#best-practices-for-jwt-implementation" aria-label="Link to section: Best Practices for JWT Implementation"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use short expiration times&lt;/strong&gt;: Limit the window of opportunity for token theft.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rotate private keys&lt;/strong&gt;: Regularly update the signing key to mitigate risks associated with key compromise.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate tokens&lt;/strong&gt;: Before processing requests, always check the tokens validity on the authorizing backend.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="conclusion"&gt;Conclusion &lt;a class="heading-anchor" href="#conclusion" aria-label="Link to section: Conclusion"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In modern web applications, JWTs offer a reliable and adaptable stateless authentication method. Developers may create secure, scalable, and effective apps that satisfy the requirements of today&amp;rsquo;s digital ecosystem by utilizing their advantages and following best practices.&lt;/p&gt;
&lt;h2 id="faqs"&gt;FAQs &lt;a class="heading-anchor" href="#faqs" aria-label="Link to section: FAQs"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="what-is-the-main-purpose-of-using-jwt-in-stateless-authentication"&gt;What is the main purpose of using JWT in stateless authentication? &lt;a class="heading-anchor" href="#what-is-the-main-purpose-of-using-jwt-in-stateless-authentication" aria-label="Link to section: What is the main purpose of using JWT in stateless authentication?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The major goal is enhanced scalability and performance by securely authorizing users without relying on server-side sessions.&lt;/p&gt;
&lt;h3 id="how-can-i-prevent-jwt-theft"&gt;How can I prevent JWT theft? &lt;a class="heading-anchor" href="#how-can-i-prevent-jwt-theft" aria-label="Link to section: How can I prevent JWT theft?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Tokens must be stored securely on the client side and transmitted encrypted using HTTPS. If stateful revocation procedures are not implemented, tokens should have a short expiration time.&lt;/p&gt;
&lt;h3 id="can-i-use-jwt-for-authorization-as-well-as-authentication"&gt;Can I use JWT for authorization as well as authentication? &lt;a class="heading-anchor" href="#can-i-use-jwt-for-authorization-as-well-as-authentication" aria-label="Link to section: Can I use JWT for authorization as well as authentication?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JWTs can function as bearer tokens. Authorization decisions can be made either purely on token validity or take into consideration claims carried by the token.&lt;/p&gt;
&lt;h3 id="do-i-need-to-store-jwt-session-data-in-a-database"&gt;Do I need to store JWT session data in a database? &lt;a class="heading-anchor" href="#do-i-need-to-store-jwt-session-data-in-a-database" aria-label="Link to section: Do I need to store JWT session data in a database?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;No, JWTs are self-contained and do not require server-side storage. Actually that would be an anti-pattern.&lt;/p&gt;
&lt;h3 id="how-do-i-handle-jwt-expiration"&gt;How do I handle JWT expiration? &lt;a class="heading-anchor" href="#how-do-i-handle-jwt-expiration" aria-label="Link to section: How do I handle JWT expiration?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Implement a mechanism for clients to request a new token when the existing one expires. OAuth 2.0 for example distinguishes between access token and refresh token and defines protocols on how to continuously refresh tokens without need to actively re-authenticate the user.&lt;/p&gt;</description></item><item><title>Single-Use JWT: Unlocking the Power of Stateless One Time Token</title><link>https://www.janbrennenstuhl.eu/single-use-jwt/</link><guid isPermaLink="true">https://www.janbrennenstuhl.eu/single-use-jwt/</guid><pubDate>Fri, 30 Dec 2016 00:00:00 +0100</pubDate><description>&lt;h2 id="understanding-json-web-tokens"&gt;Understanding JSON Web Tokens &lt;a class="heading-anchor" href="#understanding-json-web-tokens" aria-label="Link to section: Understanding JSON Web Tokens"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Welcome to the future of authentication! In a world where digital security is paramount, the need for robust and reliable authentication methods has never been more crucial. &lt;a href="https://www.janbrennenstuhl.eu/topics/tokens-jwt/" title="More about JWTs"&gt;JSON Web Tokens&lt;/a&gt; are the new and fancy de-facto standard in the web. JWTs are the tool of choice when it comes to authentication in an stateless environment. That has two direct implications:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You should definitely go and get some proper introduction on &lt;a href="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/" title="Stateful vs Stateless Authentication"&gt;the purpose of JSON Web Tokens&lt;/a&gt; and some insights on how they work. It&amp;rsquo;s worth to know as JWTs are widely used for e.g. authentication against REST-APIs.&lt;/li&gt;
&lt;li&gt;As JWTs are still comparably fresh, hot and simply fancy in contrast to traditional bearer token, some people think they are some kind of holy grail and can be used to solve every possible use-case … and guess what: They are not!&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this article, I will dive into the benefits of single-use JWTs, how they work, and best practices for implementing them in your application. Get ready to unlock the full potential of stateless authentication with single-use JWTs!&lt;/p&gt;
&lt;h3 id="what-are-json-web-tokens"&gt;What are JSON Web Tokens? &lt;a class="heading-anchor" href="#what-are-json-web-tokens" aria-label="Link to section: What are JSON Web Tokens?"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;JSON Web Tokens are an industry standard method for exchanging claims securely between two parties. They are defined as an open standard in &lt;a href="https://datatracker.ietf.org/doc/html/rfc7519" class="ext"&gt;RFC 7519&lt;/a&gt; but only become truly powerful in combination with &lt;a href="https://datatracker.ietf.org/doc/html/rfc7515" class="ext"&gt;RFC 7515&lt;/a&gt;, which describes JSON Web Signatures (JWS). In combination, the two standards allow the creation of a cryptographically signed json object that carries state and can get verified, which basically means that you have means to ensure that a JWT is valid and created by someone you have faith in before you trust it.&lt;/p&gt;
&lt;p&gt;Being self-contained, stateless and signed, JWT tokens allow you to leverage so-called &lt;a href="https://www.janbrennenstuhl.eu/jwt-stateless-authentication/" title="Stateless Authentication, the purpose of JWT"&gt;stateless authentication&lt;/a&gt;, mostly in form of bearer tokens within an authorization header (access token), issued by an authorization server in context of OAuth 2.0 or OpenID Connect (OIDC).&lt;/p&gt;
&lt;p&gt;But there are further information exchange use cases for standardised, signed tokens that can carry state between some other client side and a server or two servers. One example, I saw being discussed in the open wild and also within companies, is to use a JWT token as one-time token (OTT).&lt;/p&gt;
&lt;h3 id="the-power-of-json-web-token"&gt;The Power of JSON Web Token &lt;a class="heading-anchor" href="#the-power-of-json-web-token" aria-label="Link to section: The Power of JSON Web Token"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;While not being a Swiss army knife, the power of JSON Web Tokens lies in their ability to unlock a multitude of benefits especially for authorization servers, identity provider servers and system reliability. From my point of view, the key features that make JWTs a force to be reckoned with in the world of digital security, is their support of stateless architecture. Unlike traditional session-based authentication mechanisms, JWTs are stateless by design. This means that servers do not necessarily need to store any session-related information in their database, resulting in improved scalability and reduced server-side complexity.&lt;/p&gt;
&lt;p&gt;Another side-effect of the general hype around these stateless tokens, is their interoperability. JWTs have gained widespread adoption and support across different programming languages, frameworks, and platforms. Every niche has their own JWT library. This interoperability ensures seamless integration into existing systems and promotes interoperability between various components of a distributed application. JWT token basically standardised the data format of access tokens or session token.&lt;/p&gt;
&lt;h2 id="what-is-a-one-time-use-token"&gt;What Is a One Time Use Token? &lt;a class="heading-anchor" href="#what-is-a-one-time-use-token" aria-label="Link to section: What Is a One Time Use Token?"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A One Time Use Token, also known as a single-use token, is a unique and disposable identifier that is generated for a specific transaction or interaction. Unlike traditional tokens or credentials that can be reused multiple times, a one-time use token serves a singular purpose and becomes invalidated or expired after its initial use.&lt;/p&gt;
&lt;p&gt;The primary purpose of a one-time use token is to enhance security and reduce the risk of unauthorized access or fraudulent activities. By limiting the token&amp;rsquo;s usability to a single instance, it significantly mitigates the chances of interception, replay attacks, or unauthorized reuse. This makes one-time use tokens an invaluable tool in various domains, including authentication systems, payment processing, and secure communication protocols.&lt;/p&gt;
&lt;h3 id="advantages-of-single-use-token-validation"&gt;Advantages of Single-Use Token Validation &lt;a class="heading-anchor" href="#advantages-of-single-use-token-validation" aria-label="Link to section: Advantages of Single-Use Token Validation"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;One-time use tokens are typically generated through a cryptographic algorithm or hash function, ensuring their uniqueness and tamper-resistant properties. Once generated, the token is associated with a specific transaction or user session, providing a temporary authorization credential that grants access to protected resources or validates an action. Once it fulfils its intended purpose, the token expires and becomes invalid, ensuring that any subsequent attempts to use or manipulate it are rendered ineffective.&lt;/p&gt;
&lt;p&gt;The advantages of one-time use tokens extend beyond just security. They also offer enhanced traceability and auditability, as each token can be easily tracked and linked to a specific event or transaction. This attribute becomes particularly valuable in scenarios where accountability and compliance are crucial, such as financial transactions or regulatory environments.&lt;/p&gt;
&lt;h2 id="implementing-one-time-jwt-token"&gt;Implementing One-Time JWT Token &lt;a class="heading-anchor" href="#implementing-one-time-jwt-token" aria-label="Link to section: Implementing One-Time JWT Token"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s talk about use-cases: Imagine you have a setup based on OAuth 2.0 that uses signed JWTs as bearer tokens for user-auth. Further imagine you want to provide &lt;a href="https://www.janbrennenstuhl.eu/knowledge-base/account-security/account-recovery/" title="Account Recovery &amp;amp; Password Resets"&gt;account recovery via password-reset&lt;/a&gt; by email with confirmation-link included. For that confirmation-link we would then need to have some kind of proof attached as query param to the URI. As the password-reset should work only once, the token is required to get invalided after successful usage. Hence, we would need the shared secret to have OTT characteristics.&lt;/p&gt;
&lt;p&gt;One solution could be a storage holding issued tokens and validity information. Every incoming password-reset request for the protected resource that the reset-link is targeting would then be authorized by the receiving web application against that central list. This so-called &lt;em&gt;&amp;ldquo;stateful approach&amp;rdquo;&lt;/em&gt; introduces a single point of truth. The token storage becomes the central bottleneck.&lt;/p&gt;
&lt;p&gt;It would be great if another solution could leverage existing concepts. For example our stateless authentication system with its ability to create, sign and verify JWTs …&lt;/p&gt;
&lt;h3 id="generating-and-validating-tokens"&gt;Generating and Validating Tokens &lt;a class="heading-anchor" href="#generating-and-validating-tokens" aria-label="Link to section: Generating and Validating Tokens"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The solution for our problem is actually straightforward as it is based on a simple trick. Although we can&amp;rsquo;t (and don&amp;rsquo;t want to) alter issued JWTs, our resource server is still the master of verification. We just need to look at JSON Web Token verification from another angle – for example by using app-state as input secret for the HMAC algorithm during JWS issuing!&lt;/p&gt;
&lt;p&gt;To stay in the example use case of account recovery that means, that using the users current password hash as secret key for the signing algorithm guarantees single-usage of every issued reset token. This is because the salted hash naturally always changes with a new input, which at the same time concludes the password-reset procedure successfully. There is no way the same token can pass verification twice. The signature checks in case of a replay of signed tokens would always fail.&lt;/p&gt;
&lt;p&gt;Using private, server-side state, sensitive data that changes as an outcome of a business operation, to sign a JSON Web Token that is used to authorize the very same business operation, results in the JWT effectively becoming a one-time token.&lt;/p&gt;
&lt;p&gt;&lt;figure class="figure"&gt;&lt;picture&gt;&lt;source type="image/avif" srcset="https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_82b3ab911812370a.avif 480w, https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_1c275ace7e00e66a.avif 960w, https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_acd59525ad8dd4fd.avif 1334w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)"&gt;&lt;img src="https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_ec0fab830cdbc0c8.webp" srcset="https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_2a2bba7ebdf8a485.webp 480w, https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_13ffda67d9084126.webp 960w, https://www.janbrennenstuhl.eu/single-use-jwt/one-time-jwt-diagram_hu_ec0fab830cdbc0c8.webp 1334w" sizes="(min-width: 760px) 720px, calc(100vw - 2.5rem)" width="1334" height="760" alt="Sequence diagram for one-time JWTs" loading="lazy" decoding="async"&gt;&lt;/picture&gt;&lt;figcaption&gt;How to use JWT as one-time-token OTT?&lt;/figcaption&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;h3 id="user-experience-and-usability-challenges"&gt;User Experience and Usability Challenges &lt;a class="heading-anchor" href="#user-experience-and-usability-challenges" aria-label="Link to section: User Experience and Usability Challenges"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Especially in the context of email based password reset capabilities there is however technical risk attached: One of the drawbacks of JSON Web Tokens (JWTs) is their larger payload size compared to other token type. Since signed JWTs store information within the token itself, including metadata and a massive digital signature, the size can grow significantly. This can have implications, especially in scenarios where tokens are transmitted as query parameters.&lt;/p&gt;
&lt;p&gt;Browsers impose a de-facto limit of 2000 characters for URIs and also email providers or clients appears to also struggle with large URIs. As many email links already include a zoo of e.g. tracking IDs, you definitely want to think and test twice before deciding to put a JWT into a query param.&lt;/p&gt;
&lt;h3 id="security-issues--risks"&gt;Security Issues &amp;amp; Risks &lt;a class="heading-anchor" href="#security-issues--risks" aria-label="Link to section: Security Issues &amp;amp; Risks"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Besides the regular security issues of JSON Web Token that largely resided with insufficient implementations on the signature verification, combining JWT and OTT characteristics does not introduce any new attack vectors. Both, authorization server and resource server have to perform the same tasks, whether they implement JWT or not.&lt;/p&gt;
&lt;h2 id="embracing-the-future-of-stateless-one-time-tokens"&gt;Embracing the Future of Stateless One-Time Tokens &lt;a class="heading-anchor" href="#embracing-the-future-of-stateless-one-time-tokens" aria-label="Link to section: Embracing the Future of Stateless One-Time Tokens"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;So let&amp;rsquo;s sum up our findings on how to implement single-use tokens based on JWT:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;There is no need to setup some kind of token-registry storage. This would counter the idea of stateless tokens anyway.&lt;/li&gt;
&lt;li&gt;Instead, compose signature secrets based on values that change after each token usage. In other words: &lt;strong&gt;Turn app state into HMAC-keys to guarantee one-time use of JWTs!&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Although in theory, this might also work for other use-cases like email verification or account confirmation, my thoughts took shape while reasoning about account recovery approaches. Furthermore, I do believe this to be a rather academical exercise and that we won&amp;rsquo;t see this getting widely adapted in the open wild.&lt;/p&gt;
&lt;p&gt;What do you think? Let me know &lt;a href="https://infosec.exchange/@jbspeakr" class="ext"&gt;via Mastodon&lt;/a&gt; and also make sure to share your exotic use cases for JSON web tokens and one-time token approaches with me. I also appreciate feedback and further discussion, thanks!&lt;/p&gt;</description></item></channel></rss>