Skip to content

Commit

Permalink
Move IdentityServer to UserModule Infrastructure (#296)
Browse files Browse the repository at this point in the history
Move IdentityServer to UserModule Infrastructure
  • Loading branch information
bistok authored Dec 19, 2023
1 parent 0d18452 commit e00d241
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 42 deletions.
34 changes: 6 additions & 28 deletions src/API/CompanyName.MyMeetings.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
using CompanyName.MyMeetings.Modules.Administration.Infrastructure.Configuration;
using CompanyName.MyMeetings.Modules.Meetings.Infrastructure.Configuration;
using CompanyName.MyMeetings.Modules.Payments.Infrastructure.Configuration;
using CompanyName.MyMeetings.Modules.UserAccess.Application.IdentityServer;
using CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.Configuration;
using CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.Configuration.Identity;
using Hellang.Middleware.ProblemDetails;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Validation;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Server.HttpSys;
using Serilog;
using Serilog.Formatting.Compact;
using ILogger = Serilog.ILogger;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddSwaggerDocumentation();

ConfigureIdentityServer(services);
services.ConfigureIdentityService();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IExecutionContextAccessor, ExecutionContextAccessor>();
Expand All @@ -71,7 +71,7 @@ public void ConfigureServices(IServiceCollection services)
options.AddPolicy(HasPermissionAttribute.HasPermissionPolicyName, policyBuilder =>
{
policyBuilder.Requirements.Add(new HasPermissionAuthorizationRequirement());
policyBuilder.AddAuthenticationSchemes(IdentityServerAuthenticationDefaults.AuthenticationScheme);
policyBuilder.AddAuthenticationSchemes("Bearer");
});
});

Expand Down Expand Up @@ -100,7 +100,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IService

app.UseSwaggerDocumentation();

app.UseIdentityServer();
app.AddIdentityService();

if (env.IsDevelopment())
{
Expand Down Expand Up @@ -137,28 +137,6 @@ private static void ConfigureLogger()
_loggerForApi.Information("Logger configured");
}

private void ConfigureIdentityServer(IServiceCollection services)
{
services.AddIdentityServer()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiScopes(IdentityServerConfig.GetApiScopes())
.AddInMemoryApiResources(IdentityServerConfig.GetApis())
.AddInMemoryClients(IdentityServerConfig.GetClients())
.AddInMemoryPersistedGrants()
.AddProfileService<ProfileService>()
.AddDeveloperSigningCredential();

services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, x =>
{
x.Authority = "http://localhost:5000";
x.ApiName = "myMeetingsAPI";
x.RequireHttpsMetadata = false;
});
}

private void InitializeModules(ILifetimeScope container)
{
var httpContextAccessor = container.Resolve<IHttpContextAccessor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
<ItemGroup>
<ProjectReference Include="..\..\Meetings\IntegrationEvents\CompanyName.MyMeetings.Modules.Meetings.IntegrationEvents.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="4.1.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace CompanyName.MyMeetings.Modules.UserAccess.Application.Contracts
{
internal class CustomClaimTypes
public class CustomClaimTypes
{
internal const string Roles = "roles";
internal const string Email = "email";
internal const string Name = "name";
public const string Roles = "roles";
public const string Email = "email";
public const string Name = "name";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk" />
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="4.1.2" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.IdentityServer;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Validation;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.Configuration.Identity;

public static class IdentityConfiguration
{
public static IServiceCollection ConfigureIdentityService(this IServiceCollection services)
{
services.AddIdentityServer()
.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
.AddInMemoryApiScopes(IdentityServerConfig.GetApiScopes())
.AddInMemoryApiResources(IdentityServerConfig.GetApis())
.AddInMemoryClients(IdentityServerConfig.GetClients())
.AddInMemoryPersistedGrants()
.AddProfileService<ProfileService>()
.AddDeveloperSigningCredential();

services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, x =>
{
x.Authority = "http://localhost:5000";
x.ApiName = "myMeetingsAPI";
x.RequireHttpsMetadata = false;
});

return services;
}

public static IApplicationBuilder AddIdentityService(this IApplicationBuilder app)
{
app.UseIdentityServer();
return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using IdentityServer4;
using IdentityServer4.Models;

namespace CompanyName.MyMeetings.Modules.UserAccess.Application.IdentityServer
namespace CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.IdentityServer
{
public class IdentityServerConfig
internal class IdentityServerConfig
{
public static IEnumerable<ApiScope> GetApiScopes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using IdentityServer4.Models;
using IdentityServer4.Services;

namespace CompanyName.MyMeetings.Modules.UserAccess.Application.IdentityServer
namespace CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.IdentityServer
{
public class ProfileService : IProfileService
internal class ProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using IdentityServer4.Models;
using IdentityServer4.Validation;

namespace CompanyName.MyMeetings.API.Modules.UserAccess
namespace CompanyName.MyMeetings.Modules.UserAccess.Infrastructure.IdentityServer
{
public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
internal class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
private readonly IUserAccessModule _userAccessModule;

Expand Down

0 comments on commit e00d241

Please sign in to comment.