| | | 1 | | using Dapper; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Features.Identity.CheckSsoAvailability; |
| | | 4 | | |
| | 0 | 5 | | public class CheckSsoAvailabilityService(SykiDbContext ctx) : ISykiService |
| | | 6 | | { |
| | | 7 | | private class Validator : AbstractValidator<CheckSsoAvailabilityIn> |
| | | 8 | | { |
| | 0 | 9 | | public Validator() |
| | | 10 | | { |
| | 0 | 11 | | RuleFor(x => x.Email).NotEmpty().WithError(InvalidEmail.I); |
| | 0 | 12 | | RuleFor(x => x.Email).Must(x => x.IsValidEmail()).WithError(InvalidEmail.I); |
| | 0 | 13 | | } |
| | | 14 | | } |
| | 0 | 15 | | private static readonly Validator V = new(); |
| | | 16 | | |
| | | 17 | | public async Task<OneOf<CheckSsoAvailabilityOut, SykiError>> Check(CheckSsoAvailabilityIn data) |
| | | 18 | | { |
| | 0 | 19 | | if (V.Run(data, out var error)) return error; |
| | | 20 | | |
| | 0 | 21 | | var domain = ExtractDomain(data.Email); |
| | 0 | 22 | | if (domain == null) return new CheckSsoAvailabilityOut { SsoEnabled = false }; |
| | | 23 | | |
| | | 24 | | const string sql = @" |
| | | 25 | | SELECT |
| | | 26 | | c.require_sso, |
| | | 27 | | c.provider_type |
| | | 28 | | FROM |
| | | 29 | | syki.sso_allowed_domains d |
| | | 30 | | INNER JOIN |
| | | 31 | | syki.sso_configurations c ON c.id = d.sso_configuration_id |
| | | 32 | | WHERE |
| | | 33 | | d.domain = @Domain |
| | | 34 | | AND |
| | | 35 | | c.is_active = true |
| | | 36 | | LIMIT 1 |
| | | 37 | | "; |
| | | 38 | | |
| | 0 | 39 | | var config = await ctx.Database.GetDbConnection().QueryFirstOrDefaultAsync<SsoConfigDto>(sql, new { Domain = dom |
| | 0 | 40 | | if (config == null) return new CheckSsoAvailabilityOut { SsoEnabled = false }; |
| | | 41 | | |
| | 0 | 42 | | return new CheckSsoAvailabilityOut |
| | 0 | 43 | | { |
| | 0 | 44 | | SsoEnabled = true, |
| | 0 | 45 | | SsoRequired = config.RequireSso, |
| | 0 | 46 | | ProviderType = config.ProviderType, |
| | 0 | 47 | | }; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | private static string? ExtractDomain(string email) |
| | | 51 | | { |
| | 0 | 52 | | if (email.IsEmpty()) return null; |
| | | 53 | | |
| | 0 | 54 | | var parts = email.Split('@'); |
| | 0 | 55 | | if (parts.Length != 2) return null; |
| | | 56 | | |
| | 0 | 57 | | return parts[1].Trim().ToLowerInvariant(); |
| | | 58 | | } |
| | | 59 | | } |