public virtual async Task <Session> GetSessionFromCache(Session authData)
        {
            Session session = null;

            try
            {
                var wcfSession = await this._cacheWCF.GetSessionByUsernameAsync(authData.Username);

                session = wcfSession;
                //session = this._mapper.Map<Session>(wcfSession);
            }
            catch (Exception ex)
            {
            }

            return(session);
        }
Example #2
0
        public virtual bool Deauthorize(Session authData)
        {
            var authResult = this._authInterface.Deauthorize(authData).Result;

            if (authResult)
            {
                this._loggerService.LogMessage(
                    string.Format(StringContainer.SuccessfullyLoggedOut, authData.Username), LogLevel.Info);
            }
            else
            {
                this._loggerService.LogMessage(string.Format(StringContainer.FailedLoggingOut, authData.Username),
                                               LogLevel.Info);
            }

            this._loggerService.LogMessage(
                string.Format(CommonContainer.InfoMethodCompleted, nameof(this.Deauthorize)), LogLevel.Info);

            return(authResult);
        }
Example #3
0
        public virtual Session Authorize(Session authData)
        {
            var authResult = this._authInterface.Authorize(authData).Result;

            if (authResult.TokenValidity > DateTime.Now)
            {
                this._loggerService.LogMessage(
                    string.Format(StringContainer.SuccessfullyLoggedIn, authData.Username), LogLevel.Info);
            }
            else
            {
                this._loggerService.LogMessage(string.Format(StringContainer.FailedLoggingIn, authData.Username),
                                               LogLevel.Info);
            }

            this._loggerService.LogMessage(
                string.Format(CommonContainer.InfoMethodCompleted, nameof(this.Authorize)), LogLevel.Info);

            authResult.Password = string.Empty;

            return(authResult);
        }
        public async Task <Session> Authorize(Session authData)
        {
            if (!this.ValidateUserData(authData))
            {
                throw new ArgumentNullException(nameof(authData));
            }

            var user = await this._cacheWCF.GetUserByUsernameAsync(authData.Username);

            // get session from cache by username
            var configuration = this._configurationRepository.Get();

            var sessionFromCache = await this.GetSessionFromCache(authData);

            if (sessionFromCache != null && sessionFromCache.FailedLoginAttempts.Count >= configuration.MaximumLoginFailures)
            {
                if (sessionFromCache.FailedLoginAttempts.Max() > DateTime.Now.AddHours(-(configuration.AccountBlockTime)))
                {
                    throw new SessionException(
                              $"Account blocked till {sessionFromCache.FailedLoginAttempts.Max().AddHours(configuration.AccountBlockTime)}",
                              ErrorCode.AccountBlocked,
                              sessionFromCache.FailedLoginAttempts.Max().AddHours(configuration.AccountBlockTime));
                }
            }

            if (sessionFromCache?.FailedLoginAttempts != null)
            {
                authData.FailedLoginAttempts = sessionFromCache?.FailedLoginAttempts;
            }
            else
            {
                authData.FailedLoginAttempts = new List <DateTime>();
            }

            authData.AccountPhoto = user.AccountPhoto;
            authData.UserId       = user.UserID;

            // generate json web tokens
            this._jwtService.SecretKey = configuration.JWTSecretKey;
            var jwtContainerModel = this._jwtService.CreateContainer(authData);

            authData.Token         = this._jwtService.GenerateToken(jwtContainerModel);
            authData.TokenValidity = DateTime.Now.AddMinutes(jwtContainerModel.ExpireMinutes);


            // cleanup the failed login attempts
            if (authData.FailedLoginAttempts == null)
            {
                authData.FailedLoginAttempts = new List <DateTime>();
            }
            else
            {
                authData.FailedLoginAttempts.Clear();
            }


            // add session to cache
            await this._cacheWCF.AddSessionAsync(authData);

            return(authData);
        }
 private bool ValidateUserData(Session authData)
 {
     return(authData != null && !string.IsNullOrEmpty(authData.Username) && !string.IsNullOrEmpty(authData.Password));
 }
 public async Task <bool> Deauthorize(Session authData)
 {
     return(await Task.Run(() => true));
 }