Skip to content

Commit 3a80988

Browse files
committed
Add reclaming token mechanism in some samples
1 parent 8a289ab commit 3a80988

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

‎Samples/REST Services/Fill Address Form with Autosuggest/Fill Address Form with Autosuggest.html‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,40 @@
8484
};
8585
}
8686

87+
function getExpiration(jwtToken) {
88+
// Decode the JWT token to get the expiration timestamp
89+
const json = atob(jwtToken.split(".")[1]); - const decode = JSON.parse(json);
90+
91+
// Return the milliseconds until the token needs renewed
92+
// Reduce the time until renew by 2 minutes to avoid using an expired token
93+
// The exp property is the timestamp of the expiration in seconds
94+
const renewSkew = 120000;
95+
return (1000 * decode.exp) - Date.now() - renewSkew;
96+
}
97+
8798
async function initSasToken() {
99+
//Get Azure Maps SAS token.
100+
token = await getSasToken();
101+
102+
// Create a repeating timeout that will renew the SAS token
103+
// This timeout must be cleared once the TokenCredential object is no longer needed
104+
// If the timeout is not cleared the memory used by the TokenCredential will never be reclaimed.
105+
const renewToken = async () => { - try {
106+
console.log("Renewing token");
107+
token = await getSasToken();
108+
tokenRenewalTimer = setTimeout(renewToken, getExpiration(token));
109+
} catch (error) {
110+
console.log("Caught error when renewing token"); - console.log("Caught error when renewing token");
111+
clearTimeout(tokenRenewalTimer);
112+
throw error; - }
113+
}
114+
tokenRenewalTimer = setTimeout(renewToken, getExpiration(token));
115+
}
116+
117+
async function getSasToken() {
88118
//Fetch a SAS token from the token service.
89119
const response = await fetch(tokenServiceUrl);
90-
token = await response.text();
120+
return await response.text();
91121
}
92122
</script>
93123
<style>

‎Samples/REST Services/Get Users Timezone/Get Users Timezone.html‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
if (response.TimeZones && response.TimeZones.length > 0) {
5454
var timeZoneInfo = response.TimeZones[0];
55-
5655
var html = [];
5756

5857
html.push('IANA Id: ', timeZoneInfo.Id, '<br/>');
@@ -94,7 +93,7 @@
9493

9594
output.innerHTML = html.join('');
9695
} else {
97-
output.innerHTML = "No timezone information found."
96+
output.innerHTML = "No timezone information found.";
9897
}
9998
});
10099
}, function (error) {
@@ -116,10 +115,43 @@
116115
});
117116
}
118117

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+
119130
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() {
120152
//Fetch a SAS token from the token service.
121153
const response = await fetch(tokenServiceUrl);
122-
token = await response.text();
154+
return await response.text();
123155
}
124156
</script>
125157
</head>

0 commit comments

Comments
 (0)