Skip to content

Commit e9d02b2

Browse files
committed
update 1-1
1 parent 3f87d7e commit e9d02b2

11 files changed

Lines changed: 767 additions & 503 deletions

File tree

1-Authentication/1-sign-in/App/app.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ const authConfig = require('./authConfig.js');
1313

1414
const SERVER_PORT = process.env.PORT || 4000;
1515

16-
async function main() {
16+
// initialize express
17+
const app = express();
1718

18-
// initialize express
19-
const app = express();
19+
async function main() {
2020

2121
/**
2222
* Using express-session middleware. Be sure to familiarize yourself with available options
@@ -28,7 +28,7 @@ async function main() {
2828
saveUninitialized: false,
2929
cookie: {
3030
httpOnly: true,
31-
secure: false, // set this to true on production
31+
secure: process.env.NODE_ENV === "production", // set this to true on production
3232
}
3333
}));
3434

@@ -46,7 +46,7 @@ async function main() {
4646
// instantiate the wrapper
4747
const authProvider = await WebAppAuthProvider.initialize(authConfig);
4848

49-
// initialize the wrapper
49+
// initialize the auth middleware before any route handlers
5050
app.use(authProvider.authenticate());
5151

5252
// app routes
@@ -57,8 +57,7 @@ async function main() {
5757
'/signin',
5858
(req, res, next) => {
5959
return req.authContext.login({
60-
postLoginRedirectUri: "/",
61-
postFailureRedirectUri: "/"
60+
postLoginRedirectUri: "/", // redirect here after login
6261
})(req, res, next);
6362
}
6463
);
@@ -67,24 +66,29 @@ async function main() {
6766
'/signout',
6867
(req, res, next) => {
6968
return req.authContext.logout({
70-
postLogoutRedirectUri: "/",
69+
postLogoutRedirectUri: "/", // redirect here after logout
7170
})(req, res, next);
7271
}
7372
);
7473

7574
// secure routes
7675
app.get('/id',
7776
authProvider.guard({
78-
forceLogin: true
77+
forceLogin: true // force user to login if not authenticated
7978
}),
8079
mainController.getIdPage
8180
);
8281

82+
/**
83+
* This error handler is needed to catch interaction_required errors thrown by MSAL.
84+
* Make sure to add it to your middleware chain after all your routers, but before any other
85+
* error handlers.
86+
*/
8387
app.use(authProvider.interactionErrorHandler());
8488

8589
app.listen(SERVER_PORT, () => console.log(`Msal Node Auth Code Sample app listening on port ${SERVER_PORT}!`));
8690
}
8791

8892
main();
8993

90-
module.exports = main;
94+
module.exports = app;

1-Authentication/1-sign-in/App/authConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const authConfig = {
2-
authOptions: {
2+
auth: {
33
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Info_Here",
44
clientId: "Enter_the_Application_Id_Here",
55
clientSecret: "Enter_the_Client_Secret_Here",
66
redirectUri: "/redirect",
77
},
8-
systemOptions: {
8+
system: {
99
loggerOptions: {
1010
loggerCallback: (logLevel, message, containsPii) => {
1111
if (containsPii) {

1-Authentication/1-sign-in/App/package-lock.json

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

1-Authentication/1-sign-in/App/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"ejs": "^3.1.8",
1616
"express": "^4.18.1",
1717
"express-session": "^1.17.3",
18-
"msal-node-wrapper": "file:../../../shared/msal-node-wrapper"
18+
"msal-node-wrapper": "file:../../../common/msal-node-wrapper"
1919
},
2020
"devDependencies": {
2121
"jest": "^27.0.6",

1-Authentication/1-sign-in/App/sample.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,54 @@ const request = require('supertest');
22
const { v4: uuidv4 } = require('uuid');
33

44
describe('Sanitize configuration object', () => {
5-
let appSettings;
5+
let authConfig;
66

77
beforeAll(() => {
8-
appSettings = require('./appSettings.js');
8+
authConfig = require('./authConfig.js');
99
});
1010

1111
it('should define the config object', () => {
12-
expect(appSettings).toBeDefined();
12+
expect(authConfig).toBeDefined();
1313
});
1414

1515
it('should not contain client Id', () => {
1616
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
17-
expect(regexGuid.test(appSettings.appCredentials.clientId)).toBe(false);
17+
expect(regexGuid.test(authConfig.auth.clientId)).toBe(false);
1818
});
1919

2020
it('should not contain tenant Id', () => {
2121
const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
22-
expect(regexGuid.test(appSettings.appCredentials.tenantId)).toBe(false);
22+
expect(regexGuid.test(authConfig.auth.tenantId)).toBe(false);
2323
});
2424

2525
it('should not contain client secret', () => {
2626
const regexSecret = /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{34,}$/;
27-
expect(regexSecret.test(appSettings.appCredentials.clientSecret)).toBe(false);
27+
expect(regexSecret.test(authConfig.auth.clientSecret)).toBe(false);
2828
});
2929
});
3030

3131
describe('Ensure pages served', () => {
3232

3333
let app;
34-
let appSettings;
34+
let authConfig;
3535
let randomGuid;
3636

3737
beforeAll(() => {
3838
process.env.NODE_ENV = 'test';
3939

40-
appSettings = require('./appSettings.js');
40+
authConfig = require('./authConfig.js');
4141
randomGuid = uuidv4();
4242

43-
appSettings.appCredentials.clientId = randomGuid;
44-
appSettings.appCredentials.tenantId = randomGuid;
43+
authConfig.auth.clientId = randomGuid;
44+
authConfig.auth.authority = randomGuid;
4545

4646
app = require('./app.js');
4747
});
4848

4949
it('should serve home page', async () => {
5050

5151
const res = await request(app)
52-
.get('/home');
52+
.get('/');
5353

5454
expect(res.statusCode).toEqual(200);
5555
});
Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
# Registering sample apps with the Microsoft identity platform and updating the configuration files using PowerShell
1+
# Registering sample apps with the Microsoft identity platform and updating configuration files using PowerShell
22

33
## Overview
44

55
### Quick summary
66

7-
1. On Windows, run PowerShell as **Administrator** and navigate to the root of the cloned directory
8-
1. In PowerShell run:
9-
10-
```PowerShell
11-
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
12-
```
13-
147
1. Run the script to create your Azure AD application and configure the code of the sample application accordingly.
158

169
```PowerShell
1710
cd .\AppCreationScripts\
18-
.\Configure.ps1
11+
.\Configure.ps1 -TenantId "your test tenant's id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'"
1912
```
2013

2114
### More details
@@ -28,9 +21,7 @@
2821
- [Run the script and start running](#run-the-script-and-start-running)
2922
- [Four ways to run the script](#four-ways-to-run-the-script)
3023
- [Option 1 (interactive)](#option-1-interactive)
31-
- [Option 2 (non-interactive)](#option-2-non-interactive)
32-
- [Option 3 (Interactive, but create apps in a specified tenant)](#option-3-Interactive-but-create-apps-in-a-specified-tenant)
33-
- [Option 4 (non-interactive, and create apps in a specified tenant)](#option-4-non-interactive-and-create-apps-in-a-specified-tenant)
24+
- [Option 2 (Interactive, but create apps in a specified tenant)](#option-3-Interactive-but-create-apps-in-a-specified-tenant)
3425
- [Running the script on Azure Sovereign clouds](#running-the-script-on-Azure-Sovereign-clouds)
3526

3627
## Goal of the provided scripts
@@ -42,7 +33,7 @@ This sample comes with two PowerShell scripts, which automate the creation of th
4233
These scripts are:
4334

4435
- `Configure.ps1` which:
45-
- creates Azure AD applications and their related objects (permissions, dependencies, secrets),
36+
- creates Azure AD applications and their related objects (permissions, dependencies, secrets, app roles),
4637
- changes the configuration files in the sample projects.
4738
- creates a summary file named `createdApps.html` in the folder from which you ran the script, and containing, for each Azure AD application it created:
4839
- the identifier of the application
@@ -51,6 +42,8 @@ These scripts are:
5142

5243
- `Cleanup.ps1` which cleans-up the Azure AD objects created by `Configure.ps1`. Note that this script does not revert the changes done in the configuration files, though. You will need to undo the change from source control (from Visual Studio, or from the command line using, for instance, `git reset`).
5344

45+
> :information_source: If the sample supports using certificates instead of client secrets, this folder will contain an additional set of scripts: `Configure-WithCertificates.ps1` and `Cleanup-WithCertificates.ps1`. You can use them in the same way to register app(s) that use certificates instead of client secrets.
46+
5447
### Usage pattern for tests and DevOps scenarios
5548

5649
The `Configure.ps1` will stop if it tries to create an Azure AD application which already exists in the tenant. For this, if you are using the script to try/test the sample, or in DevOps scenarios, you might want to run `Cleanup.ps1` just before `Configure.ps1`. This is what is shown in the steps below.
@@ -59,31 +52,26 @@ The `Configure.ps1` will stop if it tries to create an Azure AD application whic
5952

6053
### Pre-requisites
6154

55+
1. PowerShell 7 or later (see: [installing PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell))
6256
1. Open PowerShell (On Windows, press `Windows-R` and type `PowerShell` in the search window)
63-
1. Navigate to the root directory of the project.
64-
1. Until you change it, the default [Execution Policy](https:/go.microsoft.com/fwlink/?LinkID=135170) for scripts is usually `Restricted`. In order to run the PowerShell script you need to set the Execution Policy to `RemoteSigned`. You can set this just for the current PowerShell process by running the command:
6557

66-
```PowerShell
67-
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
68-
```
69-
70-
### (Optionally) install AzureAD PowerShell modules
58+
### (Optionally) install Microsoft.Graph.Applications PowerShell modules
7159

72-
The scripts install the required PowerShell module (AzureAD) for the current user if needed. However, if you want to install if for all users on the machine, you can follow the following steps:
60+
The scripts install the required PowerShell module (Microsoft.Graph.Applications) for the current user if needed. However, if you want to install if for all users on the machine, you can follow the following steps:
7361

74-
1. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
62+
1. If you have never done it already, in the PowerShell window, install the Microsoft.Graph.Applications PowerShell modules. For this:
7563

76-
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select **Run as administrator**).
64+
1. Open PowerShell
7765
2. Type:
7866

7967
```PowerShell
80-
Install-Module AzureAD
68+
Install-Module Microsoft.Graph.Applications
8169
```
8270
83-
or if you cannot be administrator on your machine, run:
71+
or if you want the modules to be installed for the current user only, run:
8472
8573
```PowerShell
86-
Install-Module AzureAD -Scope CurrentUser
74+
Install-Module Microsoft.Graph.Applications -Scope CurrentUser
8775
```
8876
8977
### Run the script and start running
@@ -100,14 +88,12 @@ The scripts install the required PowerShell module (AzureAD) for the current use
10088
10189
You're done!
10290
103-
### Four ways to run the script
91+
### Two ways to run the script
10492
10593
We advise four ways of running the script:
10694
10795
- Interactive: you will be prompted for credentials, and the scripts decide in which tenant to create the objects,
108-
- non-interactive: you will provide credentials, and the scripts decide in which tenant to create the objects,
10996
- Interactive in specific tenant: you will provide the tenant in which you want to create the objects and then you will be prompted for credentials, and the scripts will create the objects,
110-
- non-interactive in specific tenant: you will provide the tenant in which you want to create the objects and credentials, and the scripts will create the objects.
11197
11298
Here are the details on how to do this.
11399
@@ -118,20 +104,7 @@ Here are the details on how to do this.
118104
119105
Note that the script will choose the tenant in which to create the applications, based on the user. Also to run the `Cleanup.ps1` script, you will need to re-sign-in.
120106
121-
#### Option 2 (non-interactive)
122-
123-
When you know the identity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window
124-
125-
```PowerShell
126-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
127-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
128-
. .\Cleanup.ps1 -Credential $mycreds
129-
. .\Configure.ps1 -Credential $mycreds
130-
```
131-
132-
Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from **Azure Key Vault**.
133-
134-
#### Option 3 (Interactive, but create apps in a specified tenant)
107+
#### Option 2 (Interactive, but create apps in a specified tenant)
135108
136109
if you want to create the apps in a particular tenant, you can use the following option:
137110
@@ -147,18 +120,6 @@ $tenantId = "yourTenantIdGuid"
147120
. .\Configure.ps1 -TenantId $tenantId
148121
```
149122

150-
#### Option 4 (non-interactive, and create apps in a specified tenant)
151-
152-
This option combines option 2 and option 3: it creates the application in a specific tenant. See option 3 for the way to get the tenant Id. Then run:
153-
154-
```PowerShell
155-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
156-
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
157-
$tenantId = "yourTenantIdGuid"
158-
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
159-
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
160-
```
161-
162123
### Running the script on Azure Sovereign clouds
163124

164125
All the four options listed above can be used on any Azure Sovereign clouds. By default, the script targets `AzureCloud`, but it can be changed using the parameter `-AzureEnvironmentName`.
@@ -168,11 +129,10 @@ The acceptable values for this parameter are:
168129
- AzureCloud
169130
- AzureChinaCloud
170131
- AzureUSGovernment
171-
- AzureGermanyCloud
172132

173133
Example:
174134

175135
```PowerShell
176-
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
177-
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
136+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureUSGovernment"
137+
. .\Configure.ps1 -AzureEnvironmentName "AzureUSGovernment"
178138
```

0 commit comments

Comments
 (0)