Skip to content
This repository was archived by the owner on Jun 25, 2023. It is now read-only.

Commit 76902b3

Browse files
authored
Merge pull request #15 from Azure-Samples/dev
merge dev to main
2 parents 5cc736d + a7a1538 commit 76902b3

40 files changed

Lines changed: 4741 additions & 907 deletions

README.md

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

88
---
99

10-
This project illustrates a simple wrapper around **MSAL Node** [ConfidentialClientApplication](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_node.confidentialclientapplication.html) class in order to streamline routine authentication tasks such as login, logout and token acquisition, as well as securing routes and protecting resources.
10+
This project illustrates a simple wrapper around the [ConfidentialClientApplication](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_node.confidentialclientapplication.html) class of the [Microsoft Authentication Library for Node.js](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node#microsoft-authentication-library-for-node-msal-node) (MSAL Node), in order to streamline routine authentication tasks such as login, logout and token acquisition, as well as securing routes and controlling access.
1111

1212
This is an open source project. [Suggestions](https://github.com/Azure-Samples/msal-express-wrapper/issues/new) and [contributions](https://github.com/Azure-Samples/msal-express-wrapper/blob/dev/CONTRIBUTING.md) are welcome!
1313

@@ -21,6 +21,8 @@ This is an open source project. [Suggestions](https://github.com/Azure-Samples/m
2121
* (coming soon) Enable [Conditional Access](https://docs.microsoft.com/azure/active-directory/develop/v2-conditional-access-dev-guide) and [Zero-Trust](https://docs.microsoft.com/azure/active-directory/develop/developer-guide-conditional-access-authentication-context)
2222
* (coming soon) Run custom policies with [Azure AD B2C](https://docs.microsoft.com/azure/active-directory-b2c/overview)
2323

24+
> :warning: Protected web API scenarios are currently not supported.
25+
2426
## Prerequisites
2527

2628
* [Node](https://nodejs.org/en/) 12 LTS or higher
@@ -128,6 +130,12 @@ app.use(router(authProvider)); // use authProvider in routers downstream
128130
app.listen(SERVER_PORT, () => console.log(`Server is listening on port ${SERVER_PORT}!`));
129131
```
130132

133+
The wrapper stores user data on `req.session` variable. Below are some of the useful variables:
134+
135+
* `req.session.isAuthenticated`: indicates if user is currently authenticated (*boolean*)
136+
* `req.session.account`: MSAL.js account object containing useful information like ID token claims (see [AccountInfo](https://azuread.github.io/microsoft-authentication-library-for-js/ref/modules/_azure_msal_common.html#accountinfo))
137+
* `req.session.remoteResources.{resourceName}`: Contains parameters related to an Azure AD / Azure AD B2C protected resource, including raw access tokens (see [Resource](https://azure-samples.github.io/msal-express-wrapper/docs/modules.html#resource))
138+
131139
### Middleware
132140

133141
#### Authentication
@@ -254,7 +262,7 @@ Session support in this sample is provided by the [express-session](https://www.
254262

255263
MSAL Node has an in-memory cache by default. The demo app also features a [persistent cache plugin](./demo/App/utils/cachePlugin.js) in order to save the cache to disk. This plugin is not meant to be production-ready. As such, you might want to implement persistent caching using a 3rd party library like [redis](https://redis.io/).
256264

257-
## Resources
265+
## Information
258266

259267
* [Initializing a confidential client app with MSAL Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md)
260268
* [MSAL Node Configuration options](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md)

demo/App/app.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ async function main() {
3333
* Using express-session middleware. Be sure to familiarize yourself with available options
3434
* and set the desired options. Visit: https://www.npmjs.com/package/express-session
3535
*/
36-
3736
const sessionConfig = {
3837
secret: 'ENTER_YOUR_SECRET_HERE',
3938
resave: false,
@@ -50,13 +49,18 @@ async function main() {
5049

5150
app.use(session(sessionConfig));
5251

53-
const authProvider = await msalWrapper.AuthProvider.buildAsync(settings, cache);
54-
55-
app.use(authProvider.initialize());
56-
57-
app.use(router(authProvider));
58-
59-
app.listen(SERVER_PORT, () => console.log(`Server is listening on port ${SERVER_PORT}!`));
52+
try {
53+
// async building the wrapper as fetching credentials from key vault
54+
const authProvider = await msalWrapper.AuthProvider.buildAsync(settings, cache);
55+
56+
app.use(authProvider.initialize());
57+
58+
app.use(router(authProvider));
59+
60+
app.listen(SERVER_PORT, () => console.log(`Server is listening on port ${SERVER_PORT}!`));
61+
} catch (error) {
62+
console.log(error);
63+
}
6064
}
6165

6266
main();

demo/App/controllers/mainController.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ exports.getProfilePage = async(req, res, next) => {
2020
let profile;
2121

2222
try {
23-
profile = await fetchManager.callAPI(appSettings.remoteResources.graphAPI.endpoint, req.session.remoteResources["graphAPI"].accessToken);
23+
profile = await fetchManager.callAPI(appSettings.remoteResources.graphAPI.endpoint, req.session.remoteResources["graphAPI"].accessToken);
24+
res.render('profile', { isAuthenticated: req.session.isAuthenticated, profile: profile });
2425
} catch (error) {
25-
console.log(error)
26+
console.log(error);
27+
next(error);
2628
}
27-
28-
res.render('profile', { isAuthenticated: req.session.isAuthenticated, profile: profile });
2929
}
3030

3131
exports.getTenantPage = async(req, res, next) => {
3232
let tenant;
3333

3434
try {
35-
tenant = await fetchManager.callAPI(appSettings.remoteResources.armAPI.endpoint, req.session.remoteResources["armAPI"].accessToken);
35+
tenant = await fetchManager.callAPI(appSettings.remoteResources.armAPI.endpoint, req.session.remoteResources["armAPI"].accessToken);
36+
res.render('tenant', { isAuthenticated: req.session.isAuthenticated, tenant: tenant.value[0] });
3637
} catch (error) {
37-
console.log(error)
38+
console.log(error);
39+
next(error);
3840
}
39-
40-
res.render('tenant', { isAuthenticated: req.session.isAuthenticated, tenant: tenant.value[0] });
4141
}

demo/App/routes/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const mainController = require('../controllers/mainController');
33
const appSettings = require('../appSettings');
44

55
module.exports = (authProvider) => {
6+
67
// initialize router
78
const router = express.Router();
89

0 commit comments

Comments
 (0)