< Summary - Syki

Information
Class: Syki.Back.Features.Identity.CreateSsoConfiguration.CreateSsoConfigurationService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Identity/CreateSsoConfiguration/CreateSsoConfigurationService.cs
Tag: 56_26538939494
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 54
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor()100%210%
.cctor()100%210%
Create()0%7280%

File(s)

/home/runner/work/syki/syki/Back/Features/Identity/CreateSsoConfiguration/CreateSsoConfigurationService.cs

#LineLine coverage
 1using Syki.Back.Auth.Managers;
 2using Syki.Back.Domain.Identity;
 3
 4namespace Syki.Back.Features.Identity.CreateSsoConfiguration;
 5
 06public class CreateSsoConfigurationService(SykiDbContext ctx, SsoEncryptionManager encryption, SsoSchemeManager ssoSchem
 7{
 8    private class Validator : AbstractValidator<CreateSsoConfigurationIn>
 9    {
 010        public Validator()
 11        {
 012            RuleFor(x => x.ProviderType).IsInEnum().WithError(InvalidSsoProviderType.I);
 13
 014            RuleFor(x => x.Authority).NotEmpty().WithError(InvalidSsoAuthority.I);
 15
 016            RuleFor(x => x.ClientId).NotEmpty().WithError(InvalidSsoClientId.I);
 017            RuleFor(x => x.ClientId).MinimumLength(5).WithError(InvalidSsoClientId.I);
 18
 019            RuleFor(x => x.ClientSecret).NotEmpty().WithError(InvalidSsoClientSecret.I);
 020            RuleFor(x => x.ClientSecret).MinimumLength(10).WithError(InvalidSsoClientSecret.I);
 021        }
 22    }
 023    private static readonly Validator V = new();
 24
 25    public async Task<OneOf<CreateSsoConfigurationOut, SykiError>> Create(CreateSsoConfigurationIn data)
 26    {
 027        if (V.Run(data, out var error)) return error;
 28
 029        var authorityError = data.Authority.ValidateSsoAuthority();
 030        if (authorityError != null) return authorityError;
 31
 032        var userEmail = await ctx.Users.Where(x => x.Id == ctx.RequestUser.Id).Select(x => x.Email).FirstAsync();
 33
 034        var domain = userEmail!.Split('@').Last().NormalizeSsoDomain();
 035        if (domain == null) return InvalidSsoAllowedDomains.I;
 36
 037        var domainExists = await ctx.WebSsoAllowedDomains.AnyAsync(d => d.Domain == domain);
 038        if (domainExists) return SsoDomainAlreadyConfigured.I;
 39
 040        var config = new SsoConfiguration(
 041            ctx.RequestUser.InstitutionId,
 042            data.ProviderType,
 043            data.Authority.TrimEnd('/'),
 044            data.ClientId.Trim(),
 045            encryption.Encrypt(data.ClientSecret),
 046            [domain]);
 47
 048        await ctx.SaveChangesAsync(config);
 49
 050        ssoSchemeManager.RegisterScheme(config);
 51
 052        return new CreateSsoConfigurationOut { Id = config.PublicId };
 053    }
 54}