Skip to content

Commit b5fc8f2

Browse files
committed
Signal R + custom token
1 parent 0ccb5c3 commit b5fc8f2

File tree

83 files changed

+18810
-27
lines changed

Some content is hidden

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

83 files changed

+18810
-27
lines changed

UbikLink.AppHost/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var authTokenStoreKey = builder.AddParameter("auth-token-store-key", secret: true);
2020
var authRegisterAuthorizationKey = builder.AddParameter("auth-register-authorization-key", secret: true);
2121
var emailActivationEnable = builder.AddParameter("email-activation-enable", secret: false);
22+
var hubSignSecureKey = builder.AddParameter("hub-signtoken-key", secret: true);
2223

2324
//Postgres (local)
2425
var db = builder.AddPostgres("ubiklink-postgres", postgresUsername, postgresPassword)
@@ -56,12 +57,21 @@
5657
.WithEnvironment("Messaging__RabbitUser", rabbitUser)
5758
.WithEnvironment("Messaging__RabbitPassword", rabbitPassword)
5859
.WithEnvironment("AuthRegister__Key", authRegisterAuthorizationKey)
60+
.WithEnvironment("AuthRegister__HubSignSecureKey", hubSignSecureKey)
5961
.WithEnvironment("AuthRegister__EmailActivationActivated", emailActivationEnable)
6062
.WithReference(securityDB)
6163
.WaitFor(securityDB)
6264
.WithReference(rabbitmq)
6365
.WithReference(serviceBus);
6466

67+
//Hub
68+
var hub = builder.AddProject<Projects.UbikLink_Commander>("ubiklink-commander")
69+
.WithEnvironment("AuthRegister__HubSignSecureKey", hubSignSecureKey)
70+
.WithReference(securityDB)
71+
.WaitFor(securityDB)
72+
.WithReference(rabbitmq)
73+
.WithReference(serviceBus);
74+
6575
//Proxy
6676
var proxy = builder.AddProject<Projects.UbikLink_Proxy>("ubiklink-proxy")
6777
.WithEnvironment("Proxy__Token",securitytoken)
@@ -76,8 +86,10 @@
7686
.WithReference(cache)
7787
.WithReference(serviceBus)
7888
.WithReference(rabbitmq)
89+
.WithReference(hub)
7990
.WaitFor(cache)
8091
.WaitFor(securityApi)
92+
.WaitFor(hub)
8193
.WaitFor(keycloak);
8294

8395
//.WithReference(rabbitmq)
@@ -105,6 +117,7 @@
105117
.WithEnvironment("Messaging__RabbitUser", rabbitUser)
106118
.WithEnvironment("Messaging__RabbitPassword", rabbitPassword);
107119

120+
108121
//Add npm sevltekit project (not work with fnm.... because of path)
109122
//builder.AddNpmApp("svelte-ui", "../svelte-link-ui","dev")
110123
// .WithEnvironment("BROWSER", "none")

UbikLink.AppHost/UbikLink.AppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26+
<ProjectReference Include="..\UbikLink.Commander\UbikLink.Commander.csproj" />
2627
<ProjectReference Include="..\UbikLink.Proxy\UbikLink.Proxy.csproj" />
2728
<ProjectReference Include="..\UbikLink.Security.Api\UbikLink.Security.Api.csproj" />
2829
<ProjectReference Include="..\UbikLink.Security.UI\UbikLink.Security.UI.csproj" />

UbikLink.AppHost/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"Parameters:keycloak-password": "admin",
3535
"Parameters:auth-token-store-key": "Ye6Y36ocA4SaGqYzd0HgmqMhVaM2jlkE",
3636
"Parameters:auth-register-authorization-key": "Ye6Y36oddddcA4SaGqYzd0HgmqMhVaM2jlkE",
37-
"Parameters:email-activation-enable": "false"
37+
"Parameters:email-activation-enable": "false",
38+
"Parameters:hub-signtoken-key": "0YksXysNolWIH4K0dRzhZm/pv/q+TDVUujmCIsm/nlI="
3839
}

