Skip to content

Commit 0b2a34b

Browse files
committed
(#107) SignalR Authentication and Authorization
1 parent 03f21d3 commit 0b2a34b

File tree

42 files changed

+679
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+679
-167
lines changed

src/Microservices/.helm/microservices/charts/webapi/templates/webapi.configmap.yml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,44 @@ data:
1111
ReRoutes__1__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-product-api"
1212
ReRoutes__1__DownstreamHostAndPorts__0__Port: "80"
1313
ReRoutes__2__DownstreamScheme: "http"
14-
ReRoutes__2__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-storage-api"
14+
ReRoutes__2__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-product-api"
1515
ReRoutes__2__DownstreamHostAndPorts__0__Port: "80"
1616
ReRoutes__3__DownstreamScheme: "http"
1717
ReRoutes__3__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-storage-api"
1818
ReRoutes__3__DownstreamHostAndPorts__0__Port: "80"
1919
ReRoutes__4__DownstreamScheme: "http"
20-
ReRoutes__4__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-auditlog-api"
20+
ReRoutes__4__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-storage-api"
2121
ReRoutes__4__DownstreamHostAndPorts__0__Port: "80"
2222
ReRoutes__5__DownstreamScheme: "http"
23-
ReRoutes__5__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-auditlog-api"
23+
ReRoutes__5__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-storage-api"
2424
ReRoutes__5__DownstreamHostAndPorts__0__Port: "80"
2525
ReRoutes__6__DownstreamScheme: "http"
26-
ReRoutes__6__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-identity-api"
26+
ReRoutes__6__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-auditlog-api"
2727
ReRoutes__6__DownstreamHostAndPorts__0__Port: "80"
2828
ReRoutes__7__DownstreamScheme: "http"
29-
ReRoutes__7__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-identity-api"
29+
ReRoutes__7__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-auditlog-api"
3030
ReRoutes__7__DownstreamHostAndPorts__0__Port: "80"
3131
ReRoutes__8__DownstreamScheme: "http"
32-
ReRoutes__8__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-configuration-api"
32+
ReRoutes__8__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-auditlog-api"
3333
ReRoutes__8__DownstreamHostAndPorts__0__Port: "80"
3434
ReRoutes__9__DownstreamScheme: "http"
35-
ReRoutes__9__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-configuration-api"
35+
ReRoutes__9__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-identity-api"
3636
ReRoutes__9__DownstreamHostAndPorts__0__Port: "80"
37+
ReRoutes__10__DownstreamScheme: "http"
38+
ReRoutes__10__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-identity-api"
39+
ReRoutes__10__DownstreamHostAndPorts__0__Port: "80"
40+
ReRoutes__11__DownstreamScheme: "http"
41+
ReRoutes__11__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-identity-api"
42+
ReRoutes__11__DownstreamHostAndPorts__0__Port: "80"
43+
ReRoutes__12__DownstreamScheme: "http"
44+
ReRoutes__12__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-configuration-api"
45+
ReRoutes__12__DownstreamHostAndPorts__0__Port: "80"
46+
ReRoutes__13__DownstreamScheme: "http"
47+
ReRoutes__13__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-configuration-api"
48+
ReRoutes__13__DownstreamHostAndPorts__0__Port: "80"
49+
ReRoutes__14__DownstreamScheme: "http"
50+
ReRoutes__14__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-configuration-api"
51+
ReRoutes__14__DownstreamHostAndPorts__0__Port: "80"
52+
ReRoutes__15__DownstreamScheme: "ws"
53+
ReRoutes__15__DownstreamHostAndPorts__0__Host: "{{ .Release.Name}}-notification-api"
54+
ReRoutes__15__DownstreamHostAndPorts__0__Port: "80"

src/Microservices/.k8s/webapi.configmap.yml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,44 @@ data:
1111
ReRoutes__1__DownstreamHostAndPorts__0__Host: "product-api"
1212
ReRoutes__1__DownstreamHostAndPorts__0__Port: "80"
1313
ReRoutes__2__DownstreamScheme: "http"
14-
ReRoutes__2__DownstreamHostAndPorts__0__Host: "storage-api"
14+
ReRoutes__2__DownstreamHostAndPorts__0__Host: "product-api"
1515
ReRoutes__2__DownstreamHostAndPorts__0__Port: "80"
1616
ReRoutes__3__DownstreamScheme: "http"
1717
ReRoutes__3__DownstreamHostAndPorts__0__Host: "storage-api"
1818
ReRoutes__3__DownstreamHostAndPorts__0__Port: "80"
1919
ReRoutes__4__DownstreamScheme: "http"
20-
ReRoutes__4__DownstreamHostAndPorts__0__Host: "auditlog-api"
20+
ReRoutes__4__DownstreamHostAndPorts__0__Host: "storage-api"
2121
ReRoutes__4__DownstreamHostAndPorts__0__Port: "80"
2222
ReRoutes__5__DownstreamScheme: "http"
23-
ReRoutes__5__DownstreamHostAndPorts__0__Host: "auditlog-api"
23+
ReRoutes__5__DownstreamHostAndPorts__0__Host: "storage-api"
2424
ReRoutes__5__DownstreamHostAndPorts__0__Port: "80"
2525
ReRoutes__6__DownstreamScheme: "http"
26-
ReRoutes__6__DownstreamHostAndPorts__0__Host: "identity-api"
26+
ReRoutes__6__DownstreamHostAndPorts__0__Host: "auditlog-api"
2727
ReRoutes__6__DownstreamHostAndPorts__0__Port: "80"
2828
ReRoutes__7__DownstreamScheme: "http"
29-
ReRoutes__7__DownstreamHostAndPorts__0__Host: "identity-api"
29+
ReRoutes__7__DownstreamHostAndPorts__0__Host: "auditlog-api"
3030
ReRoutes__7__DownstreamHostAndPorts__0__Port: "80"
3131
ReRoutes__8__DownstreamScheme: "http"
32-
ReRoutes__8__DownstreamHostAndPorts__0__Host: "configuration-api"
32+
ReRoutes__8__DownstreamHostAndPorts__0__Host: "auditlog-api"
3333
ReRoutes__8__DownstreamHostAndPorts__0__Port: "80"
3434
ReRoutes__9__DownstreamScheme: "http"
35-
ReRoutes__9__DownstreamHostAndPorts__0__Host: "configuration-api"
35+
ReRoutes__9__DownstreamHostAndPorts__0__Host: "identity-api"
3636
ReRoutes__9__DownstreamHostAndPorts__0__Port: "80"
37+
ReRoutes__10__DownstreamScheme: "http"
38+
ReRoutes__10__DownstreamHostAndPorts__0__Host: "identity-api"
39+
ReRoutes__10__DownstreamHostAndPorts__0__Port: "80"
40+
ReRoutes__11__DownstreamScheme: "http"
41+
ReRoutes__11__DownstreamHostAndPorts__0__Host: "identity-api"
42+
ReRoutes__11__DownstreamHostAndPorts__0__Port: "80"
43+
ReRoutes__12__DownstreamScheme: "http"
44+
ReRoutes__12__DownstreamHostAndPorts__0__Host: "configuration-api"
45+
ReRoutes__12__DownstreamHostAndPorts__0__Port: "80"
46+
ReRoutes__13__DownstreamScheme: "http"
47+
ReRoutes__13__DownstreamHostAndPorts__0__Host: "configuration-api"
48+
ReRoutes__13__DownstreamHostAndPorts__0__Port: "80"
49+
ReRoutes__14__DownstreamScheme: "http"
50+
ReRoutes__14__DownstreamHostAndPorts__0__Host: "configuration-api"
51+
ReRoutes__14__DownstreamHostAndPorts__0__Port: "80"
52+
ReRoutes__15__DownstreamScheme: "ws"
53+
ReRoutes__15__DownstreamHostAndPorts__0__Host: "notification-api"
54+
ReRoutes__15__DownstreamHostAndPorts__0__Port: "80"

src/Microservices/Gateways.WebAPI/ClassifiedAds.Gateways.WebAPI/ClassifiedAds.Gateways.WebAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.10" />
89
<PackageReference Include="Ocelot" Version="13.8.0" />
910
</ItemGroup>
1011

src/Microservices/Gateways.WebAPI/ClassifiedAds.Gateways.WebAPI/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void ConfigureServices(IServiceCollection services)
2020
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2121
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2222
{
23+
app.UseWebSockets();
2324
app.UseOcelot().Wait();
2425
}
2526
}

