Welcome Guest, Not a member yet? Register   Sign In
crsf and ion_auth reset password
#1

I am having a challenge with the csrf token for resetting a password in ion_auth.  I have read other posts here and on other sites about ion_auth & csrf.

In a nutshell, it seems the csrf data stored in flashdata under $_SESSION does not survive from the form created and when the form is submitted.

PHP Code:
    public function reset_password($code NULL)
    {
        if (!
$code)
        {
            
show_404();
        }

        
$user $this->ion_auth->forgotten_password_check($code);

        if (
$user)
        {
            
// if the code is valid then display the password reset form

            
$this->form_validation->set_rules('new'$this->lang->line('reset_password_validation_new_password_label'), 'required|min_length[' $this->config->item('min_password_length''ion_auth') . ']|max_length[' $this->config->item('max_password_length''ion_auth') . ']|matches[new_confirm]');
            
$this->form_validation->set_rules('new_confirm'$this->lang->line('reset_password_validation_new_password_confirm_label'), 'required');

            if (
$this->form_validation->run() === FALSE)
            {
                
// display the form

                // set the flash data error message if there is one
                
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

                
$this->data['min_password_length'] = $this->config->item('min_password_length''ion_auth');
                
$this->data['new_password'] = array(
                    
'name' => 'new',
                    
'id' => 'new',
                    
'type' => 'password',
                    
'pattern' => '^.{' $this->data['min_password_length'] . '}.*$',
                );
                
$this->data['new_password_confirm'] = array(
                    
'name' => 'new_confirm',
                    
'id' => 'new_confirm',
                    
'type' => 'password',
                    
'pattern' => '^.{' $this->data['min_password_length'] . '}.*$',
                );
                
$this->data['user_id'] = array(
                    
'name' => 'user_id',
                    
'id' => 'user_id',
                    
'type' => 'hidden',
                    
'value' => $user->id,
                );
                
$this->data['csrf'] = $this->_get_csrf_nonce();
                
$this->data['code'] = $code;

                
$this->data['site_title'] = 'Reset Password — '.$this->config->item('site_title');

                
// render
                
$this->_render_page('reset_password_form'$this->data);
            }
            else
            {
                
// do we have a valid request?
                
if ($this->_valid_csrf_nonce() === FALSE || $user->id != $this->input->post('user_id'))
                {

                    
// something fishy might be up
                    
$this->ion_auth->clear_forgotten_password_code($code);

                    
show_error($this->lang->line('error_csrf'));

                }
                else
                {
                    
// finally change the password
                    
$identity $user->{$this->config->item('identity''ion_auth')};

                    
$change $this->ion_auth->reset_password($identity$this->input->post('new'));

                    if (
$change)
                    {
                        
// if the password was successfully changed
                        
$this->session->set_flashdata('message'$this->ion_auth->messages());
                        
redirect("auth/login"'refresh');
                    }
                    else
                    {
                        
$this->session->set_flashdata('message'$this->ion_auth->errors());
                        
redirect('auth/reset_password/' $code'refresh');
                    }
                }
            }
        }
        else
        {
            
// if the code is invalid then send them back to the forgot password page
            
$this->session->set_flashdata('message'$this->ion_auth->errors());
            
redirect("auth/forgot_password"'refresh');
        }
    }

    public function 
_get_csrf_nonce()
    {
        
$this->load->helper('string');
        
$key random_string('alnum'8);
        
$value random_string('alnum'20);
        
$this->session->set_flashdata('csrfkey'$key);
        
$this->session->set_flashdata('csrfvalue'$value);

        return array(
$key => $value);
    }

    public function 
_valid_csrf_nonce()
    {
        
$csrfkey $this->input->post($this->session->flashdata('csrfkey'));
        if (
$csrfkey && $csrfkey === $this->session->flashdata('csrfvalue'))
        {
            return 
TRUE;
        }
        else
        {
            return 
FALSE;
        }
    } 
Stepping through the code with Xdebug, the flashdata of the csrf key and its value is not persisting in $_SESSION between the instant it is set at the time the form is rendered and when the form is submitted.  The flashdata is set by 
Code:
$this->data['csrf'] = $this->_get_csrf_nonce();
when "$this->form_validation->run() === FALSE".  

The steps involved to check the csrf token occur after "[font=Monaco, Consolas, Courier, monospace]// [font=Monaco, Consolas, Courier, monospace]do we have a valid request?[/font]".  The function "_valid_csrf_nonce()" returns FALSE for the 'csrfkey' and [font=Monaco, Consolas, Courier, monospace]'csrfvalue' are no longer present. [/font][/font]

I have tried setting cookies by a file or sessions managed by a database, as recommended on Ben Edmunds' github page.

Has anyone else encountered such a problem and can recommend a solution?  Thanks for taking the time to read this.
Reply


Messages In This Thread
crsf and ion_auth reset password - by dwlamb - 02-07-2018, 01:29 PM
RE: crsf and ion_auth reset password - by dwlamb - 02-07-2018, 10:43 PM
RE: crsf and ion_auth reset password - by dwlamb - 02-08-2018, 08:10 AM
RE: crsf and ion_auth reset password - by dwlamb - 02-08-2018, 10:27 AM
RE: crsf and ion_auth reset password - by dwlamb - 02-12-2018, 04:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB