<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: unstable JWT TOKEN authentication accessing Qlik SaaS embeds in Integration, Extension &amp; APIs</title>
    <link>https://community.qlik.com/t5/Integration-Extension-APIs/unstable-JWT-TOKEN-authentication-accessing-Qlik-SaaS-embeds/m-p/2460798#M20751</link>
    <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/301779"&gt;@Livia_Gu&lt;/a&gt;&amp;nbsp;Hey there! question: is it possible for you to use OAuth? I'm using it here in our services company and it is working flawlessly on Qlik Cloud with OAuth m2m impersonation, etc.&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jun 2024 13:53:38 GMT</pubDate>
    <dc:creator>thomaspessato_</dc:creator>
    <dc:date>2024-06-10T13:53:38Z</dc:date>
    <item>
      <title>unstable JWT TOKEN authentication accessing Qlik SaaS embeds</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/unstable-JWT-TOKEN-authentication-accessing-Qlik-SaaS-embeds/m-p/2459717#M20738</link>
      <description>&lt;P&gt;I am trying to set JWT token authentication to anonymous access to Qlik embed content by following the tutorial&amp;nbsp;&lt;BR /&gt;&lt;A href="https://qlik.dev/embed/iframe/quickstart/embedding-with-anonymous-access-and-qlik-cloud/" target="_blank"&gt;https://qlik.dev/embed/iframe/quickstart/embedding-with-anonymous-access-and-qlik-cloud/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;I followed the flow, and done all the steps. However, the token generated seems very unstable. most of the time, I would get unauthorised error as follow:&amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-06-06 at 3.12.55 pm.png" style="width: 810px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/167431iD5206CCFF4A095D0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2024-06-06 at 3.12.55 pm.png" alt="Screenshot 2024-06-06 at 3.12.55 pm.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;But sometimes, I retry the same token, it will successfully log me in, other time, it still get the same error.&amp;nbsp;&lt;BR /&gt;It is also mentioned in this post&amp;nbsp;&lt;BR /&gt;&lt;A href="https://community.qlik.com/t5/Integration-Extension-APIs/Seemingly-unstable-JWT-Authentication-for-Qlik-SaaS-Mashup/td-p/1954395" target="_blank"&gt;https://community.qlik.com/t5/Integration-Extension-APIs/Seemingly-unstable-JWT-Authentication-for-Qlik-SaaS-Mashup/td-p/1954395&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;so I implemented retry mechanism, code as follow:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;  async function retryJwtLogin(token, maxRetries = &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; {
        for (let attempt = 1; attempt &amp;lt;= maxRetries; attempt++) {
          try {
            const loginRes = await jwtLogin(token);
            if (loginRes) {
              return loginRes; // Return the successful response
            } else {
              console.error(`Attempt ${attempt} failed`);
            }
          } catch (error) {
            console.error(
              `Attempt ${attempt} failed with error: ${error.message}`
            );
            return null;
          }

          if (attempt &amp;lt; maxRetries) {
            console.log(`Retrying... (${attempt}/${maxRetries})`);
          } else {
            const message =
              "Something went wrong while logging in after multiple attempts.";
            throw new Error(message);
          }
        }
      }

  async function jwtLogin(token) {
        try {
          const authHeader = `Bearer ${token}`;
          const reponse = await fetch(
            `https://${TENANT}/login/jwt-session?qlik-web-integration-id=${WEBINTEGRATIONID}`,
            {
              credentials: "include",
              mode: "cors",
              method: "POST",
              headers: {
                Authorization: authHeader,
                "qlik-web-integration-id": WEBINTEGRATIONID,
              },
            }
          );
          if (reponse.status === 200) {
            console.log(117, await reponse.json());
            return reponse;
          } else {
            console.log(117, await reponse.json());
            return null;
          }
        } catch (e) {
          console.error(e);
          return null;
        }
      }&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;The result is still the same, most of the time i get unauthrised error, but occasionally it successfully logs me in.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;my token decode is as following:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;{
  sub: 'ANON//fc2018e5-e566-467d-9958-bc3a8a78c480',
  subType: 'user',
  name: 'anonymous',
  email: 'fc2018e5-e566-467d-9958-bc3a8a78c480@anon.com',
  email_verified: true,
  iss: 'my issuer.ap.qlikcloud.com',
  iat: 1717650839500,
  nbf: 1717650901500,
  exp: 1717654400500,
  jti: 'fc2018e5-e566-467d-9958-bc3a8a78c480',
  aud: 'qlik.api/login/jwt-session',
  groups: [ 'Anonymous' ]
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also made sure that exp does not exceed 3600 seconds and jti is unique, also I tried the method mentioned in previous post, to make iat 1 min early before current time.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Still, I have no idea why the authentication would sometimes work and sometimes fail giving the same code and configration.&amp;nbsp; Does anyone also encounter this or have a solution for it? Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jun 2024 06:01:23 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/unstable-JWT-TOKEN-authentication-accessing-Qlik-SaaS-embeds/m-p/2459717#M20738</guid>
      <dc:creator>Livia_Gu</dc:creator>
      <dc:date>2024-06-06T06:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: unstable JWT TOKEN authentication accessing Qlik SaaS embeds</title>
      <link>https://community.qlik.com/t5/Integration-Extension-APIs/unstable-JWT-TOKEN-authentication-accessing-Qlik-SaaS-embeds/m-p/2460798#M20751</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/301779"&gt;@Livia_Gu&lt;/a&gt;&amp;nbsp;Hey there! question: is it possible for you to use OAuth? I'm using it here in our services company and it is working flawlessly on Qlik Cloud with OAuth m2m impersonation, etc.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 13:53:38 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Integration-Extension-APIs/unstable-JWT-TOKEN-authentication-accessing-Qlik-SaaS-embeds/m-p/2460798#M20751</guid>
      <dc:creator>thomaspessato_</dc:creator>
      <dc:date>2024-06-10T13:53:38Z</dc:date>
    </item>
  </channel>
</rss>

