Skip to content

Commit 2e1238a

Browse files
committed
review changes
1 parent 53d29ed commit 2e1238a

36 files changed

Lines changed: 3606 additions & 93 deletions

1-Authentication/1-sign-in/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ Locate the root of the sample folder. Then:
150150

151151
1. Open your browser and navigate to `http://localhost:4000`.
152152
1. Select the **sign-in** button on the top right corner.
153+
1. Once signed in, select the **ID** button to see some of the claims in your ID token.
153154

154155
![Screenshot](./ReadmeFiles/screenshot.png)
155156

1-Authentication/2-sign-in-b2c/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
## Overview
1717

18-
This sample demonstrates a Node.js & Express web application that authenticates users against Azure AD, with the help of [Microsoft Authentication Library for Node.js](https://aka.ms/msalnode) (MSAL Node). In doing so, it illustrates authentication concepts such as [OpenID scopes](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes), [ID Tokens](https://docs.microsoft.com/azure/active-directory-b2c/openid-connect), [ID Token validation](https://docs.microsoft.com/azure/active-directory-b2c/openid-connect#validate-the-id-token), [user-flows](https://docs.microsoft.com/azure/active-directory-b2c/user-flow-overview) and more.
18+
This sample demonstrates a Node.js & Express web application that authenticates users against Azure AD B2C, with the help of [Microsoft Authentication Library for Node.js](https://aka.ms/msalnode) (MSAL Node). In doing so, it illustrates authentication concepts such as [OpenID scopes](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#openid-connect-scopes), [ID Tokens](https://docs.microsoft.com/azure/active-directory-b2c/openid-connect), [ID Token validation](https://docs.microsoft.com/azure/active-directory-b2c/openid-connect#validate-the-id-token), [user-flows](https://docs.microsoft.com/azure/active-directory-b2c/user-flow-overview) and more.
1919

2020
## Scenario
2121

@@ -108,7 +108,7 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
108108
109109
1. Open the `appSettings.json` file.
110110
1. Find the key `clientId` and replace the existing value with the application ID (clientId) of `msal-node-webapp` app copied from the Azure portal.
111-
1. Find the key `tenantId` and replace the existing value with your Azure AD tenant ID.
111+
1. Find the key `tenantId` and replace the existing value with your Azure AD B2C tenant ID.
112112
1. Find the key `clientSecret` and replace the existing value with the key you saved during the creation of `msal-node-webapp` copied from the Azure portal.
113113
1. Find the key `redirectUri` and replace the existing value with the Redirect URI for `msal-node-webapp`. (by default `http://localhost:4000`).
114114
1. Find the key `postLogoutRedirectUri` and replace the existing value with the base address of `msal-node-webapp` (by default `http://localhost:4000`).
@@ -125,7 +125,8 @@ Locate the root of the sample folder. Then:
125125
## Explore the sample
126126

127127
1. Open your browser and navigate to `http://localhost:4000`.
128-
1. Click the sign-in button on the top right corner.
128+
1. Click the **sign-in** button on the top right corner.
129+
1. Once signed in, select the **ID** button to see some of the claims in your ID token.
129130

130131
![Screenshot](./ReadmeFiles/screenshot.png)
131132

2-Authorization-I/1-call-graph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ The rest of the **key-value** pairs are for resources/APIs that you would like t
159159
"nameOfYourResource": {
160160
"callingPageRoute": "/<route_where_this_resource_will_be_called_from>",
161161
"endpoint": "<uri_coordinates_of_the_resource>",
162-
"scopes": ["scope1_of_the_resource", "scope1_of_the_resource", "..."]
162+
"scopes": ["scope1_of_the_resource", "scope2_of_the_resource", "..."]
163163
},
164164
```
165165

3-Authorization-II/2-call-api-b2c/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
162162
163163
1. Open the `WebApp\appSettings.json` file.
164164
1. Find the key `clientId` and replace the existing value with the application ID (clientId) of `msal-node-webapp` app copied from the Azure portal.
165-
1. Find the key `tenantId` and replace the existing value with your Azure AD tenant ID.
165+
1. Find the key `tenantId` and replace the existing value with your Azure AD B2C tenant ID.
166166
1. Find the key `clientSecret` and replace the existing value with the key you saved during the creation of `msal-node-webapp` copied from the Azure portal.
167167
1. Find the key `redirectUri` and replace the existing value with the Redirect URI for `msal-node-webapp`. (by default `http://localhost:4000/`).
168168
1. Find the key `postLogoutRedirectUri` and replace the existing value with the base address of `msal-node-webapp` (by default `http://localhost:4000/`).

4-Deployment/README.md

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

1414
## Overview
1515

16-
This sample demonstrates how to deploy a Node.js & Express web application coupled with a Node.js & Express web API to **Azure Cloud** using the [Azure App Service](https://docs.microsoft.com/azure/app-service/). To do so, we will use the [same code sample from Chapter 3](../3-Authorization-II/1-call-api). One of the principles of security is to place credentials like secrets and certificates out of your code and use it in a manner that allows them to be replaced or rotated without incurring a downtime. To do this, we will make use [Azure Key Vault](https://docs.microsoft.com/azure/key-vault/general/about-keys-secrets-certificates) to store client secrets.
16+
This sample demonstrates how to deploy a Node.js & Express web application coupled with a Node.js & Express web API to **Azure Cloud** using the [Azure App Service](https://docs.microsoft.com/azure/app-service/). To do so, we will use the [same code sample from Chapter 3](../3-Authorization-II/1-call-api). One of the principles of security is to place credentials like secrets and certificates out of your code and use it in a manner that allows them to be replaced or rotated without incurring a downtime. To do this, we will make use of the [Azure Key Vault](https://docs.microsoft.com/azure/key-vault/general/about-keys-secrets-certificates) to store client secrets.
1717

1818
> :information_source: The steps below apply similarly to B2C applications, for instance the [B2C sample from Chapter 3](../3-Authorization-II/2-call-api-b2c)
1919
@@ -30,7 +30,7 @@ This sample demonstrates how to deploy a Node.js & Express web application coupl
3030
- [VS Code Azure Tools Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-node-azure-pack) extension is recommended for interacting with **Azure** through VS Code interface.
3131
- An **Azure AD** tenant. For more information, see: [How to get an Azure AD tenant](https://azure.microsoft.com/documentation/articles/active-directory-howto-tenant/)
3232
- A user account in your **Azure AD** tenant.
33-
- An **Azure subscription**. This sample uses **Azure App Service**.
33+
- An **Azure subscription**. This sample uses **Azure App Service** and **Azure Key Vault**.
3434

3535
## Setup
3636

4-Deployment/WebApp/App/routes/router.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const express = require('express');
44
const identity = require("@azure/identity");
55
const keyvaultSecret = require('@azure/keyvault-secrets');
66

7-
const msalWrapper = require('msal-express-wrapper');
7+
// importing from Packages folder
8+
const msalWrapper = require('../../Package/index');
89

910
const mainController = require('../controllers/mainController');
1011

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Request, Response, NextFunction } from 'express';
2+
import { ConfidentialClientApplication, Configuration, ICachePlugin, CryptoProvider } from '@azure/msal-node';
3+
import { TokenValidator } from './TokenValidator';
4+
import { UrlUtils } from './UrlUtils';
5+
import { AppSettings } from './Types';
6+
/**
7+
* A simple wrapper around MSAL Node ConfidentialClientApplication object.
8+
* It offers a collection of middleware and utility methods that automate
9+
* basic authentication and authorization tasks in Express MVC web apps.
10+
*
11+
* You must have express and express-sessions package installed. Middleware
12+
* here can be used with express sessions in route controllers.
13+
*
14+
* Session variables accessible are as follows:
15+
* req.session.isAuthenticated: boolean
16+
* req.session.isAuthorized: boolean
17+
* req.session.account: AccountInfo
18+
* req.session.<resourceName>.accessToken: string
19+
*/
20+
export declare class AuthProvider {
21+
appSettings: AppSettings;
22+
msalConfig: Configuration;
23+
urlUtils: UrlUtils;
24+
cryptoProvider: CryptoProvider;
25+
tokenValidator: TokenValidator;
26+
msalClient: ConfidentialClientApplication;
27+
/**
28+
* @param {JSON} appSettings
29+
* @param {ICachePlugin} cache: cachePlugin
30+
*/
31+
constructor(appSettings: AppSettings, cache?: ICachePlugin);
32+
/**
33+
* Initiate sign in flow
34+
* @param {Request} req: express request object
35+
* @param {Response} res: express response object
36+
* @param {NextFunction} next: express next function
37+
*/
38+
signIn: (req: Request, res: Response, next: NextFunction) => void;
39+
/**
40+
* Initiate sign out and clean the session
41+
* @param {Request} req: express request object
42+
* @param {Response} res: express response object
43+
* @param {NextFunction} next: express next function
44+
*/
45+
signOut: (req: Request, res: Response, next: NextFunction) => void;
46+
/**
47+
* Middleware that handles redirect depending on request state
48+
* There are basically 2 stages: sign-in and acquire token
49+
* @param {Request} req: express request object
50+
* @param {Response} res: express response object
51+
* @param {NextFunction} next: express next function
52+
*/
53+
handleRedirect: (req: Request, res: Response, next: NextFunction) => Promise<void>;
54+
/**
55+
* Middleware that gets tokens and calls web APIs
56+
* @param {Object} req: express request object
57+
* @param {Object} res: express response object
58+
* @param {Function} next: express next
59+
*/
60+
getToken: (req: Request, res: Response, next: NextFunction) => Promise<void>;
61+
/**
62+
* Check if authenticated in session
63+
* @param {Object} req: express request object
64+
* @param {Object} res: express response object
65+
* @param {Function} next: express next
66+
*/
67+
isAuthenticated: (req: Request, res: Response, next: NextFunction) => void | Response;
68+
/**
69+
* Receives access token in req authorization header
70+
* and validates it using the jwt.verify
71+
* @param {Object} req: express request object
72+
* @param {Object} res: express response object
73+
* @param {Function} next: express next
74+
*/
75+
isAuthorized: (req: Request, res: Response, next: NextFunction) => Promise<void | Response>;
76+
/**
77+
* This method is used to generate an auth code request
78+
* @param {Object} req: express request object
79+
* @param {Object} res: express response object
80+
* @param {NextFunction} next: express next function
81+
* @param {AuthCodeParams} params: modifies auth code request url
82+
*/
83+
private getAuthCode;
84+
/**
85+
* Util method to get the resource name for a given callingPageRoute (appSettings.json)
86+
* @param {string} path: /path string that the resource is associated with
87+
*/
88+
private getResourceName;
89+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Configuration, ICachePlugin } from '@azure/msal-node';
2+
import { AppSettings } from './Types';
3+
export declare class ConfigurationUtils {
4+
/**
5+
* Validates the fields in the custom JSON configuration file
6+
* @param {JSON} config: configuration file
7+
*/
8+
static validateAppSettings: (config: AppSettings) => void;
9+
/**
10+
* Maps the custom JSON configuration file to configuration
11+
* object expected by MSAL Node ConfidentialClientApplication
12+
* @param {JSON} config: configuration file
13+
* @param {Object} cachePlugin: passed during initialization
14+
*/
15+
static getMsalConfiguration: (config: AppSettings, cachePlugin?: ICachePlugin) => Configuration;
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Basic authentication stages used to determine
3+
* appropriate action after redirect occurs
4+
*/
5+
export declare const AppStages: {
6+
SIGN_IN: string;
7+
SIGN_OUT: string;
8+
ACQUIRE_TOKEN: string;
9+
};
10+
/**
11+
* Allowed fields in JSON configuration file
12+
*/
13+
export declare const JsonConfiguration: {
14+
CREDENTIALS: string;
15+
CONFIGURATION: string;
16+
RESOURCES: string;
17+
POLICIES: string;
18+
PROTECTED: string;
19+
};
20+
/**
21+
* Various error constants
22+
*/
23+
export declare const ErrorMessages: {
24+
NOT_PERMITTED: string;
25+
INVALID_TOKEN: string;
26+
CANNOT_DETERMINE_APP_STAGE: string;
27+
NONCE_MISMATCH: string;
28+
INTERACTION_REQUIRED: string;
29+
TOKEN_NOT_FOUND: string;
30+
TOKEN_NOT_DECODED: string;
31+
TOKEN_NOT_VERIFIED: string;
32+
KEYS_NOT_OBTAINED: string;
33+
STATE_NOT_FOUND: string;
34+
};
35+
/**
36+
* For more information, visit: https://login.microsoftonline.com/error
37+
*/
38+
export declare const ErrorCodes: {
39+
65001: string;
40+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Configuration } from '@azure/msal-node';
2+
import { AppSettings } from './Types';
3+
export declare class TokenValidator {
4+
appSettings: AppSettings;
5+
msalConfig: Configuration;
6+
constructor(appSettings: AppSettings, msalConfig: Configuration);
7+
/**
8+
* Validates the id token for a set of claims
9+
* @param {Object} idTokenClaims: decoded id token claims
10+
*/
11+
validateIdToken: (idTokenClaims: any) => boolean;
12+
/**
13+
* Validates the access token for signature and against a predefined set of claims
14+
* @param {string} accessToken: raw JWT token
15+
* @param {string} protectedRoute: used for checking scope
16+
*/
17+
validateAccessToken: (accessToken: any, protectedRoute: any) => Promise<boolean>;
18+
/**
19+
* Fetches signing keys of an access token
20+
* from the authority discovery endpoint
21+
* @param {string} header
22+
*/
23+
private getSigningKeys;
24+
}

0 commit comments

Comments
 (0)