Skip to content

Commit d5b8846

Browse files
committed
sync
1 parent c76bf7b commit d5b8846

5 files changed

Lines changed: 47 additions & 40 deletions

File tree

3-Authorization-II/1-call-api/AppCreationScripts/sample.json

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"Mappings": [
4040
{
4141
"key": "clientId",
42-
"value": ".AppId"
42+
"value": "Service.AppId"
4343
},
4444
{
4545
"key": "tenantId",
@@ -52,26 +52,34 @@
5252
"SettingKind": "JSON",
5353
"SettingFile": "\\..\\WebApp\\auth.json",
5454
"Mappings": [
55-
{
56-
"key": "clientId",
57-
"value": ".AppId"
58-
},
59-
{
60-
"key": "tenantId",
61-
"value": "$tenantId"
62-
},
63-
{
64-
"key": "clientSecret",
65-
"value": ".AppKey"
66-
},
67-
{
68-
"key": "redirectUri",
69-
"value": ".ReplyUrls"
70-
},
71-
{
72-
"key": "postLogoutRedirectUri",
73-
"value": ".HomePage"
74-
}
55+
{
56+
"key": "clientId",
57+
"value": ".AppId"
58+
},
59+
{
60+
"key": "tenantId",
61+
"value": "$tenantId"
62+
},
63+
{
64+
"key": "clientSecret",
65+
"value": ".AppKey"
66+
},
67+
{
68+
"key": "redirectUri",
69+
"value": ".ReplyUrls"
70+
},
71+
{
72+
"key": "postLogoutRedirectUri",
73+
"value": ".HomePage"
74+
},
75+
{
76+
"key": "endpoint",
77+
"value": "Service.HomePage"
78+
},
79+
{
80+
"key": "scopes",
81+
"value": "Service.Scope"
82+
}
7583
]
7684
}
7785
]

3-Authorization-II/1-call-api/WebApp/App/routes/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const MsalExpressMiddleware = require('../../../../../MsalNodeCommons/MsalExpres
66
const auth = require('../../auth.json');
77
const cache = require('../utils/cachePlugin');
88

9-
const msal = new MsalExpressMiddleware(auth, cache);
9+
const msal = new MsalExpressMiddleware(auth);
1010

1111
// initialize router
1212
const router = express.Router();

5-Deployment/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
## Overview
1515

16-
This sample demonstrates how to deploy a Node.js & Express web application coupled with a Node.js & Express web API to **Azure Cloud** using the [Azure App Service](https://docs.microsoft.com/azure/app-service/). To do so, we will use the [same code sample from Chapter 3](../3-Authorization-II/1-call-api).
16+
This sample demonstrates how to deploy a Node.js & Express web application coupled with a Node.js & Express web API to **Azure Cloud** using the [Azure App Service](https://docs.microsoft.com/azure/app-service/). To do so, we will use the [same code sample from Chapter 3](../3-Authorization-II/1-call-api).
1717

1818
> :information_source: The steps below apply similarly to B2C applications, for instance the [B2C sample from Chapter 3](../3-Authorization-II/2-call-api-b2c)
1919
@@ -127,7 +127,7 @@ At this point, the only field left to update is `resources.webAPI.endpoint`. We
127127
2. On the **App Service** explorer section you will see an upward-facing arrow icon. Click on it publish your local files in the `WebAPI` folder to **Azure App Services**.
128128
3. Choose a creation option based on the operating system to which you want to deploy. in this sample, we choose **Linux**.
129129
4. Select a Node.js version when prompted. An **LTS** version is recommended.
130-
5. Type a globally unique name for your web API and press Enter. The name must be unique across all of **Azure**. (e.g. `https://msal-nodejs-webapi1.azurewebsites.net/`)
130+
5. Type a globally unique name for your web API and press Enter. The name must be unique across all of **Azure**. (e.g. `msal-nodejs-webapi1`)
131131
6. After you respond to all the prompts, **VS Code** shows the **Azure** resources that are being created for your app in its notification popup.
132132
7. Select **Yes** when prompted to update your configuration to run npm install on the target Linux server.
133133

MsalNodeCommons/CryptoUtilities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { v4: uuidv4 } = require('uuid');
77

88
/**
99
* Basic cryptography methods for generating GUIDs and encoding state.
10-
* Credits: https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node/src/crypto
10+
* Source: https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node/src/crypto
1111
*/
1212
class CryptoUtilities {
1313

MsalNodeCommons/MsalExpressMiddleware.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class MsalExpressMiddleware {
8282
auth: {
8383
clientId: config.credentials.clientId,
8484
authority: config.hasOwnProperty('policies') ? config.policies.signUpSignIn.authority : constants.AuthorityStrings.AAD + config.credentials.tenantId, // single organization
85-
clientSecret: config.credentials.hasOwnProperty('clientSecret') ? config.credentials.clientSecret : "",
85+
clientSecret: config.credentials.clientSecret,
8686
redirectUri: config.hasOwnProperty('configuration') ? config.configuration.redirectUri : null, // defaults to calling page
8787
knownAuthorities: config.hasOwnProperty('policies') ? [config.policies.authorityDomain] : [], //
8888
},
@@ -154,7 +154,7 @@ class MsalExpressMiddleware {
154154
stage: constants.AppStages.RESET_PASSWORD,
155155
path: req.route.path,
156156
rand: req.session.rand
157-
}), 'null');
157+
}), null);
158158

