Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 833f29c

Browse files
committed
fix string comparison bug in b2c samples
1 parent 7dfca3c commit 833f29c

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

1-Authentication/2-sign-in-b2c/SPA/src/App.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useEffect } from 'react';
2+
import { Route, Routes } from 'react-router-dom';
23
import { MsalProvider, useMsal } from '@azure/msal-react';
34
import { EventType } from '@azure/msal-browser';
45

5-
import { Route, Routes } from 'react-router-dom';
66
import { PageLayout } from './components/PageLayout';
77
import { Home } from './pages/Home';
88
import { b2cPolicies } from './authConfig';
9-
9+
import { compareIssuingPolicy } from './utils/claimUtils';
1010

1111
import './styles/App.css';
1212

@@ -57,7 +57,7 @@ const Pages = () => {
5757
* you can replace the code below with the same pattern used for handling the return from
5858
* profile edit flow
5959
*/
60-
if (event.payload.idTokenClaims['tfp'] === b2cPolicies.names.forgotPassword) {
60+
if (compareIssuingPolicy(event.payload.idTokenClaims, b2cPolicies.names.forgotPassword)) {
6161
let signUpSignInFlowRequest = {
6262
authority: b2cPolicies.authorities.signUpSignIn.authority,
6363
};

1-Authentication/2-sign-in-b2c/SPA/src/utils/claimUtils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,16 @@ const populateClaim = (claim, value, description, index, claimsObject) => {
220220
const changeDateFormat = (date) => {
221221
let dateObj = new Date(date * 1000);
222222
return `${date} - [${dateObj.toString()}]`;
223-
};
223+
};
224+
225+
/**
226+
* Compare the token issuing policy with a specific policy name
227+
* @param {object} idTokenClaims - Object containing the claims from the parsed token
228+
* @param {string} policyToCompare - ID/Name of the policy as expressed in the Azure portal
229+
* @returns {boolean}
230+
*/
231+
export function compareIssuingPolicy(idTokenClaims, policyToCompare) {
232+
let tfpMatches = idTokenClaims.hasOwnProperty('tfp') && idTokenClaims['tfp'].toLowerCase() === policyToCompare.toLowerCase();
233+
let acrMatches = idTokenClaims.hasOwnProperty('acr') && idTokenClaims['acr'].toLowerCase() === policyToCompare.toLowerCase();
234+
return tfpMatches || acrMatches;
235+
}

3-Authorization-II/2-call-api-b2c/SPA/src/App.jsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { useEffect } from 'react';
2+
import { Routes, Route } from "react-router-dom";
23
import { MsalProvider, useMsal } from '@azure/msal-react';
34
import { EventType } from '@azure/msal-browser';
45

5-
import { Routes, Route } from "react-router-dom";
66
import { PageLayout } from './components/PageLayout';
77
import { TodoList } from './pages/TodoList';
88
import { Home } from './pages/Home';
9-
109
import { b2cPolicies, protectedResources } from './authConfig';
1110
import { compareIssuingPolicy } from './utils/claimUtils';
1211

3-Authorization-II/2-call-api-b2c/SPA/src/hooks/useFetchWithMsal.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
useCallback,
44
} from 'react';
55

6-
import { InteractionType, PopupRequest } from '@azure/msal-browser';
6+
import { InteractionType } from '@azure/msal-browser';
77
import { useMsal, useMsalAuthentication } from "@azure/msal-react";
88

99
/**

3-Authorization-II/2-call-api-b2c/SPA/src/utils/claimUtils.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ export const createClaimsTable = (claims) => {
175175
index++;
176176
break;
177177
case "at_hash":
178-
populateClaim(
179-
key,
180-
claims[key],
181-
'An access token hash included in an ID token only when the token is issued together with an OAuth 2.0 access token. An access token hash can be used to validate the authenticity of an access token',
182-
index,
183-
claimsObj
184-
);
178+
populateClaim(
179+
key,
180+
claims[key],
181+
'An access token hash included in an ID token only when the token is issued together with an OAuth 2.0 access token. An access token hash can be used to validate the authenticity of an access token',
182+
index,
183+
claimsObj
184+
);
185185
index++;
186186
break;
187187
case 'uti':
@@ -225,13 +225,12 @@ const changeDateFormat = (date) => {
225225

226226
/**
227227
* Compare the token issuing policy with a specific policy name
228-
* @param {object} idTokenClaims - Object containining token claims
228+
* @param {object} idTokenClaims - Object containing the claims from the parsed token
229229
* @param {string} policyToCompare - ID/Name of the policy as expressed in the Azure portal
230230
* @returns {boolean}
231231
*/
232-
export function compareIssuingPolicy(idTokenClaims, policyToCompare) {
233-
let tfpMatches = idTokenClaims['tfp'] === policyToCompare.toLowerCase();
234-
let acrMatches = idTokenClaims['acr'] === policyToCompare.toLowerCase()
235-
return tfpMatches || acrMatches
236-
}
237-
232+
export function compareIssuingPolicy(idTokenClaims, policyToCompare) {
233+
let tfpMatches = idTokenClaims.hasOwnProperty('tfp') && idTokenClaims['tfp'].toLowerCase() === policyToCompare.toLowerCase();
234+
let acrMatches = idTokenClaims.hasOwnProperty('acr') && idTokenClaims['acr'].toLowerCase() === policyToCompare.toLowerCase();
235+
return tfpMatches || acrMatches;
236+
}

0 commit comments

Comments
 (0)