Ejemplo n.º 1
0
        public CreateUserResponse ResetPassword([FromBody] ResetPasswordRequest pwResetRequest)
        {
            CreateUserResponse response = new CreateUserResponse();

            if (string.IsNullOrEmpty(pwResetRequest.User.Email) ||
                string.IsNullOrEmpty(pwResetRequest.User.Password) ||
                string.IsNullOrEmpty(pwResetRequest.Token))
            {
                response.Error = "Missing necessary data";
            }
            User user = pwResetRequest.User;

            using (RequestTracker request = new RequestTracker(Constant.RequestAPI.ResetPW.ToString(), user.Email))
            {
                try
                {
                    user.Init();
                    if (string.IsNullOrEmpty(user.Password) || user.Password.Length < 8)
                    {
                        request.response = RequestTracker.RequestResponse.UserError;
                        response.Error   = "Password must be at least 8 characters";
                        return(response);
                    }
                    User retrievedUser;
                    TableStore.Get <User>(TableStore.TableName.users, user.PartitionKey, user.Email, out retrievedUser);
                    if (retrievedUser == null)
                    {
                        request.response = RequestTracker.RequestResponse.UserError;
                        response.Error   = "Invalid user";
                        return(response);
                    }
                    // Confirm token
                    if (retrievedUser.PWResetTokenWithExpiry == null ||
                        retrievedUser.PWResetTokenWithExpiry.Item1 != pwResetRequest.Token ||
                        DateTime.Compare(DateTime.UtcNow, retrievedUser.PWResetTokenWithExpiry.Item2) > 0)
                    {
                        request.response = RequestTracker.RequestResponse.UserError;
                        response.Error   = "Invalid or expired token";
                        return(response);
                    }
                    retrievedUser.Password = user.Password;
                    retrievedUser.EncryptPassword();
                    retrievedUser.PWResetTokenWithExpiry = null;
                    retrievedUser.ClearAuthTokens();
                    string token = retrievedUser.GetAuthToken();
                    TableStore.Update(TableStore.TableName.users, retrievedUser);
                    response.Token = token;
                    response.Error = "Success";
                    return(response);
                }
                catch (Exception e)
                {
                    request.response = RequestTracker.RequestResponse.ServerError;
                    ExceptionTracker.LogException(e);
                    response.Error = "Oops, something went wrong. Initiating developer punishment.";
                    return(response);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the last updated time, null if there has never been an update
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DateTime?GetLastUpdate(string userId)
        {
            LastUpdate lastUpdate = null;

            if (!TableStore.Get <LastUpdate>(TableStore.TableName.lastUpdate, userId, userId, out lastUpdate))
            {
                return(null);
            }
            return(lastUpdate.LastUpdateTime);
        }