Skip to content

Commit 32464cf

Browse files
committed
review changes
1 parent 0c280c8 commit 32464cf

7 files changed

Lines changed: 92 additions & 2 deletions

File tree

4-AccessControl/1-app-roles/App/controllers/todolistController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const lowdb = require('lowdb');
22
const FileSync = require('lowdb/adapters/FileSync');
33
const adapter = new FileSync('./data/db.json');
44
const db = lowdb(adapter);
5+
const { nanoid } = require('nanoid')
56

67
exports.getTodos = (req, res) => {
78
const owner = req.session.account.idTokenClaims['preferred_username'];
@@ -14,7 +15,13 @@ exports.getTodos = (req, res) => {
1415
}
1516

1617
exports.postTodo = (req, res) => {
17-
db.get('todos').push(req.body).write();
18+
const newTodo = {
19+
id: nanoid(),
20+
name: req.body.name,
21+
owner: req.session.account.idTokenClaims['preferred_username'],
22+
};
23+
24+
db.get('todos').push(newTodo).write();
1825
res.redirect('/todolist');
1926
}
2027

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"todos": []
2+
"todos": [
3+
]
34
}

4-AccessControl/1-app-roles/App/views/dashboard.ejs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
<p>You can only see this page if you are in <strong>TaskAdmin</strong> role</p>
1717
</div>
1818

19+
<div class="table-area-div">
20+
<table class="table">
21+
<thead class="thead-dark">
22+
<tr>
23+
<th scope="col">Task</th>
24+
<th scope="col">Owner</th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
<% for (let i=0; i < todos.length; i++) { %>
29+
<tr>
30+
<td><%= todos[i]['name'] %></td>
31+
<td><%= todos[i]['owner'] %></td>
32+
</tr>
33+
<% } %>
34+
</tbody>
35+
</table>
36+
</div>
37+
1938
<%- include('includes/footer'); %>
2039

2140
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

4-AccessControl/1-app-roles/App/views/todolist.ejs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@
1616
<p>You can see this page if you are in <strong>TaskUser</strong> or <strong>TaskAdmin</strong> role</p>
1717
</div>
1818

19+
<div class="table-area-div">
20+
<form action='/todolist' method='POST'>
21+
<input type='text' name='name'>
22+
<button type='submit'>Add</button>
23+
</form>
24+
</div>
25+
26+
<div class="table-area-div">
27+
<table class="table">
28+
<tbody>
29+
<% for (let i=0; i < todos.length; i++) { %>
30+
<tr>
31+
<td><%= todos[i]['name'] %></td>
32+
<td>
33+
<form action='/todolist' method='POST'>
34+
<input type='hidden' name='id' value='<%= todos[i]["id"] %>'>
35+
<button type='submit'>Delete</button>
36+
</form>
37+
</td>
38+
</tr>
39+
<% } %>
40+
</tbody>
41+
</table>
42+
</div>
43+
1944
<%- include('includes/footer'); %>
2045

2146
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

4-AccessControl/1-app-roles/AppCreationScripts/Configure.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ param(
2121
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
2222
#>
2323

24+
# Create a password that can be used as an application key
25+
Function ComputePassword
26+
{
27+
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
28+
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
29+
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
30+
$aesManaged.BlockSize = 128
31+
$aesManaged.KeySize = 256
32+
$aesManaged.GenerateKey()
33+
return [System.Convert]::ToBase64String($aesManaged.Key)
34+
}
35+
36+
# Create an application key
37+
# See https://www.sabin.io/blog/adding-an-azure-active-directory-application-and-key-using-powershell/
38+
Function CreateAppKey([DateTime] $fromDate, [double] $durationInMonths, [string]$pw)
39+
{
40+
$endDate = $fromDate.AddMonths($durationInMonths);
41+
$keyId = (New-Guid).ToString();
42+
$key = New-Object Microsoft.Open.AzureAD.Model.PasswordCredential
43+
$key.StartDate = $fromDate
44+
$key.EndDate = $endDate
45+
$key.Value = $pw
46+
$key.KeyId = $keyId
47+
return $key
48+
}
49+
2450
Function UpdateLine([string] $line, [string] $value)
2551
{
2652
$index = $line.IndexOf('=')
@@ -148,10 +174,16 @@ Function ConfigureApplications
148174

149175
# Create the client AAD application
150176
Write-Host "Creating the AAD application (msal-node-webapp)"
177+
# Get a 6 months application key for the client Application
178+
$pw = ComputePassword
179+
$fromDate = [DateTime]::Now;
180+
$key = CreateAppKey -fromDate $fromDate -durationInMonths 6 -pw $pw
181+
$clientAppKey = $pw
151182
# create the application
152183
$clientAadApplication = New-AzureADApplication -DisplayName "msal-node-webapp" `
153184
-HomePage "http://localhost:4000" `
154185
-ReplyUrls "http://localhost:4000/redirect" `
186+
-PasswordCredentials $key `
155187
-PublicClient $False
156188

157189
# create the service principal of the newly created application

4-AccessControl/1-app-roles/AppCreationScripts/sample.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"Audience": "AzureADMyOrg",
1616
"HomePage": "http://localhost:4000",
1717
"ReplyUrls": "http://localhost:4000/redirect",
18+
"PasswordCredentials": "Auto",
1819
"AppRoles": [
1920
{
2021
"Types": [

4-AccessControl/1-app-roles/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ There is one project in this sample. To register it, you can:
123123
1. Select **Register** to create the application.
124124
1. In the app's registration screen, find and note the **Application (client) ID**. You use this value in your app's configuration file(s) later in your code.
125125
1. Select **Save** to save your changes.
126+
1. In the **Client secrets** section, select **New client secret**:
127+
- Type a key description (for instance `app secret`),
128+
- Select one of the available key durations (**6 months**, **12 months** or **Custom**) as per your security posture.
129+
- The generated key value will be displayed when you select the **Add** button. Copy and save the generated value for use in later steps.
130+
- You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or blade.
126131

127132
### Define Application Roles
128133

0 commit comments

Comments
 (0)