Skip to content

Commit

Permalink
Server side token storage
Browse files Browse the repository at this point in the history
  • Loading branch information
JZO001 committed Oct 24, 2022
1 parent 6b040b2 commit e7365b8
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ $RECYCLE.BIN/
.DS_Store

# For Future
.Output/*
Forge.Yoda.Apps.ASPNETCore.Client/wwwroot/*
Forge.Yoda.Apps.MAUI/wwwroot/*
Forge.Yoda.Apps.WinForms/wwwroot/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.3" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.10" PrivateAssets="all" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Forge.Yoda.Apps.MAUI/Forge.Yoda.Apps.MAUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.3" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<ProjectReference Include="..\Forge.Yoda.Shared.Models\Forge.Yoda.Shared.Models.csproj" />
<ProjectReference Include="..\Forge.Yoda.Shared.ServiceImpls\Forge.Yoda.Shared.ServiceImpls.csproj" />
<ProjectReference Include="..\Forge.Yoda.Shared.ServiceInterfaces\Forge.Yoda.Shared.ServiceInterfaces.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion Forge.Yoda.Apps.MAUI/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static MauiApp CreateMauiApp()
#endif
options.RefreshTokenBeforeExpirationInMilliseconds = 50000;
options.SecondaryKeys.Add(new JwtKeyValuePair(Consts.DEVICE_ID, "7010c030-6a2c-4dc5-86a3-2a9702baa7b3"));
options.HttpMessageHandler = GetLocalhostHandler();
options.HttpMessageHandlerFactory = GetLocalhostHandler;
});

builder.Services.AddForgeJwtClientAuthenticationCoreWithLocalStorage();
Expand Down
6 changes: 3 additions & 3 deletions Forge.Yoda.Apps.WPF/Forge.Yoda.Apps.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.3" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="6.0.541" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
Expand Down
4 changes: 2 additions & 2 deletions Forge.Yoda.Apps.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MainWindow()
{
InitializeComponent();

Func<HttpClientHandler> GetInsecureHandler = delegate ()
Func<HttpMessageHandler> GetInsecureHandler = delegate ()
{
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
Expand Down Expand Up @@ -56,7 +56,7 @@ public MainWindow()
options.BaseAddress = "https://localhost:7253/";
options.RefreshTokenBeforeExpirationInMilliseconds = 50000;
options.SecondaryKeys.Add(new JwtKeyValuePair(Consts.DEVICE_ID, "eb11812d-fb1b-4ec9-95d3-c9f9f9832a2a"));
options.HttpMessageHandler = GetInsecureHandler();
options.HttpMessageHandlerFactory = GetInsecureHandler;
});

services.AddForgeJwtClientAuthenticationCoreWithLocalStorage();
Expand Down
6 changes: 3 additions & 3 deletions Forge.Yoda.Apps.WinForms/Forge.Yoda.Apps.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Client" Version="1.2.3" />
<PackageReference Include="Forge.Security.Jwt.Client.Storage.Browser" Version="1.2.2" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" Version="6.0.541" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="log4net" Version="2.0.15" />
Expand Down
4 changes: 2 additions & 2 deletions Forge.Yoda.Apps.WinForms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void MainForm_Shown(object sender, EventArgs e)
{
ServiceCollection services = new ServiceCollection();

Func<HttpClientHandler> GetInsecureHandler = delegate ()
Func<HttpMessageHandler> GetInsecureHandler = delegate ()
{
HttpClientHandler handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
Expand Down Expand Up @@ -54,7 +54,7 @@ private void MainForm_Shown(object sender, EventArgs e)
options.BaseAddress = "https://localhost:7253/";
options.RefreshTokenBeforeExpirationInMilliseconds = 50000;
options.SecondaryKeys.Add(new JwtKeyValuePair(Consts.DEVICE_ID, "7010c030-6a2c-4dc5-86a3-2a9702baa7b3"));
options.HttpMessageHandler = GetInsecureHandler();
options.HttpMessageHandlerFactory = GetInsecureHandler;
});

services.AddForgeJwtClientAuthenticationCoreWithLocalStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public DatabaseContext() : base(CreateOptions())
{
}

public DatabaseContext(DbContextOptions options) : base(options)
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}

Expand All @@ -22,7 +22,7 @@ public static DatabaseContext Create()
return new DatabaseContext(CreateOptions());
}

private static DbContextOptions CreateOptions()
private static DbContextOptions<DatabaseContext> CreateOptions()
{
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseSqlServer(DefaultConnectionString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -18,8 +18,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Service" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Service" Version="1.5.1" />
<PackageReference Include="Forge.Security.Jwt.Service.Storage.SqlServer" Version="1.0.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" />
Expand All @@ -42,7 +43,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
</ItemGroup>

<ItemGroup><!--Reference Include="Forge.Security.Jwt.Service">
<ItemGroup>
<!--Reference Include="Forge.Security.Jwt.Service">
<HintPath>..\Forge.Yoda.Externals\Forge.Security.Jwt.Service.dll</HintPath>
</Reference>
<Reference Include="Forge.Security.Jwt.Shared">
Expand Down
33 changes: 18 additions & 15 deletions Forge.Yoda.Services.Authentication/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Forge.Yoda.Services.Authentication.Codes;
using Forge.Yoda.Services.Authentication.Database;
using Forge.Yoda.Services.Authentication.Services;
using Forge.Security.Jwt.Service.Storage.SqlServer;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
Expand All @@ -28,22 +29,22 @@ public void ConfigureServices(IServiceCollection services)
JwtTokenConfiguration jwtTokenConfig = Configuration.GetSection("JwtTokenConfig").Get<JwtTokenConfiguration>();
services.AddSingleton(jwtTokenConfig);

var connectionString = DatabaseContext.DefaultConnectionString = Configuration.GetConnectionString("DefaultConnection");

// migrate database, if neccessary
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseSqlServer(connectionString);
using (DatabaseContext context = new DatabaseContext(optionsBuilder.Options))
{
context.Database.Migrate();
string connectionString = DatabaseContext.DefaultConnectionString = Configuration.GetConnectionString("DefaultConnection");
var dbContextOptionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
dbContextOptionsBuilder.UseSqlServer(connectionString);
using (DatabaseContext context = new DatabaseContext(dbContextOptionsBuilder.Options))
{
context.Database.Migrate();
}
services.AddDbContext<DatabaseContext>(config =>
{
config.UseSqlServer(connectionString);
//config.UseInMemoryDatabase("Memory");
});
}

services.AddDbContext<DatabaseContext>(config =>
{
config.UseSqlServer(connectionString);
//config.UseInMemoryDatabase("Memory");
});

// AddIdentity registers the services
services.AddIdentity<User, IdentityRole>(config =>
{
Expand Down Expand Up @@ -82,10 +83,12 @@ public void ConfigureServices(IServiceCollection services)
};
});

// add Jwt Token service
services.AddForgeJwtServerAuthenticationCore();
//services.AddSingleton<IStorage<JwtRefreshToken>, MemoryStorage<JwtRefreshToken>>(); // replace this later in a distributed environment
//services.AddSingleton<IJwtManagementService, JwtManagementService>();
//services.AddHostedService<JwtTokenMaintenanceHostedService>();
// add SqlServer storage
services.AddForgeJwtServiceSqlServerStorage(config => {
config.ConnectionString = Configuration.GetConnectionString("ServiceStorageConnection");
});

services.AddScoped<IUserService, UserService>();
services.AddCors(options =>
Expand Down
3 changes: 2 additions & 1 deletion Forge.Yoda.Services.Authentication/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=.\\SQLEXPRESS2019;Initial Catalog=ForgeYodaAuth;Integrated Security=True"
"DefaultConnection": "Data Source=.\\SQLEXPRESS2019;Initial Catalog=ForgeYodaAuth;Integrated Security=True",
"ServiceStorageConnection": "Data Source=.\\SQLEXPRESS2019;Initial Catalog=ForgeJwtServiceStorage;Integrated Security=True"
},
"JwtTokenConfig": {
"Secret": "fa1226aa-4f01-4228-9cf4-4896560798c3",
Expand Down
10 changes: 5 additions & 5 deletions Forge.Yoda.Shared.UI/Forge.Yoda.Shared.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.5.0" />
<PackageReference Include="Forge.Security.Jwt.Shared" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
Expand All @@ -22,10 +22,10 @@
<ProjectReference Include="..\Forge.Yoda.Shared.ServiceInterfaces\Forge.Yoda.Shared.ServiceInterfaces.csproj" />
</ItemGroup>

<!--ItemGroup>
<Reference Include="Forge.Security.Jwt.Shared">
<ItemGroup>
<!--Reference Include="Forge.Security.Jwt.Shared">
<HintPath>..\Forge.Yoda.Externals\Forge.Security.Jwt.Shared.dll</HintPath>
</Reference>
</ItemGroup-->
</Reference-->
</ItemGroup>

</Project>

0 comments on commit e7365b8

Please sign in to comment.