forked from docs/doc-exports
Reviewed-by: Kovács, Zoltán <zkovacs@t-systems.com> Co-authored-by: Dong, Qiu Jian <qiujiandong1@huawei.com> Co-committed-by: Dong, Qiu Jian <qiujiandong1@huawei.com>
59 lines
8.0 KiB
HTML
59 lines
8.0 KiB
HTML
<a name="asm_01_0096"></a><a name="asm_01_0096"></a>
|
|
|
|
<h1 class="topictitle1">JWT Authentication Principles</h1>
|
|
<div id="body0000001527767249"><div class="section" id="asm_01_0096__section464417112143"><h4 class="sectiontitle">JWT Authentication Principles</h4><p id="asm_01_0096__p218512321416">JWT is an authentication mode in which the server issues tokens to the client. When a user logs in to the client using the username and password, the server generates and returns a token to the client. The client only needs to carry the token when sending a request to the server. The server verifies the token to determine whether the request is from a valid client and determines whether to return a response to the client. This method, which connects authenticated clients based on tokens in requests, solves various stateful problems of storing sessions on a server at an early stage.</p>
|
|
<p id="asm_01_0096__p65857320143">In Istio, the JWT token is generated by a specific authentication service and verified by meshes, completely decoupling the authentication logic in user services and enabling applications to focus on their own services. <a href="#asm_01_0096__fig184171654514">Figure 1</a> shows the complete Istio-based JWT mechanism.</p>
|
|
<div class="fignone" id="asm_01_0096__fig184171654514"><a name="asm_01_0096__fig184171654514"></a><a name="fig184171654514"></a><span class="figcap"><b>Figure 1 </b>Istio JWT authentication process</span><br><span><img id="asm_01_0096__image1651116144513" src="en-us_image_0000001477127516.png"></span></div>
|
|
<p id="asm_01_0096__p931719814145">1. The client connects the authentication service by logging with the user name and password.</p>
|
|
<p id="asm_01_0096__p9624151833416">2. The authentication service verifies the username and password, generates a JWT token (including the user ID and expiration time), and signs the token with the private key of the authentication service.</p>
|
|
<p id="asm_01_0096__p1085461116356">3. The authentication service returns the JWT token to the client.</p>
|
|
<p id="asm_01_0096__p188134883515">4. The client stores the JWT token locally for subsequent requests.</p>
|
|
<p id="asm_01_0096__p67571911163617">5. When requesting to another service, the client carries the JWT token and does not need to provide information such as the username and password.</p>
|
|
<p id="asm_01_0096__p723305810572">6. The mesh data plane proxy intercepts the traffic and verifies the JWT token using the configured public key.</p>
|
|
<p id="asm_01_0096__p12372145893619">7. Once the JWT token is verified, the mesh proxy forwards the request to the server.</p>
|
|
<p id="asm_01_0096__p161994421339">8. The server processes the request.</p>
|
|
<p id="asm_01_0096__p19176956185815">9. The server returns the response data to the mesh proxy.</p>
|
|
<p id="asm_01_0096__p10471151102319">10. The mesh data plane proxy forwards the response data to the caller.</p>
|
|
<p id="asm_01_0096__p4377174223317">The step 6 is important because the JWT authentication function is migrated from the server to the mesh proxy. The mesh data plane obtains, from the authentication policy configured by the control plane, the public key for verifying the JWT token. The public key may be one configured on the JWKS (JSON Web Key Set) or one obtained from a public key address configured by jwksUri. After obtaining the public key, the mesh proxy uses it to verify the token signed by the private key of the authentication service, decrypts the <strong id="asm_01_0096__b1465116553115">iss</strong> in the token, and verifies whether the issuer information in the authentication policy is matched. If the verification is successful, the request is sent to the application. If not, the request is rejected.</p>
|
|
</div>
|
|
<div class="section" id="asm_01_0096__section1695162651116"><h4 class="sectiontitle">JWT Structure</h4><p id="asm_01_0096__p18271933101112">JWT is of the JSON structure that contains specific declarations. Step 6 of the JWT authentication process shows the request identity can be confirmed by verifying the JSON structure. Querying the backend service is not needed. The following shows how the authentication information is carried by parsing the JWT structure.</p>
|
|
<p id="asm_01_0096__p13181602132">A JWT token consists of three parts: header, payload, and signature.</p>
|
|
<ul id="asm_01_0096__ul1890112184131"><li id="asm_01_0096__li1690121821315">Header<p id="asm_01_0096__p1425912298135"><a name="asm_01_0096__li1690121821315"></a><a name="li1690121821315"></a>Describes the JWT metadata, including the algorithm <strong id="asm_01_0096__b1414613981814">alg</strong> and type <strong id="asm_01_0096__b9894104210181">typ</strong>. <strong id="asm_01_0096__b1222103621915">alg</strong> describes the signature algorithm so that the receiver can verify the signature based on the corresponding algorithm. The default algorithm is <strong id="asm_01_0096__b119262046152011">HS256</strong> (as follows), indicating <strong id="asm_01_0096__b191742443203">HMAC-SHA256</strong>. <strong id="asm_01_0096__b594518913216">typ</strong> indicates the token type. If <strong id="asm_01_0096__b153511840112116">typ</strong> is <strong id="asm_01_0096__b15955194410213">JWT</strong>, indicating that the token is of the JWT type.</p>
|
|
<pre class="screen" id="asm_01_0096__screen3425202101414">{
|
|
"alg": "HS256",
|
|
"typ": "JWT"
|
|
}</pre>
|
|
</li><li id="asm_01_0096__li16261420101313">Payload<p id="asm_01_0096__p141736300137"><a name="asm_01_0096__li16261420101313"></a><a name="li16261420101313"></a>Stores the main content of the token. The authentication service AuthN generates related information and places the information in the token payload. The attributes of <strong id="asm_01_0096__b1828064019266">payload</strong> include:</p>
|
|
<ul id="asm_01_0096__ul15130105111518"><li id="asm_01_0096__li1213014591512"><strong id="asm_01_0096__b1431011587269">iss</strong>: token issuer</li><li id="asm_01_0096__li1892234410155"><strong id="asm_01_0096__b56601420275">aud</strong>: token audience</li></ul>
|
|
<p id="asm_01_0096__p1910525631417">During JWT verification, the <strong id="asm_01_0096__b918712473271">iss</strong> and <strong id="asm_01_0096__b1846417521273">aud</strong> will be verified to check whether they are matched with the token issuer and audience. The JWT content is not encrypted. All services that obtain the token can view the content in the token payload. You are advised not to store private information in the payload.</p>
|
|
</li><li id="asm_01_0096__li34541624181317">Signature<p id="asm_01_0096__p6896531101318"><a name="asm_01_0096__li34541624181317"></a><a name="li34541624181317"></a>Signature of the header and payload, ensuring that only the specific legitimate authentication services can issue tokens. Generally, the header and payload are converted into strings using Base64, and then the private key of the authentication service is used to sign the strings. The signature algorithm is the <strong id="asm_01_0096__b1821253115526">alg</strong> defined in the header.</p>
|
|
</li></ul>
|
|
<div class="p" id="asm_01_0096__p176156021316">The following is a complete JWT example. Signature is obtained by signing the header and payload.<pre class="screen" id="asm_01_0096__screen2034919802118"># Header:
|
|
{
|
|
"alg": "RS512",
|
|
"typ": "JWT"
|
|
}
|
|
|
|
# Payload
|
|
{
|
|
"iss": "weather@cloudnative-istio",
|
|
"audience": "weather@cloudnative-istio"
|
|
}
|
|
|
|
# Signature
|
|
RSASHA512(
|
|
base64UrlEncode(header) + "." +
|
|
base64UrlEncode(payload)
|
|
)</pre>
|
|
</div>
|
|
<p id="asm_01_0096__p1139215622110">The token output of the preceding structure is as follows. The three strings separated by periods (.) correspond to the header, payload, and signature of the JWT structure, respectively.</p>
|
|
<pre class="screen" id="asm_01_0096__screen65361821182218">eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ2ODU5ODk3MDAsInZlciI6IjIuMCIsImlhdCI6MTUzMjM4OTcwMCwiaXNzIjoid2VhdGhlckBjbG91ZG5hdGl2ZS1pc3Rpby5ib29rIiwic3ViIjoid2VhdGhlckBjbG91ZG5hdGl2ZS1pc3Rpby5ib29rIn0.SEp-8qiMwI45BuBgQPH-wTHvOYxcE_jPI0wqOxEpauw</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="asm_01_0087.html">Security</a></div>
|
|
</div>
|
|
</div>
|
|
|