Skip to content

Commit db2394e

Browse files
committed
Updated packages in api, removed some code that was not needed
1 parent c82267d commit db2394e

File tree

5 files changed

+60
-50
lines changed

5 files changed

+60
-50
lines changed

1-Authentication/7-sign-in-express-mfa/Api/authConfig.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require("dotenv").config({ path: ".env.dev" });
77

88
const TENANT_SUBDOMAIN =
99
process.env.TENANT_SUBDOMAIN || "Enter_the_Tenant_Subdomain_Here";
10-
const TENANT_ID = process.env.TENANT_ID || "Enter_the_Tenant_Subdomain_Here";
10+
const TENANT_ID = process.env.TENANT_ID || "Enter_the_Tenant_ID_Here";
1111
const REDIRECT_URI =
1212
process.env.REDIRECT_URI || "http://localhost:3000/auth/redirect";
1313
const POST_LOGOUT_REDIRECT_URI =
@@ -20,10 +20,12 @@ const POST_LOGOUT_REDIRECT_URI =
2020
*/
2121
const msalConfig = {
2222
auth: {
23-
clientId: process.env.CLIENT_ID || "Enter_the_Application_Id_Here", // 'Application (client) ID' of app registration in Azure portal - this value is a GUID
23+
clientId:
24+
process.env.CLIENT_ID ||
25+
"Enter_the_Edit_Profile_Service_Application_Id_Here", // 'Application (client) ID' of the Edit_Profile Service App registration in Microsoft Entra admin center - this value is a GUID
2426
authority:
25-
process.env.AUTHORITY || `https://${TENANT_SUBDOMAIN}.ciamlogin.com/`, // Replace the placeholder with your tenant name
26-
clientSecret: process.env.CLIENT_SECRET || "Enter_the_Client_Secret_Here", // Client secret generated from the app registration in Azure portal
27+
process.env.AUTHORITY || `https://${TENANT_SUBDOMAIN}.ciamlogin.com/`, // Replace the placeholder with your external tenant name
28+
clientSecret: process.env.CLIENT_SECRET || "Enter_the_Client_Secret_Here", // Client secret generated from the app registration in Microsoft Entra admin center
2729
},
2830
system: {
2931
loggerOptions: {
@@ -41,12 +43,8 @@ const GRAPH_API_ENDPOINT = process.env.GRAPH_API_ENDPOINT || "graph_end_point";
4143
// https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=http
4244
const GRAPH_ME_ENDPOINT = GRAPH_API_ENDPOINT + "v1.0/me";
4345

44-
const mfaProtectedResourceScope =
45-
process.env.MFA_PROTECTED_SCOPE || "Add_your_protected_scope_here";
46-
4746
module.exports = {
4847
msalConfig,
49-
mfaProtectedResourceScope,
5048
REDIRECT_URI,
5149
POST_LOGOUT_REDIRECT_URI,
5250
TENANT_SUBDOMAIN,

1-Authentication/7-sign-in-express-mfa/Api/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ app.post("/updateUserInfo", async (req, res) => {
4343
fetch(GRAPH_ME_ENDPOINT, accessToken, "PATCH", req.body)
4444
.then((response) => {
4545
if (response.status === 204) {
46-
res.status(204);
46+
res.status(response.status);
4747
res.json({ message: "Success" });
4848
} else {
49-
res.status(500);
50-
res.json({ message: "Failed" });
49+
res.status(502);
50+
res.json({ message: "Failed, " + response.body });
5151
}
5252
})
5353
.catch((error) => {
54-
res.status(400);
55-
res.json({ message: "Failed" });
54+
res.status(502);
55+
res.json({ message: "Failed, " + error });
5656
});
5757
} catch (err) {
58-
res.json({ message: "Failed" });
58+
res.json({ message: "Failed, " + err });
5959
}
6060
});
6161

1-Authentication/7-sign-in-express-mfa/Api/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
"author": "Microsoft",
99
"license": "MIT",
1010
"dependencies": {
11-
"@azure/msal-node": "^2.9.2",
12-
"axios": "^1.0.0",
11+
"@azure/msal-node": "^2.13.1",
12+
"axios": "^1.7.7",
1313
"cookie-parser": "^1.4.6",
14-
"dotenv": "^16.0.3",
15-
"express": "^4.18.1",
16-
"express-session": "^1.17.3",
14+
"dotenv": "^16.4.5",
15+
"express": "^4.21.0",
16+
"express-session": "^1.18.0",
1717
"http-errors": "^2.0.0",
1818
"uuid": "^10.0.0"
1919
}

1-Authentication/7-sign-in-express-mfa/App/authConfig.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,52 @@
33
* Licensed under the MIT License.
44
*/
55

6-
require('dotenv').config({ path: '.env.dev' });
6+
require("dotenv").config({ path: ".env.dev" });
77

8-
const TENANT_SUBDOMAIN = process.env.TENANT_SUBDOMAIN || 'Enter_the_Tenant_Subdomain_Here';
9-
const REDIRECT_URI = process.env.REDIRECT_URI || 'http://localhost:3000/auth/redirect';
10-
const POST_LOGOUT_REDIRECT_URI = process.env.POST_LOGOUT_REDIRECT_URI || 'http://localhost:3000';
8+
const TENANT_SUBDOMAIN =
9+
process.env.TENANT_SUBDOMAIN || "Enter_the_Tenant_Subdomain_Here";
10+
const REDIRECT_URI =
11+
process.env.REDIRECT_URI || "http://localhost:3000/auth/redirect";
12+
const POST_LOGOUT_REDIRECT_URI =
13+
process.env.POST_LOGOUT_REDIRECT_URI || "http://localhost:3000";
1114

1215
/**
1316
* Configuration object to be passed to MSAL instance on creation.
1417
* For a full list of MSAL Node configuration parameters, visit:
1518
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md
1619
*/
1720
const msalConfig = {
18-
auth: {
19-
clientId: process.env.CLIENT_ID || 'Enter_the_Application_Id_Here', // 'Application (client) ID' of app registration in Azure portal - this value is a GUID
20-
authority: process.env.AUTHORITY || `https://${TENANT_SUBDOMAIN}.ciamlogin.com/`, // Replace the placeholder with your tenant name
21-
clientSecret: process.env.CLIENT_SECRET || 'Enter_the_Client_Secret_Here', // Client secret generated from the app registration in Azure portal
22-
},
23-
system: {
24-
loggerOptions: {
25-
loggerCallback(loglevel, message, containsPii) {
26-
console.log(message);
27-
},
28-
piiLoggingEnabled: false,
29-
logLevel: 'Info',
30-
},
21+
auth: {
22+
clientId: process.env.CLIENT_ID || "Enter_the_Application_Id_Here", // 'Application (client) ID' of app registration in Azure portal - this value is a GUID
23+
authority:
24+
process.env.AUTHORITY || `https://${TENANT_SUBDOMAIN}.ciamlogin.com/`, // Replace the placeholder with your tenant name
25+
clientSecret: process.env.CLIENT_SECRET || "Enter_the_Client_Secret_Here", // Client secret generated from the app registration in Azure portal
26+
},
27+
system: {
28+
loggerOptions: {
29+
loggerCallback(loglevel, message, containsPii) {
30+
console.log(message);
31+
},
32+
piiLoggingEnabled: false,
33+
logLevel: "Info",
3134
},
35+
},
3236
};
3337

3438
const GRAPH_API_ENDPOINT = process.env.GRAPH_API_ENDPOINT || "graph_end_point";
3539
// Refers to the user that is single user singed in.
3640
// https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=http
3741
const GRAPH_ME_ENDPOINT = GRAPH_API_ENDPOINT + "v1.0/me";
3842

39-
const mfaProtectedResourceScope = process.env.MFA_PROTECTED_SCOPE || 'Add_your_protected_scope_here';
43+
const editProfileScope =
44+
process.env.EDIT_PROFILE_SCOPE || "Add_your_protected_scope_here";
4045

4146
module.exports = {
42-
msalConfig,
43-
mfaProtectedResourceScope,
44-
REDIRECT_URI,
45-
POST_LOGOUT_REDIRECT_URI,
46-
TENANT_SUBDOMAIN,
47-
GRAPH_API_ENDPOINT,
48-
GRAPH_ME_ENDPOINT,
47+
msalConfig,
48+
editProfileScope,
49+
REDIRECT_URI,
50+
POST_LOGOUT_REDIRECT_URI,
51+
TENANT_SUBDOMAIN,
52+
GRAPH_API_ENDPOINT,
53+
GRAPH_ME_ENDPOINT,
4954
};

1-Authentication/7-sign-in-express-mfa/App/routes/users.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ const router = express.Router();
88
const authProvider = require("../auth/AuthProvider");
99

1010
var { fetch } = require("../fetch");
11-
const {
12-
GRAPH_ME_ENDPOINT,
13-
mfaProtectedResourceScope,
14-
} = require("../authConfig");
11+
const { GRAPH_ME_ENDPOINT, editProfileScope } = require("../authConfig");
1512

1613
// custom middleware to check auth state
1714
function isAuthenticated(req, res, next) {
@@ -39,6 +36,11 @@ router.get(
3936
GRAPH_ME_ENDPOINT,
4037
req.session.accessToken,
4138
);
39+
if (!graphResponse.ok) {
40+
return res
41+
.status(graphResponse.status)
42+
.send("Failed to fetch profile data");
43+
}
4244
res.render("gatedUpdateProfile", {
4345
profile: graphResponse,
4446
});
@@ -49,14 +51,19 @@ router.get(
4951
"/updateProfile",
5052
isAuthenticated, // check if user is authenticated
5153
authProvider.getToken(
52-
["User.Read", mfaProtectedResourceScope],
54+
["User.Read", editProfileScope],
5355
"http://localhost:3000/users/updateProfile",
5456
),
5557
async function (req, res, next) {
5658
const graphResponse = await fetch(
5759
GRAPH_ME_ENDPOINT,
5860
req.session.accessToken,
5961
);
62+
if (!graphResponse.ok) {
63+
return res
64+
.status(graphResponse.status)
65+
.send("Failed to fetch profile data");
66+
}
6067
res.render("updateProfile", {
6168
profile: graphResponse,
6269
});
@@ -66,7 +73,7 @@ router.get(
6673
router.post(
6774
"/update",
6875
isAuthenticated,
69-
authProvider.getToken([mfaProtectedResourceScope]),
76+
authProvider.getToken([editProfileScope]),
7077
async function (req, res, next) {
7178
try {
7279
if (!!req.body) {

0 commit comments

Comments
 (0)