src/Microservices/Gateways.WebAPI/ClassifiedAds.Gateways.WebAPI/appsettings.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
],
2020
"UpstreamPathTemplate": "/api/products"
2121
},
22+
{
23+
"DownstreamPathTemplate": "/api/products/",
24+
"DownstreamScheme": "https",
25+
"DownstreamHostAndPorts": [
26+
{
27+
"Host": "localhost",
28+
"Port": 44328
29+
}
30+
],
31+
"UpstreamPathTemplate": "/api/products/"
32+
},
2233
{
2334
"DownstreamPathTemplate": "/api/products/{everything}",
2435
"DownstreamScheme": "https",
@@ -41,6 +52,17 @@
4152
],
4253
"UpstreamPathTemplate": "/api/files"
4354
},
55+
{
56+
"DownstreamPathTemplate": "/api/files/",
57+
"DownstreamScheme": "https",
58+
"DownstreamHostAndPorts": [
59+
{
60+
"Host": "localhost",
61+
"Port": 44345
62+
}
63+
],
64+
"UpstreamPathTemplate": "/api/files/"
65+
},
4466
{
4567
"DownstreamPathTemplate": "/api/files/{everything}",
4668
"DownstreamScheme": "https",
@@ -63,6 +85,17 @@
6385
],
6486
"UpstreamPathTemplate": "/api/auditLogEntries"
6587
},
88+
{
89+
"DownstreamPathTemplate": "/api/auditLogEntries/",
90+
"DownstreamScheme": "https",
91+
"DownstreamHostAndPorts": [
92+
{
93+
"Host": "localhost",
94+
"Port": 44378
95+
}
96+
],
97+
"UpstreamPathTemplate": "/api/auditLogEntries/"
98+
},
6699
{
67100
"DownstreamPathTemplate": "/api/auditLogEntries/{everything}",
68101
"DownstreamScheme": "https",
@@ -85,6 +118,17 @@
85118
],
86119
"UpstreamPathTemplate": "/api/users"
87120
},
121+
{
122+
"DownstreamPathTemplate": "/api/users/",
123+
"DownstreamScheme": "https",
124+
"DownstreamHostAndPorts": [
125+
{
126+
"Host": "localhost",
127+
"Port": 44306
128+
}
129+
],
130+
"UpstreamPathTemplate": "/api/users/"
131+
},
88132
{
89133
"DownstreamPathTemplate": "/api/users/{everything}",
90134
"DownstreamScheme": "https",
@@ -107,6 +151,17 @@
107151
],
108152
"UpstreamPathTemplate": "/api/ConfigurationEntries"
109153
},
154+
{
155+
"DownstreamPathTemplate": "/api/ConfigurationEntries/",
156+
"DownstreamScheme": "https",
157+
"DownstreamHostAndPorts": [
158+
{
159+
"Host": "localhost",
160+
"Port": 44301
161+
}
162+
],
163+
"UpstreamPathTemplate": "/api/ConfigurationEntries/"
164+
},
110165
{
111166
"DownstreamPathTemplate": "/api/ConfigurationEntries/{everything}",
112167
"DownstreamScheme": "https",
@@ -117,6 +172,17 @@
117172
}
118173
],
119174
"UpstreamPathTemplate": "/api/ConfigurationEntries/{everything}"
175+
},
176+
{
177+
"DownstreamPathTemplate": "/hubs/{everything}",
178+
"DownstreamScheme": "wss",
179+
"DownstreamHostAndPorts": [
180+
{
181+
"Host": "localhost",
182+
"Port": 44390
183+
}
184+
],
185+
"UpstreamPathTemplate": "/hubs/{everything}"
120186
}
121187
],
122188
"GlobalConfiguration": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using ClassifiedAds.Services.Notification.Hubs;
2+
using Microsoft.AspNetCore.SignalR;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
6+
using System;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace ClassifiedAds.Services.Notification.HostedServices
11+
{
12+
public class PushNotificationHostedService : BackgroundService
13+
{
14+
private readonly IServiceProvider _services;
15+
private readonly ILogger<PushNotificationHostedService> _logger;
16+
17+
public PushNotificationHostedService(IServiceProvider services,
18+
ILogger<PushNotificationHostedService> logger)
19+
{
20+
_services = services;
21+
_logger = logger;
22+
}
23+
24+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
25+
{
26+
await DoWork(stoppingToken);
27+
}
28+
29+
private async Task DoWork(CancellationToken stoppingToken)
30+
{
31+
while (!stoppingToken.IsCancellationRequested)
32+
{
33+
using (var scope = _services.CreateScope())
34+
{
35+
var notificationHubContext = scope.ServiceProvider.GetRequiredService<IHubContext<NotificationHub>>();
36+
37+
await notificationHubContext.Clients.All.SendAsync("ReceiveMessage", $"Test message from NotificationHub ...", cancellationToken: stoppingToken);
38+
}
39+
40+
await Task.Delay(30000, stoppingToken);
41+
}
42+
}
43+
}
44+
}

