Skip to content

Commit 9d59480

Browse files
authored
Merge pull request #3 from EventTriangle/oidc
Implement Azure AD OIDC role-based authorization
2 parents 735b656 + 992b558 commit 9d59480

File tree

13 files changed

+110
-42
lines changed

13 files changed

+110
-42
lines changed

src/consumer/EventTriangleAPI.Consumer.Domain/EventTriangleAPI.Consumer.Domain.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\..\shared\EventTriangleAPI.Shared.Application\EventTriangleAPI.Shared.Application.csproj" />
11+
</ItemGroup>
12+
913
</Project>

src/consumer/EventTriangleAPI.Consumer.Presentation/Controllers/WeatherForecastController.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Authorization;
12
using Microsoft.AspNetCore.Mvc;
23

34
namespace EventTriangleAPI.Consumer.Presentation.Controllers;
@@ -17,9 +18,23 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
1718
{
1819
_logger = logger;
1920
}
21+
22+
[Authorize(Roles = "User, Admin")]
23+
[HttpGet("user_and_admin")]
24+
public IEnumerable<WeatherForecast> GetForUserAndAdmin()
25+
{
26+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
27+
{
28+
Date = DateTime.Now.AddDays(index),
29+
TemperatureC = Random.Shared.Next(-20, 55),
30+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
31+
})
32+
.ToArray();
33+
}
2034

21-
[HttpGet(Name = "GetWeatherForecast")]
22-
public IEnumerable<WeatherForecast> Get()
35+
[Authorize(Roles = "Admin")]
36+
[HttpGet("admin")]
37+
public IEnumerable<WeatherForecast> GetForAdmin()
2338
{
2439
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
2540
{

src/consumer/EventTriangleAPI.Consumer.Presentation/EventTriangleAPI.Consumer.Presentation.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3"/>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\EventTriangleAPI.Consumer.Domain\EventTriangleAPI.Consumer.Domain.csproj" />
1115
</ItemGroup>
1216

1317
</Project>

src/consumer/EventTriangleAPI.Consumer.Presentation/Program.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
var builder = WebApplication.CreateBuilder(args);
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.Identity.Web;
3+
using Microsoft.IdentityModel.Logging;
24

3-
// Add services to the container.
5+
var builder = WebApplication.CreateBuilder(args);
46

57
builder.Services.AddControllers();
6-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
78
builder.Services.AddEndpointsApiExplorer();
89
builder.Services.AddSwaggerGen();
910

11+
var configurationSection = builder.Configuration.GetSection("AzureAd");
12+
13+
builder.Services
14+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
15+
.AddMicrosoftIdentityWebApi(configurationSection);
16+
1017
var app = builder.Build();
1118

12-
// Configure the HTTP request pipeline.
13-
if (app.Environment.IsDevelopment())
14-
{
15-
app.UseSwagger();
16-
app.UseSwaggerUI();
17-
}
19+
IdentityModelEventSource.ShowPII = true;
20+
21+
app.UseSwagger();
22+
app.UseSwaggerUI();
23+
1824

1925
app.UseHttpsRedirection();
2026

27+
app.UseAuthentication();
28+
2129
app.UseAuthorization();
2230

2331
app.MapControllers();

src/consumer/EventTriangleAPI.Consumer.Presentation/appsettings.Development.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/consumer/EventTriangleAPI.Consumer.Presentation/appsettings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"AzureAd": {
10+
"Instance": "https://login.microsoftonline.com/",
11+
"TenantId": "b40a105f-0643-4922-8e60-10fc1abf9c4b",
12+
"ClientId": "25128d03-9817-4e11-bddf-dc5f6df4042a",
13+
"Scopes": "EventTriangleLocalAuth.All"
14+
}
915
}

src/sender/EventTriangleAPI.Sender.Domain/EventTriangleAPI.Sender.Domain.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\..\shared\EventTriangleAPI.Shared.Application\EventTriangleAPI.Shared.Application.csproj" />
11+
</ItemGroup>
12+
913
</Project>

src/sender/EventTriangleAPI.Sender.Presentation/Controllers/WeatherForecastController.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using Microsoft.AspNetCore.Authorization;
12
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Identity.Web.Resource;
24

35
namespace EventTriangleAPI.Sender.Presentation.Controllers;
46

57
[ApiController]
68
[Route("[controller]")]
9+
[RequiredScope(RequiredScopesConfigurationKey = "AzureAd:Scopes")]
710
public class WeatherForecastController : ControllerBase
811
{
912
private static readonly string[] Summaries = new[]
@@ -18,8 +21,22 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
1821
_logger = logger;
1922
}
2023

21-
[HttpGet(Name = "GetWeatherForecast")]
22-
public IEnumerable<WeatherForecast> Get()
24+
[Authorize(Roles = "User, Admin")]
25+
[HttpGet("user_and_admin")]
26+
public IEnumerable<WeatherForecast> GetForUserAndAdmin()
27+
{
28+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
29+
{
30+
Date = DateTime.Now.AddDays(index),
31+
TemperatureC = Random.Shared.Next(-20, 55),
32+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
33+
})
34+
.ToArray();
35+
}
36+
37+
[Authorize(Roles = "Admin")]
38+
[HttpGet("admin")]
39+
public IEnumerable<WeatherForecast> GetForAdmin()
2340
{
2441
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
2542
{

src/sender/EventTriangleAPI.Sender.Presentation/EventTriangleAPI.Sender.Presentation.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3"/>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\EventTriangleAPI.Sender.Domain\EventTriangleAPI.Sender.Domain.csproj" />
1115
</ItemGroup>
1216

1317
</Project>

src/sender/EventTriangleAPI.Sender.Presentation/Program.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
var builder = WebApplication.CreateBuilder(args);
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.Identity.Web;
3+
using Microsoft.IdentityModel.Logging;
24

3-
// Add services to the container.
5+
var builder = WebApplication.CreateBuilder(args);
46

57
builder.Services.AddControllers();
6-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
78
builder.Services.AddEndpointsApiExplorer();
89
builder.Services.AddSwaggerGen();
910

11+
var configurationSection = builder.Configuration.GetSection("AzureAd");
12+
13+
builder.Services
14+
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
15+
.AddMicrosoftIdentityWebApi(configurationSection);
16+
1017
var app = builder.Build();
1118

12-
// Configure the HTTP request pipeline.
13-
if (app.Environment.IsDevelopment())
14-
{
15-
app.UseSwagger();
16-
app.UseSwaggerUI();
17-
}
19+
IdentityModelEventSource.ShowPII = true;
20+
21+
22+
app.UseSwagger();
23+
app.UseSwaggerUI();
24+
1825

1926
app.UseHttpsRedirection();
2027

28+
app.UseAuthentication();
29+
2130
app.UseAuthorization();
2231

2332
app.MapControllers();

0 commit comments

Comments
 (0)