Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit c4b5ae4

Browse files
committed
update ui, scripts
1 parent 78efd62 commit c4b5ae4

File tree

13 files changed

+439
-578
lines changed

13 files changed

+439
-578
lines changed

.github/workflows/node.js.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
strategy:
1616
matrix:
17-
node-version: [14.x]
17+
node-version: [16.x]
1818
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1919

2020
steps:

3-Authorization-II/1-call-api/API/TodoListAPI/Controllers/TodoListController.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.EntityFrameworkCore;
99
using TodoListAPI.Models;
1010
using System.Security.Claims;
11+
using Microsoft.Identity.Web;
1112
using Microsoft.Identity.Web.Resource;
1213

1314
namespace TodoListAPI.Controllers
@@ -17,7 +18,7 @@ namespace TodoListAPI.Controllers
1718
[ApiController]
1819
public class TodoListController : ControllerBase
1920
{
20-
// The Web API will only accept tokens 1) for users, and
21+
// The Web API will only accept tokens 1) for users, and
2122
// 2) having the access_as_user scope for this API
2223
static readonly string[] scopeRequiredByApi = new string[] { "access_as_user" };
2324

@@ -42,7 +43,7 @@ public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
4243
public async Task<ActionResult<TodoItem>> GetTodoItem(int id)
4344
{
4445
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
45-
46+
4647
var todoItem = await _context.TodoItems.FindAsync(id);
4748

4849
if (todoItem == null)
@@ -126,5 +127,26 @@ private bool TodoItemExists(int id)
126127
{
127128
return _context.TodoItems.Any(e => e.Id == id);
128129
}
130+
131+
// Checks if the presented token has application permissions
132+
private bool HasApplicationPermissions(string[] permissionsNames)
133+
{
134+
var rolesClaim = User.Claims.Where(
135+
c => c.Type == ClaimConstants.Roles || c.Type == ClaimConstants.Role)
136+
.SelectMany(c => c.Value.Split(' '));
137+
138+
var result = rolesClaim.Any(v => permissionsNames.Any(p => p.Equals(v)));
139+
140+
return result;
141+
}
142+
143+
// Checks if the presented token has delegated permissions
144+
private bool HasDelegatedPermissions(string[] scopesNames)
145+
{
146+
var result = (User.FindFirst(ClaimConstants.Scp) ?? User.FindFirst(ClaimConstants.Scope))?
147+
.Value.Split(' ').Any(v => scopesNames.Any(s => s.Equals(v)));
148+
149+
return result ?? false;
150+
}
129151
}
130152
}

3-Authorization-II/1-call-api/API/TodoListAPI/Startup.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.EntityFrameworkCore;
88
using Microsoft.Identity.Web;
9+
using System.IdentityModel.Tokens.Jwt;
10+
911
using TodoListAPI.Models;
10-
using Microsoft.AspNetCore.Authentication.JwtBearer;
1112

1213
namespace TodoListAPI
1314
{
@@ -23,19 +24,24 @@ public Startup(IConfiguration configuration)
2324
// This method gets called by the runtime. Use this method to add services to the container.
2425
public void ConfigureServices(IServiceCollection services)
2526
{
27+
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
28+
// By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
29+
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
30+
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
31+
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
32+
2633
// Setting configuration for protected web api
27-
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
28-
.AddMicrosoftIdentityWebApi(Configuration);
34+
services.AddMicrosoftIdentityWebApiAuthentication(Configuration);
2935

3036
// Creating policies that wraps the authorization requirements
3137
services.AddAuthorization();
3238

3339
services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList"));
3440

3541
services.AddControllers();
36-
37-
// Allowing CORS for all domains and methods for the purpose of the sample
38-
// In production, modify this with the actual domains you want to allow
42+
43+
// Allowing CORS for all domains and HTTP methods for the purpose of the sample
44+
// In production, modify this with the actual domains and HTTP methods you want to allow
3945
services.AddCors(o => o.AddPolicy("default", builder =>
4046
{
4147
builder.AllowAnyOrigin()
@@ -72,4 +78,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7278
});
7379
}
7480
}
75-
}
81+
}

3-Authorization-II/1-call-api/AppCreationScripts/AppCreationScripts.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ The `Configure.ps1` will stop if it tries to create an Azure AD application whic
7777
7878
### (Optionally) install AzureAD PowerShell modules
7979
80-
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:
80+
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:
8181
82-
1. If you have never done it already, in the PowerShell window, install the AzureAD PowerShell modules. For this:
82+
1. If you have never done it already, in the PowerShell window, install the Graph PowerShell modules. For this:
8383
8484
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
8585
2. Type:
86-
86+
8787
```PowerShell
88-
Install-Module AzureAD
88+
Install-Module Microsoft.Graph.Applications
8989
```
9090
9191
or if you cannot be administrator on your machine, run:
92-
92+
9393
```PowerShell
94-
Install-Module AzureAD -Scope CurrentUser
94+
Install-Module Microsoft.Graph.Applications -Scope CurrentUser
9595
```
9696
9797
### Run the script and start running
@@ -131,7 +131,7 @@ Note that the script will choose the tenant in which to create the applications,
131131
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
132132
133133
```PowerShell
134-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
134+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
135135
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
136136
. .\Cleanup.ps1 -Credential $mycreds
137137
. .\Configure.ps1 -Credential $mycreds
@@ -160,7 +160,7 @@ $tenantId = "yourTenantIdGuid"
160160
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:
161161

162162
```PowerShell
163-
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
163+
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
164164
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
165165
$tenantId = "yourTenantIdGuid"
166166
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
@@ -180,7 +180,7 @@ The acceptable values for this parameter are:
180180

181181
Example:
182182

183-
```PowerShell
184-
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
185-
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
186-
```
183+
```PowerShell
184+
. .\Cleanup.ps1 -AzureEnvironmentName "AzureGermanyCloud"
185+
. .\Configure.ps1 -AzureEnvironmentName "AzureGermanyCloud"
186+
```

3-Authorization-II/1-call-api/AppCreationScripts/Cleanup.ps1

-96
This file was deleted.

0 commit comments

Comments
 (0)