UbikLink.Commander/Program.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.IdentityModel.Tokens;
3+
using System.IdentityModel.Tokens.Jwt;
4+
using System.Security.Claims;
5+
using System.Security.Cryptography;
6+
using UbikLink.Commander.Test;
7+
using UbikLink.Common.Api;
8+
using UbikLink.Common.Auth;
9+
10+
//var SecurityKey = new SymmetricSecurityKey(RandomNumberGenerator.GetBytes(32));
11+
12+
var builder = WebApplication.CreateBuilder(args);
13+
14+
builder.AddServiceDefaults();
15+
16+
// Add services to the container.
17+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
18+
//builder.Services.AddOpenApi();
19+
builder.Services.AddSignalR();
20+
builder.Services.AddCors();
21+
22+
var keys = new AuthRegisterAuthKey();
23+
builder.Configuration.GetSection(AuthRegisterAuthKey.Position).Bind(keys);
24+
var SecurityKey = new SymmetricSecurityKey(Convert.FromBase64String(keys.HubSignSecureKey));
25+
26+
27+
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
28+
.AddJwtBearer(options =>
29+
{
30+
options.TokenValidationParameters =
31+
new TokenValidationParameters
32+
{
33+
LifetimeValidator = (before, expires, token, parameters) => expires > DateTime.UtcNow,
34+
ValidateAudience = false,
35+
ValidateIssuer = false,
36+
ValidateActor = false,
37+
ValidateLifetime = true,
38+
IssuerSigningKey = SecurityKey
39+
};
40+
41+
options.Events = new JwtBearerEvents
42+
{
43+
OnMessageReceived = context =>
44+
{
45+
var accessToken = context.Request.Query["access_token"];
46+
47+
if (!string.IsNullOrEmpty(accessToken))
48+
{
49+
context.Token = context.Request.Query["access_token"];
50+
}
51+
return Task.CompletedTask;
52+
}
53+
};
54+
});
55+
56+
builder.Services.AddAuthorizationBuilder()
57+
.AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy =>
58+
{
59+
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
60+
policy.RequireClaim(ClaimTypes.NameIdentifier);
61+
policy.RequireClaim(ClaimTypes.UserData);
62+
});
63+
64+
65+
var app = builder.Build();
66+
67+
app.MapDefaultEndpoints();
68+
69+
// Configure the HTTP request pipeline.
70+
if (app.Environment.IsDevelopment())
71+
{
72+
//app.MapOpenApi();
73+
}
74+
75+
app.UseAuthentication();
76+
app.UseAuthorization();
77+
78+
app.UseCors(x => x
79+
.AllowAnyMethod()
80+
.AllowAnyHeader()
81+
.SetIsOriginAllowed(origin => true)
82+
.AllowCredentials());
83+
84+
app.MapHub<ChatHub>("/chat");
85+
86+
//app.UseHttpsRedirection();
87+
88+
89+
app.Run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5122",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7036;http://localhost:5122",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}

UbikLink.Commander/Test/ChatHub.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.SignalR;
4+
using System.Security.Claims;
5+
6+
namespace UbikLink.Commander.Test
7+
{
8+
[Authorize]
9+
public class ChatHub : Hub<IChatClient>
10+
{
11+
public override Task OnConnectedAsync()
12+
{
13+
var userId = Context.User?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
14+
var tenantId = Context.User?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.UserData)?.Value;
15+
16+
if (userId == null || tenantId == null)
17+
{
18+
Context.Abort();
19+
return Task.CompletedTask;
20+
}
21+
22+
return base.OnConnectedAsync();
23+
}
24+
25+
public override Task OnDisconnectedAsync(Exception? exception)
26+
{
27+
return base.OnDisconnectedAsync(exception);
28+
}
29+
30+
public async Task SendMessage(string user, string message)
31+
{
32+
await Clients.All.ReceiveMessage(user,message);
33+
}
34+
}
35+
36+
public interface IChatClient
37+
{
38+
Task ReceiveMessage(string user, string message);
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.2" />
11+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
12+
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.2.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\UbikLink.Common\UbikLink.Common.csproj" />
17+
<ProjectReference Include="..\UbikLink.ServiceDefaults\UbikLink.ServiceDefaults.csproj" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Folder Include="Auth\" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@UbikLink.Commander_HostAddress = http://localhost:5122
2+
3+
GET {{UbikLink.Commander_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)