|
52 | 52 |
|
53 | 53 | if (response.TimeZones && response.TimeZones.length > 0) { |
54 | 54 | var timeZoneInfo = response.TimeZones[0]; |
55 | | - |
56 | 55 | var html = []; |
57 | 56 |
|
58 | 57 | html.push('IANA Id: ', timeZoneInfo.Id, '<br/>'); |
|
94 | 93 |
|
95 | 94 | output.innerHTML = html.join(''); |
96 | 95 | } else { |
97 | | - output.innerHTML = "No timezone information found." |
| 96 | + output.innerHTML = "No timezone information found."; |
98 | 97 | } |
99 | 98 | }); |
100 | 99 | }, function (error) { |
|
116 | 115 | }); |
117 | 116 | } |
118 | 117 |
|
| 118 | + function getExpiration(jwtToken) { |
| 119 | + // Decode the JWT token to get the expiration timestamp. |
| 120 | + const json = atob(jwtToken.split(".")[1]); |
| 121 | + const decode = JSON.parse(json); |
| 122 | + |
| 123 | + // Return the milliseconds until the token needs renewed. |
| 124 | + // Reduce the time until renew by 2 minutes to avoid using an expired token. |
| 125 | + // The exp property is the timestamp of the expiration in seconds |
| 126 | + const renewSkew = 120000; |
| 127 | + return (1000 * decode.exp) - Date.now() - renewSkew; |
| 128 | + } |
| 129 | + |
119 | 130 | async function initSasToken() { |
| 131 | + //Fetch a SAS token from the token service. |
| 132 | + token = await getSasToken(); |
| 133 | + |
| 134 | + // Create a repeating timeout that will renew the SAS token. |
| 135 | + // This timeout must be cleared once the TokenCredential object is no longer needed |
| 136 | + // If the timeout is not cleared the memory used by the TokenCredential will never be reclaimed. |
| 137 | + const renewToken = async () => { |
| 138 | + try { |
| 139 | + console.log("Renewing token"); |
| 140 | + token = await getSasToken(); |
| 141 | + tokenRenewalTimer = setTimeout(renewToken, getExpiration(token)); |
| 142 | + } catch (error) { |
| 143 | + console.log("Caught error when renewing token"); |
| 144 | + clearTimeout(tokenRenewalTimer); |
| 145 | + throw error; |
| 146 | + } |
| 147 | + }; |
| 148 | + tokenRenewalTimer = setTimeout(renewToken, getExpiration(token)); |
| 149 | + } |
| 150 | + |
| 151 | + async function getSasToken() { |
120 | 152 | //Fetch a SAS token from the token service. |
121 | 153 | const response = await fetch(tokenServiceUrl); |
122 | | - token = await response.text(); |
| 154 | + return await response.text(); |
123 | 155 | } |
124 | 156 | </script> |
125 | 157 | </head> |
|
0 commit comments