コード例 #1
0
        public async Task <Result <AuthenticatedTenantModel> > Handle(TenantAuthenticateCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("New request with {logon} and {password}", request.Logon, request.Password);

            var secret = _appSettings.GetValue <string>("Secret");

            if (secret.Length < 15)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.SecretKeyTooShort.ToString()));
            }

            Maybe <Tenant> tenant = await _repository.SingleOrDefaultAsync(x => x.Logon.Equals(request.Logon), tracking : true, cancellationToken, x => x.Enterprise);

            if (tenant.HasNoValue)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.NotFound.ToString()));
            }

            var correctPassword = _hashing.IsValidHash(tenant.Value.Password, tenant.Value.Salt, request.Password);

            if (correctPassword is false)
            {
                return(Result.Failure <AuthenticatedTenantModel>(ErrorType.IncorrectUserPassword.ToString()));
            }

            var parsedExpiration = double.TryParse(_appSettings.GetValue <string>("TokenExpiration"), out var tokenExpiration);

            if (parsedExpiration is false)
            {
                tokenExpiration = 60;
            }

            tenant.Value.Token = _jwtTokenFactory.CreateToken(secret, tokenExpiration, tenant.Value.ID.ToString(), tenant.Value.Role.ToString(), tenant.Value.Enterprise.NormalizedEnterpriseName);

            return(Result.Success(_mapper.Map <AuthenticatedTenantModel>(tenant.Value)));
        }