159159
// if coming for password reset, set the authority to resetPassword
160160
this.getAuthCode(
@@ -224,6 +224,7 @@ class MsalExpressMiddleware {
224224
// check if rand matches
225225
if (state.rand === req.session.rand) {
226226
if (state.stage === constants.AppStages.SIGN_IN) {
227+
227228
// token request should have auth code
228229
const tokenRequest = {
229230
redirectUri: this.msalConfig.auth.redirectUri,
@@ -357,7 +358,6 @@ class MsalExpressMiddleware {
357358
// TODO: cache fail safe
358359
if (!account) {
359360
throw new Error('account not found');
360-
361361
}
362362

363363
const silentRequest = {
@@ -415,15 +415,15 @@ class MsalExpressMiddleware {
415415
rand: req.session.rand
416416
}), null);
417417

418-
// initiate the first leg of auth code grant to get token
419-
this.getAuthCode(
420-
this.msalConfig.auth.authority,
421-
scopes,
422-
state,
423-
this.msalConfig.auth.redirectUri,
424-
req,
425-
res
426-
);
418+
// initiate the first leg of auth code grant to get token
419+
this.getAuthCode(
420+
this.msalConfig.auth.authority,
421+
scopes,
422+
state,
423+
this.msalConfig.auth.redirectUri,
424+
req,
425+
res
426+
);
427427
}
428428
});
429429
};
@@ -539,7 +539,7 @@ class MsalExpressMiddleware {
539539
* https://docs.microsoft.com/azure/active-directory/develop/id-tokens#validating-an-id_token
540540
*/
541541
const checkAudience = idTokenClaims["aud"] === this.msalConfig.auth.clientId ? true : false;
542-
const checkTimestamp = idTokenClaims["iat"] < now && idTokenClaims["exp"] > now ? true: false;
542+
const checkTimestamp = idTokenClaims["iat"] <= now && idTokenClaims["exp"] >= now ? true: false;
543543

544544
// TODO: B2C check tenant
545545
const checkTenant = (this.rawConfig.hasOwnProperty('policies') && idTokenClaims["tid"] === undefined) || idTokenClaims["tid"] === this.rawConfig.credentials.tenantId ? true : false;
@@ -556,7 +556,6 @@ class MsalExpressMiddleware {
556556
const now = Math.round((new Date()).getTime() / 1000); // in UNIX format
557557

558558
const authHeader = req.headers.authorization;
559-
560559
const accessToken = authHeader.split(' ')[1];
561560

562561
if (!accessToken) {
@@ -579,7 +578,7 @@ class MsalExpressMiddleware {
579578
* https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens
580579
*/
581580
const checkIssuer = verifiedToken['iss'].includes(this.rawConfig.credentials.tenantId) ? true : false;
582-
const checkTimestamp = verifiedToken["iat"] < now && verifiedToken["exp"] > now ? true : false;
581+
const checkTimestamp = verifiedToken["iat"] <= now && verifiedToken["exp"] >= now ? true : false;
583582
const checkAudience = verifiedToken['aud'] === this.rawConfig.credentials.clientId || verifiedToken['aud'] === 'api://' + this.rawConfig.credentials.clientId ? true : false;
584583
const checkScope = this.rawConfig.protected.find(item => item.route === req.route.path).scopes
585584
.every(scp => verifiedToken['scp'].includes(scp));

0 commit comments

Comments
 (0)