Skip to content

Refactor: move Refresh Token generation logic to AccountManagement #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion application/account-management/Core/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PlatformPlatform.AccountManagement.Database;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;
using PlatformPlatform.AccountManagement.Features.Users.Shared;
using PlatformPlatform.AccountManagement.Integrations.Gravatar;
using PlatformPlatform.SharedKernel.Configuration;
Expand Down Expand Up @@ -30,6 +33,17 @@ public static IServiceCollection AddAccountManagementServices(this IServiceColle

return services
.AddSharedServices<AccountManagementDbContext>(Assembly)
.AddScoped<AvatarUpdater>();
.AddScoped<AvatarUpdater>()
.AddAuthentication();
}


private static IServiceCollection AddAuthentication(this IServiceCollection services)
{
return services
.AddScoped<IPasswordHasher<object>, PasswordHasher<object>>()
.AddScoped<OneTimePasswordHelper>()
.AddScoped<RefreshTokenGenerator>()
.AddScoped<AuthenticationTokenService>();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using JetBrains.Annotations;
using Mapster;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Commands;
using PlatformPlatform.AccountManagement.Features.Users.Domain;
using PlatformPlatform.AccountManagement.Features.Users.Shared;
using PlatformPlatform.AccountManagement.Integrations.Gravatar;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Telemetry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JetBrains.Annotations;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Telemetry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using JetBrains.Annotations;
using Mapster;
using Microsoft.AspNetCore.Http;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;
using PlatformPlatform.AccountManagement.Features.Users.Domain;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Domain;
using PlatformPlatform.SharedKernel.Telemetry;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace PlatformPlatform.AccountManagement.Features.Authentication.Domain;

public static class AuthenticationTokenHttpKeys
{
public const string RefreshTokenHttpHeaderKey = "x-refresh-token";

public const string AccessTokenHttpHeaderKey = "x-access-token";

public const string AntiforgeryTokenHttpHeaderKey = "x-xsrf-token";

public const string RefreshAuthenticationTokensHeaderKey = "x-refresh-authentication-tokens-required";

// __Host prefix ensures the cookie is sent only to the host, requires Secure, HTTPS, Path=/ and no Domain specified
public const string RefreshTokenCookieName = "__Host_Refresh_Token";

public const string AccessTokenCookieName = "__Host_Access_Token";

public const string AntiforgeryTokenCookieName = "__Host_Xsrf_Token";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Text;
using Microsoft.AspNetCore.Identity;

namespace PlatformPlatform.SharedKernel.Authentication;
namespace PlatformPlatform.AccountManagement.Features.Authentication.Domain;

public class OneTimePasswordHelper(IPasswordHasher<object> passwordHasher)
public sealed class OneTimePasswordHelper(IPasswordHasher<object> passwordHasher)
{
public static string GenerateOneTimePassword(int length)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Http;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;

namespace PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
namespace PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;

public sealed class AuthenticationTokenService(
RefreshTokenGenerator refreshTokenGenerator,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.SharedKernel.Authentication.TokenSigning;

namespace PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
namespace PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;

public sealed class RefreshTokenGenerator(ITokenSigningClient tokenSigningClient)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JetBrains.Annotations;
using PlatformPlatform.SharedKernel.StronglyTypedIds;

namespace PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
namespace PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;

[PublicAPI]
[IdPrefix("rt")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using JetBrains.Annotations;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Domain;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Telemetry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Domain;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Integrations.Email;
using PlatformPlatform.SharedKernel.Telemetry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using FluentValidation;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Domain;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Integrations.Email;
using PlatformPlatform.SharedKernel.Validation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using JetBrains.Annotations;
using Mapster;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain.TokenGeneration;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Commands;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Domain;
using PlatformPlatform.AccountManagement.Features.Tenants.Commands;
using PlatformPlatform.AccountManagement.Features.Users.Domain;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.SharedKernel.Cqrs;
using PlatformPlatform.SharedKernel.Telemetry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Microsoft.AspNetCore.Identity;
using NSubstitute;
using PlatformPlatform.AccountManagement.Database;
using PlatformPlatform.AccountManagement.Features.Authentication.Domain;
using PlatformPlatform.AccountManagement.Features.EmailConfirmations.Domain;
using PlatformPlatform.AccountManagement.Features.Signups.Commands;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Tests;
using PlatformPlatform.SharedKernel.Tests.Persistence;
using PlatformPlatform.SharedKernel.Validation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace PlatformPlatform.SharedKernel.Authentication.TokenGeneration;

internal static class SecurityTokenDescriptorExtensions
public static class SecurityTokenDescriptorExtensions
{
internal static string GenerateToken(
public static string GenerateToken(
this SecurityTokenDescriptor tokenDescriptor,
DateTimeOffset expires,
string issuer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
using Azure.Security.KeyVault.Secrets;
using FluentValidation;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using PlatformPlatform.SharedKernel.Authentication;
using PlatformPlatform.SharedKernel.Authentication.TokenGeneration;
using PlatformPlatform.SharedKernel.Authentication.TokenSigning;
using PlatformPlatform.SharedKernel.DomainEvents;
Expand Down Expand Up @@ -73,11 +71,7 @@ public static ITokenSigningClient GetTokenSigningService()
private static IServiceCollection AddAuthentication(this IServiceCollection services)
{
return services
.AddScoped<IPasswordHasher<object>, PasswordHasher<object>>()
.AddScoped<OneTimePasswordHelper>()
.AddScoped<RefreshTokenGenerator>()
.AddScoped<AccessTokenGenerator>()
.AddScoped<AuthenticationTokenService>();
.AddScoped<AccessTokenGenerator>();
}

private static IServiceCollection AddDefaultJsonSerializerOptions(this IServiceCollection services)
Expand Down