Skip to content

Commit 9a516e4

Browse files
committed
update code from ADAL to MSAL and added handling claims challenge
1 parent 4328374 commit 9a516e4

File tree

1 file changed

+64
-10
lines changed

1 file changed

+64
-10
lines changed

articles/active-directory/develop/msal-error-handling-js.md

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ The following error types are available:
5454

5555
- `InteractionRequiredAuthError`: Error class, extends `ServerError` to represent server errors, which require an interactive call. This error is thrown by `acquireTokenSilent` if the user is required to interact with the server to provide credentials or consent for authentication/authorization. Error codes include `"interaction_required"`, `"login_required"`, and `"consent_required"`.
5656

57-
For error handling in authentication flows with redirect methods (`loginRedirect`, `acquireTokenRedirect`), you'll need to register the callback, which is called with success or failure after the redirect using `handleRedirectCallback()` method as follows:
57+
For error handling in authentication flows with redirect methods (`loginRedirect`, `acquireTokenRedirect`), you'll need to handle the promise, which is called with success or failure after the redirect using `handleRedirectPromise()` method as follows:
5858

5959
```javascript
60-
function authCallback(error, response) {
61-
//handle redirect response
62-
}
63-
64-
var myMSALObj = new Msal.UserAgentApplication(msalConfig);
60+
var myMSALObj = new Msal.PublicClientApplication(msalConfig);
6561

6662
// Register Callbacks for redirect flow
67-
myMSALObj.handleRedirectCallback(authCallback);
63+
myMSALObj.handleRedirectPromise()
64+
.then(function (response) {
65+
//success response
66+
})
67+
.catch((error) => {
68+
console.log(error);
69+
})
6870
myMSALObj.acquireTokenRedirect(request);
6971
```
7072

@@ -140,13 +142,65 @@ myMSALObj.acquireTokenSilent(accessTokenRequest).then(function(accessTokenRespon
140142

141143
Interactively acquiring the token prompts the user and gives them the opportunity to satisfy the required Conditional Access policy.
142144

143-
When calling an API requiring Conditional Access, you can receive a claims challenge in the error from the API. In this case, you can pass the claims returned in the error to the `claimsRequest` field of the `AuthenticationParameters.ts` class to satisfy the appropriate policy.
145+
When calling an API requiring Conditional Access, you can receive a claims challenge in error from the API. In this case, you can extract the claims challenge from the `WWW-Authenticate` header from the API error response object as shown in the `handleClaimsChallenge` method.
146+
147+
```javascript
148+
fetch(apiEndpoint, options)
149+
.catch((response) => {
150+
if (response.status === 401 && response.headers.get('www-authenticate')) {
151+
152+
const authenticateHeader = response.headers.get('www-authenticate');
153+
const claimsChallenge = parseChallenges(authenticateHeader).claims;
154+
// use the claims challenge to acquire a new access token...
155+
}
156+
})
157+
158+
/**
159+
* This method parses WWW-Authenticate authentication headers
160+
* @param header
161+
* @return {Object} challengeMap
162+
*/
163+
const parseChallenges = (header) => {
164+
const schemeSeparator = header.indexOf(' ');
165+
const challenges = header.substring(schemeSeparator + 1).split(', ');
166+
const challengeMap = {};
167+
168+
challenges.forEach((challenge) => {
169+
const [key, value] = challenge.split('=');
170+
challengeMap[key.trim()] = window.decodeURI(value.replace(/(^"|"$)/g, ''));
171+
});
172+
173+
return challengeMap;
174+
}
175+
```
176+
177+
Then pass the claims returned in the respond error to the request object in the `acquireToken` APIs to receive a new token that contains the claims.
178+
179+
```javascript
180+
const accessTokenRequest = {
181+
claims: window.atob(claimsChallenge), // decode the base64 string
182+
scopes: [],
183+
};
184+
185+
myMSALObj.acquireTokenSilent(accessTokenRequest).then(function(accessTokenResponse) {
186+
// call API
187+
}).catch(function(error) {
188+
if (error instanceof InteractionRequiredAuthError) {
189+
190+
myMSALObj.acquireTokenPopup(accessTokenRequest).then(function(accessTokenResponse) {
191+
// call API
192+
}).catch(function(error) {
193+
console.log(error);
194+
});
195+
}
196+
});
197+
```
144198

145-
See [Requesting Additional Claims](active-directory-optional-claims.md) for more detail.
199+
See [Requesting Additional Claims](active-directory-optional-claims.md) and [How to use Continuous Access Evaluation enabled APIs in your applications](./app-resilience-continuous-access-evaluation.md) for more detail.
146200

147201

148202
[!INCLUDE [Active directory error handling retries](../../../includes/active-directory-develop-error-handling-retries.md)]
149203

150204
## Next steps
151205

152-
Consider enabling [Logging in MSAL.js](msal-logging-js.md) to help you diagnose and debug issues.
206+
Consider enabling [Logging in MSAL.js](msal-logging-js.md) to help you diagnose and debug issues

0 commit comments

Comments
 (0)