You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-Authentication/1-sign-in/README-incremental.md
+152-3Lines changed: 152 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -170,15 +170,164 @@ Were we successful in addressing your learning objective? Consider taking a mome
170
170
171
171
## About the code
172
172
173
-
### Configuration
173
+
## Initialization
174
+
175
+
`MsalNodeWrapper` class is initialized in the [routes/router.js](./App/routes/router.js). It expects two parameters, a JSON configuration object (see [auth.json](./auth.json)), and an optional cache plug-in (see [cachePlugin.js](./App/utils/cachePlugin.js)) if you wish to save your cache to disk. Otherwise, in-memory only cache is used.
176
+
177
+
Once initialized, `MsalNodeWrapper` middleware can be used in routes:
178
+
179
+
```javascript
180
+
constexpress=require('express');
181
+
182
+
constmsal=newMsalNodeWrapper(config, cache);
183
+
184
+
// initialize router
185
+
constrouter=express.Router();
186
+
187
+
router.get('/signin', msal.signIn);
188
+
router.get('/signout', msal.signOut);
189
+
router.get('/redirect', msal.handleRedirect);
190
+
```
191
+
192
+
Under the hood, the wrapper creates an **MSAL Node**[configuration object](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md) and initializes [msal.ConfidentialClientApplication](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/src/client/ConfidentialClientApplication.ts) by passing it.
The user clicks on the **sign-in** button and routes to `/signin`. `msal.signIn` middleware takes over. First it creates session variables:
207
+
208
+
```javascript
209
+
signIn= (req, res, next) => {
210
+
211
+
if (!req.session['authCodeRequest']) {
212
+
req.session.authCodeRequest= {
213
+
authority:"",
214
+
scopes: [],
215
+
state: {},
216
+
redirectUri:""
217
+
};
218
+
}
219
+
220
+
if (!req.session['tokenRequest']) {
221
+
req.session.tokenRequest= {
222
+
authority:"",
223
+
scopes: [],
224
+
state: {},
225
+
redirectUri:""
226
+
};
227
+
}
228
+
229
+
// current account id
230
+
req.session.homeAccountId="";
231
+
```
232
+
233
+
Then, creates and encodes a state object to pass with an authorization code request. The object is passed to the `state` parameter as a means of controlling the application flow. For more information, see [Pass custom state in authentication requests using MSAL.js](https://docs.microsoft.com/azure/active-directory/develop/msal-js-pass-custom-state-authentication-request).
234
+
235
+
```javascript
236
+
// sign-in as usual
237
+
let state =CryptoUtilities.base64EncodeUrl(
238
+
JSON.stringify({
239
+
stage:constants.AppStages.SIGN_IN,
240
+
path:req.route.path,
241
+
nonce:req.session.nonce
242
+
}));
243
+
244
+
// get url to sign user in (and consent to scopes needed for application)
245
+
this.getAuthCode(
246
+
this.msalConfig.auth.authority,
247
+
Object.values(constants.OIDCScopes), // pass standard openid scopes as permissions
248
+
state,
249
+
this.msalConfig.auth.redirectUri,
250
+
req, // express request object
251
+
res // express response object
252
+
);
253
+
```
254
+
255
+
Under the hood, `getAuthCode()` assigns request parameters to session, and calls the **MSAL Node** `getAuthCodeUrl()` API
After making an authorization code URL request, the user is redirected to the redirect route defined in the **Azure AD** app registration. Once redirected, the `handleRedirect` middleware takes over. It first checks for `nonce` parameter in state against *cross-site resource forgery* (csrf) attacks, and then for the current app stage. Then, using the `code` in query parameters, tokens are requested using the **MSAL Node** `acquireTokenByCode()` API, and the response is appended to the *express-session** variable.
Web apps (and confidential client apps in general) should validate ID Tokens. **MSAL Node** decodes the ID token. In `MsalNodeWrapper`, we add the ID token to session, and then validate it:
ID token validation should be validated according to the guide [ID Token validation](https://docs.microsoft.com/azure/active-directory/develop/id-tokens#validating-an-id_token). Implementation can vary, and it is the app developers responsibility.
178
315
179
316
### Sign-out
180
317
181
-
### National Clouds
318
+
We construct a logout URL following the [guide here](https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc#send-a-sign-out-request). Then, we destroy the current **express-session** and redirect the user to **sign-out endpoint**.
Copy file name to clipboardExpand all lines: 1-Authentication/1-sign-in/README.md
+152-3Lines changed: 152 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -174,15 +174,164 @@ Were we successful in addressing your learning objective? Consider taking a mome
174
174
175
175
## About the code
176
176
177
-
## Configuration
177
+
## Initialization
178
+
179
+
`MsalNodeWrapper` class is initialized in the [routes/router.js](./App/routes/router.js). It expects two parameters, a JSON configuration object (see [auth.json](./auth.json)), and an optional cache plug-in (see [cachePlugin.js](./App/utils/cachePlugin.js)) if you wish to save your cache to disk. Otherwise, in-memory only cache is used.
180
+
181
+
Once initialized, `MsalNodeWrapper` middleware can be used in routes:
182
+
183
+
```javascript
184
+
constexpress=require('express');
185
+
186
+
constmsal=newMsalNodeWrapper(config, cache);
187
+
188
+
// initialize router
189
+
constrouter=express.Router();
190
+
191
+
router.get('/signin', msal.signIn);
192
+
router.get('/signout', msal.signOut);
193
+
router.get('/redirect', msal.handleRedirect);
194
+
```
195
+
196
+
Under the hood, the wrapper creates an **MSAL Node**[configuration object](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md) and initializes [msal.ConfidentialClientApplication](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/src/client/ConfidentialClientApplication.ts) by passing it.
The user clicks on the **sign-in** button and routes to `/signin`. `msal.signIn` middleware takes over. First it creates session variables:
211
+
212
+
```javascript
213
+
signIn= (req, res, next) => {
214
+
215
+
if (!req.session['authCodeRequest']) {
216
+
req.session.authCodeRequest= {
217
+
authority:"",
218
+
scopes: [],
219
+
state: {},
220
+
redirectUri:""
221
+
};
222
+
}
223
+
224
+
if (!req.session['tokenRequest']) {
225
+
req.session.tokenRequest= {
226
+
authority:"",
227
+
scopes: [],
228
+
state: {},
229
+
redirectUri:""
230
+
};
231
+
}
232
+
233
+
// current account id
234
+
req.session.homeAccountId="";
235
+
```
236
+
237
+
Then, creates and encodes a state object to pass with an authorization code request. The object is passed to the `state` parameter as a means of controlling the application flow. For more information, see [Pass custom state in authentication requests using MSAL.js](https://docs.microsoft.com/azure/active-directory/develop/msal-js-pass-custom-state-authentication-request).
238
+
239
+
```javascript
240
+
// sign-in as usual
241
+
let state =CryptoUtilities.base64EncodeUrl(
242
+
JSON.stringify({
243
+
stage:constants.AppStages.SIGN_IN,
244
+
path:req.route.path,
245
+
nonce:req.session.nonce
246
+
}));
247
+
248
+
// get url to sign user in (and consent to scopes needed for application)
249
+
this.getAuthCode(
250
+
this.msalConfig.auth.authority,
251
+
Object.values(constants.OIDCScopes), // pass standard openid scopes as permissions
252
+
state,
253
+
this.msalConfig.auth.redirectUri,
254
+
req, // express request object
255
+
res // express response object
256
+
);
257
+
```
258
+
259
+
Under the hood, `getAuthCode()` assigns request parameters to session, and calls the **MSAL Node** `getAuthCodeUrl()` API
After making an authorization code URL request, the user is redirected to the redirect route defined in the **Azure AD** app registration. Once redirected, the `handleRedirect` middleware takes over. It first checks for `nonce` parameter in state against *cross-site resource forgery* (csrf) attacks, and then for the current app stage. Then, using the `code` in query parameters, tokens are requested using the **MSAL Node** `acquireTokenByCode()` API, and the response is appended to the *express-session** variable.
Web apps (and confidential client apps in general) should validate ID Tokens. **MSAL Node** decodes the ID token. In `MsalNodeWrapper`, we add the ID token to session, and then validate it:
ID token validation should be validated according to the guide [ID Token validation](https://docs.microsoft.com/azure/active-directory/develop/id-tokens#validating-an-id_token). Implementation can vary, and it is the app developers responsibility.
182
319
183
320
### Sign-out
184
321
185
-
### National Clouds
322
+
We construct a logout URL following the [guide here](https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc#send-a-sign-out-request). Then, we destroy the current **express-session** and redirect the user to **sign-out endpoint**.
0 commit comments