| | | 1 | | using Syki.Back.Auth.Managers; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Features.Identity.UpdateSsoConfiguration; |
| | | 4 | | |
| | 0 | 5 | | public class UpdateSsoConfigurationService(SykiDbContext ctx, SsoEncryptionManager encryption, SsoSchemeManager ssoSchem |
| | | 6 | | { |
| | | 7 | | private class Validator : AbstractValidator<UpdateSsoConfigurationIn> |
| | | 8 | | { |
| | 0 | 9 | | public Validator() |
| | | 10 | | { |
| | 0 | 11 | | RuleFor(x => x.ProviderType).IsInEnum().WithError(InvalidSsoProviderType.I); |
| | 0 | 12 | | RuleFor(x => x.Authority).NotEmpty().WithError(InvalidSsoAuthority.I); |
| | 0 | 13 | | RuleFor(x => x.ClientId).NotEmpty().WithError(InvalidSsoClientId.I); |
| | 0 | 14 | | RuleFor(x => x.ClientId).MinimumLength(5).WithError(InvalidSsoClientId.I); |
| | 0 | 15 | | } |
| | | 16 | | } |
| | 0 | 17 | | private static readonly Validator V = new(); |
| | | 18 | | |
| | | 19 | | public async Task<OneOf<UpdateSsoConfigurationOut, SykiError>> Update(Guid id, UpdateSsoConfigurationIn data) |
| | | 20 | | { |
| | 0 | 21 | | if (V.Run(data, out var error)) return error; |
| | | 22 | | |
| | 0 | 23 | | var authorityError = data.Authority.ValidateSsoAuthority(); |
| | 0 | 24 | | if (authorityError != null) return authorityError; |
| | | 25 | | |
| | 0 | 26 | | var config = await ctx.WebSsoConfigurations |
| | 0 | 27 | | .Where(x => x.PublicId == id && x.InstitutionId == ctx.RequestUser.InstitutionId) |
| | 0 | 28 | | .FirstOrDefaultAsync(); |
| | | 29 | | |
| | 0 | 30 | | if (config == null) return SsoConfigurationNotFound.I; |
| | | 31 | | |
| | 0 | 32 | | var clientSecret = string.IsNullOrEmpty(data.ClientSecret) |
| | 0 | 33 | | ? config.ClientSecret |
| | 0 | 34 | | : encryption.Encrypt(data.ClientSecret); |
| | | 35 | | |
| | 0 | 36 | | config.Update( |
| | 0 | 37 | | data.ProviderType, |
| | 0 | 38 | | data.Authority.TrimEnd('/'), |
| | 0 | 39 | | data.ClientId.Trim(), |
| | 0 | 40 | | clientSecret, |
| | 0 | 41 | | data.IsActive, |
| | 0 | 42 | | data.RequireSso); |
| | | 43 | | |
| | 0 | 44 | | await ctx.SaveChangesAsync(); |
| | | 45 | | |
| | 0 | 46 | | ssoSchemeManager.RegisterScheme(config); |
| | | 47 | | |
| | 0 | 48 | | return new UpdateSsoConfigurationOut { Id = config.PublicId }; |
| | 0 | 49 | | } |
| | | 50 | | } |