Ejemplo n.º 1
0
        public UserLoginResponseModel Login([FromBody] UserLoginRequestModel userModel)
        {
            var responseMessage = this.TryExecuteOperation(() =>
            {
                var user = this.unitOfWork.userRepository.All()
                           .SingleOrDefault(
                    x => x.Username == userModel.Username &&
                    x.AuthCode == userModel.AuthCode);
                if (user == null)
                {
                    //throw new ArgumentException("User is not registered!");
                    return(new UserLoginResponseModel()
                    {
                        DisplayName = "",
                        AccessToken = ""
                    });
                }

                if (user.AccessToken == null)
                {
                    user.AccessToken = SessionGenerator.GenerateSessionKey(user.Id);
                    this.unitOfWork.userRepository.Update(user.Id, user);
                }

                return(UserLoginResponseModel.FromEntity(user));
            });

            return(responseMessage);
        }
        private List <string> GetTimestamps(DateTime today, int count)
        {
            var sessionGenerator = new SessionGenerator();

            sessionGenerator.AddTimeframe(today.AddDays(-3).AddHours(08), TimeSpan.FromHours(2), 3);
            sessionGenerator.AddTimeframe(today.AddDays(-2).AddHours(13), TimeSpan.FromHours(2), 1);
            sessionGenerator.AddTimeframe(today.AddDays(-2).AddHours(17), TimeSpan.FromHours(5), 3);
            sessionGenerator.AddTimeframe(today.AddDays(-1).AddHours(09), TimeSpan.FromHours(3), 2);

            return(sessionGenerator.GetTimestamps(count + 1));
        }
Ejemplo n.º 3
0
        public HttpResponseMessage Register([FromBody] UserRegisterRequestModel userModel)
        {
            var responseMessage = this.TryExecuteOperation(() =>
            {
                User user = UserRegisterRequestModel.ToEntity(userModel);
                UserValidator.ValidateUsername(user.Username);
                UserValidator.ValidateNickname(user.DisplayName);
                UserValidator.ValidateAuthCode(user.AuthCode);

                var doesUserExist =
                    this.unitOfWork.userRepository.All()
                    .FirstOrDefault(
                        x =>
                        x.Username.ToLower() == user.Username.ToLower() ||
                        x.DisplayName.ToLower() == user.DisplayName.ToLower());
                if (doesUserExist != null)
                {
                    //throw new InvalidOperationException("User already exist in the database!");
                    return(this.Request.CreateResponse(HttpStatusCode.Created, new UserRegisterResponseModel()
                    {
                        DisplayName = "",
                        AccessToken = ""
                    })
                           );
                }

                //register all new users as clients
                //user.UserType = UserType.Client;
                this.unitOfWork.userRepository.Add(user);
                user.AccessToken = SessionGenerator.GenerateSessionKey(user.Id);
                this.unitOfWork.userRepository.Update(user.Id, user);

                return(this.Request.CreateResponse(HttpStatusCode.Created, UserRegisterResponseModel.FromEntity(user)));
            });

            return(responseMessage);
        }
        /// <summary>
        ///  API to handle the silent Auth using refresh token
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns>UserResponse</returns>
        public UserResponse SilentAuth(UserRequest userRequest)
        {
            SessionGenerator sessionGenerator = new SessionGenerator();
            UserResponse     userResponse     = new UserResponse();

            try
            {
                if (string.IsNullOrEmpty(userRequest.CognitoClientId))
                {
                    throw new ArgumentException("CognitoClientId");
                }

                if (string.IsNullOrEmpty(userRequest.Payload.RefreshToken))
                {
                    throw new ArgumentException("RefreshToken");
                }

                if (string.IsNullOrEmpty(userRequest.ClientSecret))
                {
                    throw new ArgumentException("ClientSecret");
                }

                if (string.IsNullOrEmpty(userRequest.UserName))
                {
                    throw new ArgumentException("UserName");
                }


                //Generate Id token from the refresh token
                AuthFlowResponse authResponse = sessionGenerator.ProcessRefreshToken(userRequest);

                //Check if user having any challenges
                if (authResponse != null && authResponse.AuthenticationResult == null)
                {
                    string message = sessionGenerator.CheckChallenge(authResponse.ChallengeName);
                    userResponse.Error = ResponseBuilder.UnAuthorized(message);
                    return(userResponse);
                }
                //Create user response for valid token
                else if (authResponse != null && authResponse.AuthenticationResult != null)
                {
                    userResponse = new UserResponse
                    {
                        AccessToken   = authResponse.AuthenticationResult.AccessToken,
                        IdentityToken = authResponse.AuthenticationResult.IdToken
                    };
                    return(userResponse);
                }
                //Send bad request if user name is invalid
                else
                {
                    userResponse.Error = ResponseBuilder.BadRequest(DataResource.USERNAME);
                    return(userResponse);
                }
            }
            catch (Exception silentAuthException)
            {
                LambdaLogger.Log(silentAuthException.ToString());
                userResponse.Error = new ExceptionHandler(silentAuthException).ExceptionResponse();
                return(userResponse);
            }
        }