Skip to content

Commit cac9be1

Browse files
authored
Merge pull request #212371 from derisen/patch-9
Update code snippets for JavaScript
2 parents 995ed7a + 7486d90 commit cac9be1

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

articles/active-directory/develop/app-resilience-continuous-access-evaluation.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,22 @@ When these conditions are met, the app can extract the claims challenge from the
105105

106106
```javascript
107107
const authenticateHeader = response.headers.get('www-authenticate');
108-
const claimsChallenge = authenticateHeader
109-
.split(' ')
110-
.find((entry) => entry.includes('claims='))
111-
.split('claims="')[1]
112-
.split('",')[0];
108+
const claimsChallenge = parseChallenges(authenticateHeader).claims;
109+
110+
// ...
111+
112+
function parseChallenges(header) {
113+
const schemeSeparator = header.indexOf(' ');
114+
const challenges = header.substring(schemeSeparator + 1).split(',');
115+
const challengeMap = {};
116+
117+
challenges.forEach((challenge) => {
118+
const [key, value] = challenge.split('=');
119+
challengeMap[key.trim()] = window.decodeURI(value.replace(/['"]+/g, ''));
120+
});
121+
122+
return challengeMap;
123+
}
113124
```
114125

115126
Your app would then use the claims challenge to acquire a new access token for the resource.
@@ -118,22 +129,19 @@ Your app would then use the claims challenge to acquire a new access token for t
118129
let tokenResponse;
119130

120131
try {
121-
122132
tokenResponse = await msalInstance.acquireTokenSilent({
123-
claims: window.atob(claimsChallenge), // decode the base64 string
124-
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
125-
account: account, // current active account
126-
});
133+
claims: window.atob(claimsChallenge), // decode the base64 string
134+
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
135+
account: account, // current active account
136+
});
127137

128138
} catch (error) {
129-
130139
if (error instanceof InteractionRequiredAuthError) {
131-
132140
tokenResponse = await msalInstance.acquireTokenPopup({
133-
claims: window.atob(claimsChallenge), // decode the base64 string
134-
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
135-
account: account, // current active account
136-
});
141+
claims: window.atob(claimsChallenge), // decode the base64 string
142+
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
143+
account: account, // current active account
144+
});
137145
}
138146

139147
}

0 commit comments

Comments
 (0)