<commit_before>﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Iam.Common;
using Iam.Common.Helpers;
using IdentityModel;
using IdentityServer3.AspNetIdentity;
using IdentityServer3.Core;
using IdentityServer3.Core.Models;
using JetBrains.Annotations;

#endregion

namespace Iam.Identity
{
    [UsedImplicitly]
    public class UserService : AspNetIdentityUserService<IamUser, string>
    {
        private readonly TenantService _tenantService;
        private string _schema;

        public UserService(
            IamUserManager userManager,
            TenantService tenantService)
            : base(userManager)
        {
            _tenantService = tenantService;
        }

        public override async Task AuthenticateLocalAsync(LocalAuthenticationContext ctx)
        {
            var clientId = ctx.SignInMessage.ClientId;
            var tenant = ctx.SignInMessage.Tenant;

            if (clientId == null)
            {
                var isWsfpRequest = WsFedProtocolHelper.IsWsFedProtocolRequest(ctx.SignInMessage.ReturnUrl);

                Ensure.Equal(isWsfpRequest, true);

                var realm = WsFedProtocolHelper.GetRealm(ctx.SignInMessage.ReturnUrl);
                var wsfp = _tenantService.GetWsFedProtocolMappingByRealm(realm);
                var mapping = _tenantService.GetClientMapping(wsfp.Realm);

                _schema = mapping.TenantId;
            }
            else if (clientId == AppSettings.IamClientId)
            {
                _schema = tenant;
            }
            else
            {
                var mapping = _tenantService.GetClientMapping(clientId);
                
                _schema = mapping.TenantId;<commit_msg>Minor refactor<commit_after>﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Iam.Common;
using Iam.Common.Helpers;
using IdentityModel;
using IdentityServer3.AspNetIdentity;
using IdentityServer3.Core;
using IdentityServer3.Core.Models;
using JetBrains.Annotations;

#endregion

namespace Iam.Identity
{
    [UsedImplicitly]
    public class UserService : AspNetIdentityUserService<IamUser, string>
    {
        private readonly TenantService _tenantService;
        private string _schema;

        public UserService(
            IamUserManager userManager,
            TenantService tenantService)
            : base(userManager)
        {
            _tenantService = tenantService;
        }

        public override async Task AuthenticateLocalAsync(LocalAuthenticationContext ctx)
        {
            var clientId = ctx.SignInMessage.ClientId;
            
            if (clientId == null)
            {
                var isWsfpRequest = WsFedProtocolHelper.IsWsFedProtocolRequest(ctx.SignInMessage.ReturnUrl);

                Ensure.Equal(isWsfpRequest, true);

                var realm = WsFedProtocolHelper.GetRealm(ctx.SignInMessage.ReturnUrl);
                var wsfp = _tenantService.GetWsFedProtocolMappingByRealm(realm);
                var mapping = _tenantService.GetClientMapping(wsfp.Realm);

                _schema = mapping.TenantId;
            }
            else if (clientId == AppSettings.IamClientId)
            {
                _schema = ctx.SignInMessage.Tenant;
            }
            else
            {
                var mapping = _tenantService.GetClientMapping(clientId);
                
                _schema = mapping.TenantId;
{'fraction_non_alphanumeric': 0.06775832862789384, 'fraction_numerical': 0.0016939582156973462, 'mean_word_length': 2.1976534296028882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3209641', 'n_tokens_mistral': 956, 'n_tokens_neox': 935, 'n_words': 179}