src/Microservices/Services.Notification/ClassifiedAds.Services.Notification.Api/Hubs/HealthCheckHub.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.SignalR;
3+
4+
namespace ClassifiedAds.Services.Notification.Hubs
5+
{
6+
[Authorize]
7+
public class NotificationHub : Hub
8+
{
9+
}
10+
}

src/Microservices/Services.Notification/ClassifiedAds.Services.Notification.Api/Hubs/SimulatedLongRunningTaskHub.cs

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

src/Microservices/Services.Notification/ClassifiedAds.Services.Notification.Api/Startup.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using ClassifiedAds.Infrastructure.DistributedTracing;
22
using ClassifiedAds.Infrastructure.Web.Filters;
33
using ClassifiedAds.Services.Notification.ConfigurationOptions;
4+
using ClassifiedAds.Services.Notification.HostedServices;
5+
using ClassifiedAds.Services.Notification.Hubs;
46
using Microsoft.AspNetCore.Authentication.JwtBearer;
57
using Microsoft.AspNetCore.Builder;
68
using Microsoft.AspNetCore.Hosting;
@@ -35,12 +37,20 @@ public void ConfigureServices(IServiceCollection services)
3537
configure.Filters.Add(typeof(GlobalExceptionFilter));
3638
});
3739

40+
services.AddSignalR();
41+
3842
services.AddCors(options =>
3943
{
4044
options.AddPolicy("AllowAnyOrigin", builder => builder
4145
.AllowAnyOrigin()
4246
.AllowAnyMethod()
4347
.AllowAnyHeader());
48+
49+
options.AddPolicy("SignalRHubs", builder => builder
50+
.SetIsOriginAllowed(host => true)
51+
.AllowAnyHeader()
52+
.WithMethods("GET", "POST")
53+
.AllowCredentials());
4454
});
4555

4656
services.AddDistributedTracing(AppSettings.DistributedTracing);
@@ -57,6 +67,8 @@ public void ConfigureServices(IServiceCollection services)
5767
options.Audience = AppSettings.IdentityServerAuthentication.ApiName;
5868
options.RequireHttpsMetadata = AppSettings.IdentityServerAuthentication.RequireHttpsMetadata;
5969
});
70+
71+
services.AddHostedService<PushNotificationHostedService>();
6072
}
6173

6274
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -88,6 +100,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
88100
app.UseEndpoints(endpoints =>
89101
{
90102
endpoints.MapControllers();
103+
endpoints.MapHub<NotificationHub>("/hubs/notification").RequireCors("SignalRHubs");
91104
});
92105
}
93106
}

0 commit comments

Comments
 (0)