@@ -11,7 +11,6 @@ const methodOverride = require('method-override');
11
11
const cors = require ( 'cors' ) ;
12
12
const path = require ( 'path' ) ;
13
13
14
- const MsIdExpress = require ( 'microsoft-identity-express' ) ;
15
14
const passport = require ( 'passport' ) ;
16
15
const BearerStrategy = require ( 'passport-azure-ad' ) . BearerStrategy ;
17
16
@@ -21,6 +20,8 @@ const routeGuard = require('./auth/routeGuard');
21
20
22
21
const mongoHelper = require ( './utils/mongoHelper' ) ;
23
22
23
+ const { msalConfig, EXPRESS_SESSION_SECRET , CORS_ALLOWED_DOMAINS , API_REQUIRED_PERMISSION } = require ( './authConfig' ) ;
24
+
24
25
const app = express ( ) ;
25
26
26
27
app . set ( 'views' , path . join ( __dirname , './views' ) ) ;
@@ -39,49 +40,49 @@ app.use(express.json());
39
40
* We need to enable CORS for client's domain in order to
40
41
* expose www-authenticate header in response from the web API
41
42
*/
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
+ ) ;
46
49
47
50
/**
48
51
* Using express-session middleware. Be sure to familiarize yourself with available options
49
52
* and set them as desired. Visit: https://www.npmjs.com/package/express-session
50
53
*/
51
- const sessionConfig = {
52
- secret : process . env . EXPRESS_SESSION_SECRET ,
54
+ const sessionConfig = {
55
+ secret : EXPRESS_SESSION_SECRET ,
53
56
resave : false ,
54
57
saveUninitialized : false ,
55
58
cookie : {
56
59
secure : false , // set this to true on production
57
- }
58
- }
60
+ } ,
61
+ } ;
59
62
60
63
if ( app . get ( 'env' ) === 'production' ) {
61
-
62
64
/**
63
65
* In App Service, SSL termination happens at the network load balancers, so all HTTPS requests reach your app as unencrypted HTTP requests.
64
66
* The line below is needed for getting the correct absolute URL for redirectUri configuration. For more information, visit:
65
67
* https://docs.microsoft.com/azure/app-service/configure-language-nodejs?pivots=platform-linux#detect-https-session
66
68
*/
67
69
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
70
72
}
71
73
72
74
app . use ( session ( sessionConfig ) ) ;
73
75
74
76
// =========== Initialize Passport ==============
75
-
76
77
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
81
82
validateIssuer : true ,
82
83
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
85
86
} ;
86
87
87
88
const bearerStrategy = new BearerStrategy ( bearerOptions , ( token , done ) => {
@@ -94,52 +95,22 @@ app.use(passport.initialize());
94
95
passport . use ( bearerStrategy ) ;
95
96
96
97
// protected api endpoints
97
- app . use ( '/api' ,
98
+ app . use (
99
+ '/api' ,
98
100
passport . authenticate ( 'oauth-bearer' , { session : false } ) , // validate access tokens
99
101
routeGuard , // check for auth context
100
102
todolistRoutes
101
103
) ;
102
104
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 ) ;
136
107
137
108
const port = process . env . PORT || 5000 ;
138
109
139
110
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
+ } ) ;
143
114
} ) ;
144
115
145
116
module . exports = app ;
0 commit comments