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

Commit 8ca0fb0

Browse files
authored
Merge pull request #288 from Azure-Samples/update-acrs-sample
Remove express wrapper
2 parents 2367870 + ef158f7 commit 8ca0fb0

File tree

16 files changed

+475
-963
lines changed

16 files changed

+475
-963
lines changed

6-AdvancedScenarios/3-call-api-acrs/API/.env

-16
This file was deleted.

6-AdvancedScenarios/3-call-api-acrs/API/app.js

+27-56
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const methodOverride = require('method-override');
1111
const cors = require('cors');
1212
const path = require('path');
1313

14-
const MsIdExpress = require('microsoft-identity-express');
1514
const passport = require('passport');
1615
const BearerStrategy = require('passport-azure-ad').BearerStrategy;
1716

@@ -21,6 +20,8 @@ const routeGuard = require('./auth/routeGuard');
2120

2221
const mongoHelper = require('./utils/mongoHelper');
2322

23+
const { msalConfig, EXPRESS_SESSION_SECRET, CORS_ALLOWED_DOMAINS, API_REQUIRED_PERMISSION } = require('./authConfig');
24+
2425
const app = express();
2526

2627
app.set('views', path.join(__dirname, './views'));
@@ -39,49 +40,49 @@ app.use(express.json());
3940
* We need to enable CORS for client's domain in order to
4041
* expose www-authenticate header in response from the web API
4142
*/
42-
app.use(cors({
43-
origin: process.env.CORS_ALLOWED_DOMAINS, // replace with client domain
44-
exposedHeaders: "www-authenticate",
45-
}));
43+
app.use(
44+
cors({
45+
origin: CORS_ALLOWED_DOMAINS, // replace with client domain
46+
exposedHeaders: 'WWW-Authenticate',
47+
})
48+
);
4649

4750
/**
4851
* Using express-session middleware. Be sure to familiarize yourself with available options
4952
* and set them as desired. Visit: https://www.npmjs.com/package/express-session
5053
*/
51-
const sessionConfig = {
52-
secret: process.env.EXPRESS_SESSION_SECRET,
54+
const sessionConfig = {
55+
secret: EXPRESS_SESSION_SECRET,
5356
resave: false,
5457
saveUninitialized: false,
5558
cookie: {
5659
secure: false, // set this to true on production
57-
}
58-
}
60+
},
61+
};
5962

6063
if (app.get('env') === 'production') {
61-
6264
/**
6365
* In App Service, SSL termination happens at the network load balancers, so all HTTPS requests reach your app as unencrypted HTTP requests.
6466
* The line below is needed for getting the correct absolute URL for redirectUri configuration. For more information, visit:
6567
* https://docs.microsoft.com/azure/app-service/configure-language-nodejs?pivots=platform-linux#detect-https-session
6668
*/
6769

68-
app.set('trust proxy', 1) // trust first proxy e.g. App Service
69-
sessionConfig.cookie.secure = true // serve secure cookies
70+
app.set('trust proxy', 1); // trust first proxy e.g. App Service
71+
sessionConfig.cookie.secure = true; // serve secure cookies
7072
}
7173

7274
app.use(session(sessionConfig));
7375

7476
// =========== Initialize Passport ==============
75-
7677
const bearerOptions = {
77-
identityMetadata: `https://${process.env.AUTHORITY}/${process.env.TENANT_ID}/v2.0/.well-known/openid-configuration`,
78-
issuer: `https://${process.env.AUTHORITY}/${process.env.TENANT_ID}/v2.0`,
79-
clientID: process.env.CLIENT_ID,
80-
audience: process.env.CLIENT_ID, // audience is this application
78+
identityMetadata: `${msalConfig.auth.authority}/v2.0/.well-known/openid-configuration`,
79+
issuer: `${msalConfig.auth.authority}/v2.0`,
80+
clientID: msalConfig.auth.clientId,
81+
audience: msalConfig.auth.clientId, // audience is this application
8182
validateIssuer: true,
8283
passReqToCallback: false,
83-
loggingLevel: "info",
84-
scope: [process.env.API_REQUIRED_PERMISSION] // scope you set during app registration
84+
loggingLevel: 'info',
85+
scope: [API_REQUIRED_PERMISSION], // scope you set during app registration
8586
};
8687

8788
const bearerStrategy = new BearerStrategy(bearerOptions, (token, done) => {
@@ -94,52 +95,22 @@ app.use(passport.initialize());
9495
passport.use(bearerStrategy);
9596

9697
// protected api endpoints
97-
app.use('/api',
98+
app.use(
99+
'/api',
98100
passport.authenticate('oauth-bearer', { session: false }), // validate access tokens
99101
routeGuard, // check for auth context
100102
todolistRoutes
101103
);
102104

103-
// =========== Initialize MSAL Node Wrapper==============
104-
105-
const appSettings = {
106-
appCredentials: {
107-
clientId: process.env.CLIENT_ID,
108-
tenantId: process.env.TENANT_ID,
109-
clientSecret: process.env.CLIENT_SECRET,
110-
},
111-
authRoutes: {
112-
redirect: process.env.REDIRECT_URI, // enter the path component of your redirect URI
113-
unauthorized: "/admin/unauthorized" // the wrapper will redirect to this route in case of unauthorized access attempt
114-
},
115-
protectedResources: {
116-
// Microsoft Graph beta authenticationContextClassReference endpoint. For more information,
117-
// visit: https://docs.microsoft.com/en-us/graph/api/resources/authenticationcontextclassreference?view=graph-rest-beta
118-
msGraphAcrs: {
119-
endpoint: "https://graph.microsoft.com/beta/identity/conditionalAccess/policies",
120-
scopes: ["Policy.ReadWrite.ConditionalAccess", "Policy.Read.ConditionalAccess"]
121-
},
122-
}
123-
}
124-
125-
126-
// instantiate the wrapper
127-
const authProvider = new MsIdExpress.WebAppAuthClientBuilder(appSettings).build();
128-
129-
// initialize the wrapper
130-
app.use(authProvider.initialize());
131-
132-
// pass down to the authProvider instance to use in router
133-
app.use('/admin',
134-
adminRoutes(authProvider)
135-
);
105+
// admin routes
106+
app.use('/admin', adminRoutes);
136107

137108
const port = process.env.PORT || 5000;
138109

139110
mongoHelper.mongoConnect(() => {
140-
app.listen(port, () => {
141-
console.log('Listening on port ' + port);
142-
});
111+
app.listen(port, () => {
112+
console.log('Listening on port ' + port);
113+
});
143114
});
144115

145116
module.exports = app;

0 commit comments

Comments
 (0)