Skip to content

Commit

Permalink
Ticket #745 : Finish the ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Jun 4, 2024
1 parent 6a366a3 commit 8bd4c45
Show file tree
Hide file tree
Showing 55 changed files with 233 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public object Clone()
};
}

public static UserCredential CreatePassword(string pwd) => new UserCredential { Id = Guid.NewGuid().ToString(), CredentialType = "pwd", Value = PasswordHelper.ComputeHash(pwd) };
public static UserCredential CreatePassword(string pwd, bool isBase64Encoded) => new UserCredential { Id = Guid.NewGuid().ToString(), CredentialType = "pwd", Value = PasswordHelper.ComputeHash(pwd, isBase64Encoded) };
}

public enum OTPAlgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace SimpleIdServer.IdServer.Helpers
{
public static class PasswordHelper
{
public static string ComputeHash(string password)
public static string ComputeHash(string password, bool isBase64Encoding)
{
using (var sha256 = SHA256.Create())
{
var hashPayload = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
return Encoding.UTF8.GetString(hashPayload);
return isBase64Encoding ? Convert.ToBase64String(hashPayload) : Encoding.UTF8.GetString(hashPayload);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Microsoft.Extensions.Options;
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.Helpers;
using SimpleIdServer.IdServer.Options;
using SimpleIdServer.IdServer.Stores;
using SimpleIdServer.IdServer.UI.Services;
using SimpleIdServer.IdServer.UI.ViewModels;
Expand All @@ -16,10 +18,16 @@ public interface IPasswordAuthenticationService : IUserAuthenticationService
public class PasswordAuthenticationService : GenericAuthenticationService<AuthenticatePasswordViewModel>, IPasswordAuthenticationService
{
private readonly IEnumerable<IIdProviderAuthService> _authServices;
private readonly IdServerHostOptions _options;

public PasswordAuthenticationService(IEnumerable<IIdProviderAuthService> authServices, IAuthenticationHelper authenticationHelper, IUserRepository userRepository) : base(authenticationHelper, userRepository)
public PasswordAuthenticationService(
IEnumerable<IIdProviderAuthService> authServices,
IOptions<IdServerHostOptions> options,
IAuthenticationHelper authenticationHelper,
IUserRepository userRepository) : base(authenticationHelper, userRepository)
{
_authServices = authServices;
_options = options.Value;
}

public override string Amr => Constants.Areas.Password;
Expand Down Expand Up @@ -52,7 +60,7 @@ protected override Task<CredentialsValidationResult> Validate(string realm, User
else
{
var credential = authenticatedUser.Credentials.FirstOrDefault(c => c.CredentialType == Constants.Areas.Password && c.IsActive);
var hash = PasswordHelper.ComputeHash(viewModel.Password);
var hash = PasswordHelper.ComputeHash(viewModel.Password, _options.IsPasswordEncodeInBase64);
if (credential == null || credential.Value != hash) return Task.FromResult(CredentialsValidationResult.Error(ValidationStatus.INVALIDCREDENTIALS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ async Task<IActionResult> UpdateUser()
{
var user = await UserRepository.GetBySubject(viewModel.Login, prefix, cancellationToken);
var passwordCredential = user.Credentials.FirstOrDefault(c => c.CredentialType == UserCredential.PWD);
if (passwordCredential != null) passwordCredential.Value = PasswordHelper.ComputeHash(viewModel.Password);
if (passwordCredential != null) passwordCredential.Value = PasswordHelper.ComputeHash(viewModel.Password, Options.IsPasswordEncodeInBase64);
else user.Credentials.Add(new UserCredential
{
Id = Guid.NewGuid().ToString(),
Value = PasswordHelper.ComputeHash(viewModel.Password),
Value = PasswordHelper.ComputeHash(viewModel.Password, Options.IsPasswordEncodeInBase64),
CredentialType = UserCredential.PWD,
IsActive = true
});
Expand All @@ -118,7 +118,7 @@ protected override void EnrichUser(User user, PwdRegisterViewModel viewModel)
Id = Guid.NewGuid().ToString(),
CredentialType = "pwd",
IsActive = true,
Value = PasswordHelper.ComputeHash(viewModel.Password)
Value = PasswordHelper.ComputeHash(viewModel.Password, Options.IsPasswordEncodeInBase64)
});
user.Name = viewModel.Login;
if (Options.IsEmailUsedDuringAuthentication) user.Email = viewModel.Login;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using MassTransit.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SimpleIdServer.IdServer.Api;
using SimpleIdServer.IdServer.Domains;
using SimpleIdServer.IdServer.Helpers;
using SimpleIdServer.IdServer.Jwt;
using SimpleIdServer.IdServer.Options;
using SimpleIdServer.IdServer.Pwd.UI.ViewModels;
using SimpleIdServer.IdServer.Stores;
using SimpleIdServer.IdServer.UI.Services;
Expand All @@ -19,6 +22,7 @@ namespace SimpleIdServer.IdServer.Pwd.UI;
[Area(Constants.Areas.Password)]
public class ResetController : BaseController
{
private readonly IdServerHostOptions _options;
private readonly IEnumerable<IResetPasswordService> _resetPasswordServices;
private readonly IAuthenticationHelper _authenticationHelper;
private readonly IConfiguration _configuration;
Expand All @@ -28,6 +32,7 @@ public class ResetController : BaseController
private readonly ILogger<ResetController> _logger;

public ResetController(
IOptions<IdServerHostOptions> options,
ITokenRepository tokenRepository,
IJwtBuilder jwtBuilder,
IEnumerable<IResetPasswordService> resetPasswordServices,
Expand All @@ -38,6 +43,7 @@ public ResetController(
ITransactionBuilder transactionBuilder,
ILogger<ResetController> logger) : base(tokenRepository, jwtBuilder)
{
_options = options.Value;
_resetPasswordServices = resetPasswordServices;
_authenticationHelper = authenticationHelper;
_configuration = configuration;
Expand Down Expand Up @@ -202,7 +208,7 @@ public async Task<IActionResult> Confirm([FromRoute] string prefix, ConfirmReset
user.Credentials.Add(credential);
}

credential.Value = PasswordHelper.ComputeHash(viewModel.Password);
credential.Value = PasswordHelper.ComputeHash(viewModel.Password, _options.IsPasswordEncodeInBase64);
_userRepository.Update(user);
await transaction.Commit(cancellationToken);
viewModel.IsPasswordUpdated = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ public class IdServerConfiguration
SimpleIdServer.IdServer.Constants.StandardScopes.OfflineAccessScope,
SimpleIdServer.IdServer.Constants.StandardScopes.CredentialConfigurations,
SimpleIdServer.IdServer.Constants.StandardScopes.CredentialInstances,
SimpleIdServer.IdServer.Constants.StandardScopes.Acrs,
UniversityDegreeScope
};

public static ICollection<User> Users => new List<User>
{
UserBuilder.Create("administrator", "password", "Administrator").SetFirstname("Administrator").SetEmail("adm@email.com").SetPicture("https://cdn-icons-png.flaticon.com/512/149/149071.png").GenerateRandomTOTPKey().Build(),
UserBuilder.Create("user", "password", "User").SetPicture("https://cdn-icons-png.flaticon.com/512/149/149071.png").Build()
UserBuilder.Create("administrator", "password", "Administrator", isBase64Encoded: true).SetFirstname("Administrator").SetEmail("adm@email.com").SetPicture("https://cdn-icons-png.flaticon.com/512/149/149071.png").GenerateRandomTOTPKey().Build(),
UserBuilder.Create("user", "password", "User", isBase64Encoded: true).SetPicture("https://cdn-icons-png.flaticon.com/512/149/149071.png").Build()
};

public static ICollection<Client> Clients => new List<Client>
Expand Down
29 changes: 14 additions & 15 deletions src/IdServer/SimpleIdServer.IdServer.SqlSugar.Startup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
});
}


var section = builder.Configuration.GetSection(nameof(StorageConfiguration));
var conf = section.Get<StorageConfiguration>();

Expand All @@ -101,7 +100,6 @@
ConfigureCentralizedConfiguration(builder);

var app = builder.Build();
// SeedData(app, identityServerConfiguration.SCIMBaseUrl);
app.UseCors("AllowAll");
if (identityServerConfiguration.IsForwardedEnabled)
{
Expand Down Expand Up @@ -145,6 +143,7 @@ void ConfigureIdServer(IServiceCollection services)
if (!string.IsNullOrWhiteSpace(identityServerConfiguration.SessionCookieNamePrefix))
cb.SessionCookieName = identityServerConfiguration.SessionCookieNamePrefix;
cb.Authority = identityServerConfiguration.Authority;
cb.IsPasswordEncodeInBase64 = true;
}, cookie: c =>
{
if(!string.IsNullOrWhiteSpace(identityServerConfiguration.AuthCookieNamePrefix))
Expand Down Expand Up @@ -272,7 +271,7 @@ void ConfigureDataProtection(IDataProtectionBuilder dataProtectionBuilder)
void SeedData(WebApplication webApplication)
{
var dbContext = webApplication.Services.GetRequiredService<DbContext>();
// dbContext.Migrate();
dbContext.Migrate();
var transactionBuilder = webApplication.Services.GetRequiredService<ITransactionBuilder>();
var realmRepository = webApplication.Services.GetRequiredService<IRealmRepository>();
var scopeRepository = webApplication.Services.GetRequiredService<IScopeRepository>();
Expand All @@ -293,39 +292,39 @@ void SeedData(WebApplication webApplication)
var configurationDefinitionRepository = webApplication.Services.GetRequiredService<IConfigurationDefinitionStore>();
using (var transaction = transactionBuilder.Build())
{
// foreach (var realm in IdServerConfiguration.Realms)
// realmRepository.Add(realm);
foreach (var realm in IdServerConfiguration.Realms)
realmRepository.Add(realm);

foreach(var scope in IdServerConfiguration.Scopes)
scopeRepository.Add(scope);

foreach (var user in IdServerConfiguration.Users)
userRepository.Add(user);

foreach (var client in IdServerConfiguration.Clients)
clientRepository.Add(client);

foreach (var umaPendingRequest in IdServerConfiguration.PendingRequests)
umaPendingRequestRepository.Add(umaPendingRequest);

foreach (var umaResource in IdServerConfiguration.Resources)
umaResourceRepository.Add(umaResource);

foreach (var gotifySession in IdServerConfiguration.Sessions)
gotifySessionRepository.Add(gotifySession);

foreach (var language in IdServerConfiguration.Languages)
languageRepository.Add(language);

foreach (var definition in IdServerConfiguration.ProviderDefinitions)
providerDefinitionRepository.Add(definition);

foreach (var authProvider in IdServerConfiguration.Providers)
authSchemeProviderRepository.Add(authProvider);

foreach (var idProvisioningDef in IdServerConfiguration.IdentityProvisioningDefLst)
idProvisioningDefRepository.Add(idProvisioningDef);

foreach (var registrationWorkflow in IdServerConfiguration.RegistrationWorkflows)
registrationWorkflowRepository.Add(registrationWorkflow);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public ConfigurationDefinitionStore(StoreDbContext dbContext)
_dbContext = dbContext;
}

public void Add(ConfigurationDefinition configurationDefinition)
{
_dbContext.Definitions.Add(configurationDefinition);
}

public Task<List<ConfigurationDefinition>> GetAll(CancellationToken cancellationToken)
=> _dbContext.Definitions.Include(c => c.Records).ThenInclude(r => r.Values).ThenInclude(r => r.Translations)
.Include(c => c.Records).ThenInclude(r => r.Translations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class SugarApiResource
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; }
public string Name { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? Audience { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? Description { get; set; } = null;
public DateTime CreateDateTime { get; set; }
public DateTime UpdateDateTime { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ public class SugarAuditEvent
public string Realm { get; set; } = null!;
public bool IsError { get; set; }
public string Description { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? ErrorMessage { get; set; } = null;
public DateTime CreateDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public string? ClientId { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? UserName { get; set; } = null;
[SugarColumn(IsNullable = true, Length = 5000)]
public string? RequestJSON { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? RedirectUrl { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? AuthMethod { get; set; } = null;
public string Scopes { get; set; } = null!;
public string Claims { get; set; } = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SugarAuthenticationContextClassReference
public string AuthenticationMethodReferences { get; set; } = null!;
public DateTime CreateDateTime { get; set; }
public DateTime UpdateDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public string? RegistrationWorkflowId { get; set; }
[Navigate(NavigateType.ManyToOne, nameof(RegistrationWorkflowId))]
public SugarRegistrationWorkflow? RegistrationWorkflow { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class SugarAuthenticationSchemeProvider
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; } = null!;
public string Name { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? DisplayName { get; set; }
[SugarColumn(IsNullable = true)]
public string? Description { get; set; }
public DateTime CreateDateTime { get; set; }
public DateTime UpdateDateTime { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public class SugarAuthenticationSchemeProviderDefinition
{
[SugarColumn(IsPrimaryKey = true)]
public string Name { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? Description { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? Image { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? HandlerFullQualifiedName { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? OptionsFullQualifiedName { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? OptionsName { get; set; } = null;
[Navigate(NavigateType.OneToMany, nameof(SugarAuthenticationSchemeProvider.AuthSchemeProviderDefinitionName))]
public List<SugarAuthenticationSchemeProvider> AuthSchemeProviders { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public class SugarAuthenticationSchemeProviderMapper
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; } = null!;
public string Name { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? SourceClaimName { get; set; } = null;
public MappingRuleTypes MapperType { get; set; }
[SugarColumn(IsNullable = true)]
public string? TargetUserAttribute { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? TargetUserProperty { get; set; } = null;
public string IdProviderId { get; set; } = null!;
[Navigate(NavigateType.ManyToOne, nameof(IdProviderId))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class SugarAuthorizedResource
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string Resource { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? Audience { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? AuthorizedScopeId { get; set; } = null;

public static SugarAuthorizedResource Transform(AuthorizedResource a)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ public class SugarBCAuthorize
{
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; } = null!;
[SugarColumn(IsNullable = true)]
public string? ClientId { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? UserId { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? NotificationToken { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? NotificationMode { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? NotificationEdp { get; set; } = null;
[SugarColumn(IsNullable = true)]
public int? Interval { get; set; } = null;
public BCAuthorizeStatus LastStatus { get; set; }
public DateTime ExpirationDateTime { get; set; }
public DateTime UpdateDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime? RejectionSentDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime? NextFetchTime { get; set; }
public string Realm { get; set; } = null!;
[SugarColumn(IsNullable = true, Length = 5000)]
public string? SerializedAuthorizationDetails { get; set; } = null;
public string Scopes { get; set; }
[Navigate(NavigateType.OneToMany, nameof(SugarBCAuthorizeHistory.BCAuthorizeId))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class SugarBCAuthorizeHistory
[SugarColumn(IsPrimaryKey = true)]
public int Id { get; set; }
public DateTime StartDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime? EndDateTime { get; set; }
[SugarColumn(IsNullable = true)]
public string? Message { get; set; } = null;
public BCAuthorizeStatus Status { get; set; }
public string BCAuthorizeId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ public class SugarCertificateAuthority
public string Id { get; set; } = null!;
public string SubjectName { get; set; } = null!;
public CertificateAuthoritySources Source { get; set; }
[SugarColumn(IsNullable = true)]
public StoreLocation? StoreLocation { get; set; } = null;
[SugarColumn(IsNullable = true)]
public StoreName? StoreName { get; set; } = null;
[SugarColumn(IsNullable = true)]
public X509FindType? FindType { get; set; } = null;
[SugarColumn(IsNullable = true)]
public string? FindValue { get; set; } = null;
[SugarColumn(IsNullable = true, Length = 5000)]
public string? PublicKey { get; set; } = null;
[SugarColumn(IsNullable = true, Length = 5000)]
public string? PrivateKey { get; set; } = null;
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
Expand Down
Loading

0 comments on commit 8bd4c45

Please sign in to comment.