<commit_before><?php

/**
 * Handles Multi-Factor Authentication when authTypeMode is 'DEVICE'
 *
 * @package     Nails
 * @subpackage  module-auth
 * @category    Controller
 * @author      Nails Dev Team
 * @link
 */

use Nails\Factory;
use Nails\Auth\Controller\BaseMfa;

class Mfa_device extends BaseMfa
{
    public function _remap()
    {
        if ($this->authMfaMode == 'DEVICE') {

            $this->index();

        } else {

            show_404();
        }
    }

    // --------------------------------------------------------------------------

    public function index()
    {
        //  Validates the request token and generates a new one for the next request
        $this->validateToken();

        // --------------------------------------------------------------------------

        //  Has this user already set up an MFA?
        $mfaDevice = $this->auth_model->mfaDeviceSecretGet($this->mfaUser->id);

        if ($mfaDevice) {

            $this->requestCode();

        } else {

            $this->setupDevice();
        }
    }

    // --------------------------------------------------------------------------

    protected function setupDevice()
    {
        if ($this->input->post()) {

            $oSession        = Factory::service('Session', 'nailsapp/module-auth');
            $oFormValidation = Factory::service('FormValidation');

            $oFormValidation->set_rules('mfaSecret', '', 'xss_clean|required');
            $oFormValidation->set_rules('mfaCode1', '', 'xss_clean|required');
            $oFormValidation->set_rules('mfaCode2', '', 'xss_clean|required');

            $oFormValidation->set_message('required', lang('fv_required'));

            if ($oFormValidation->run()) {

                $secret = $this->input->post('mfaSecret');
                $code1  = $this->input->post('mfaCode1');
                $code2  = $this->input->post('mfaCode2');

                //  Verify the inout
                if ($this->auth_model->mfaDeviceSecretValidate($this->mfaUser->id, $secret, $code1, $code2)) {

                    //  Codes have been validated and saved to the DB, sign the user in and move on
                    $status   = 'success';
                    $message  = '<strong>Multi Factor Authentication Enabled!</strong><br />You successfully';
                    $message .= 'associated an MFA device with your account. You will be required ot use it ';
                    $message .= 'the next time you log in.';

                    $oSession->set_flashdata($status, $message);

                    $this->loginUser();

                } else {
                    $this->data['error'] = '<strong>Sorry,</strong> those codes failed to validate. Please try again.';
                }

            } else {
                $this->data['error'] = lang('fv_there_were_errors');
            }
        }

        //  Generate the secret
        $this->data['secret'] = $this->auth_model->mfaDeviceSecretGenerate(
            $this->mfaUser->id,
            $this->input->post('mfaSecret')
        );

        if (!$this->data['secret']) {

            $status   = 'error';
            $message  = '<Strong>Sorry,</strong> it has not been possible to get an MFA device set up for this user. ';
            $message .= $this->auth_model->lastError();

            $oSession->set_flashdata($status, $message);

            if ($this->returnTo) {
                redirect('auth/login?return_to=' . $this->returnTo);
            } else {
                redirect('auth/login');
            }
        }

        //  Render the page
        $this->data['page']->title = 'Set up a new MFA device';<commit_msg>Removed xss_filter from setupDevice method<commit_after><?php

/**
 * Handles Multi-Factor Authentication when authTypeMode is 'DEVICE'
 *
 * @package     Nails
 * @subpackage  module-auth
 * @category    Controller
 * @author      Nails Dev Team
 * @link
 */

use Nails\Factory;
use Nails\Auth\Controller\BaseMfa;

class Mfa_device extends BaseMfa
{
    public function _remap()
    {
        if ($this->authMfaMode == 'DEVICE') {

            $this->index();

        } else {

            show_404();
        }
    }

    // --------------------------------------------------------------------------

    public function index()
    {
        //  Validates the request token and generates a new one for the next request
        $this->validateToken();

        // --------------------------------------------------------------------------

        //  Has this user already set up an MFA?
        $mfaDevice = $this->auth_model->mfaDeviceSecretGet($this->mfaUser->id);

        if ($mfaDevice) {

            $this->requestCode();

        } else {

            $this->setupDevice();
        }
    }

    // --------------------------------------------------------------------------

    protected function setupDevice()
    {
        if ($this->input->post()) {

            $oSession        = Factory::service('Session', 'nailsapp/module-auth');
            $oFormValidation = Factory::service('FormValidation');

            $oFormValidation->set_rules('mfaSecret', '', 'required');
            $oFormValidation->set_rules('mfaCode1', '', 'required');
            $oFormValidation->set_rules('mfaCode2', '', 'required');

            $oFormValidation->set_message('required', lang('fv_required'));

            if ($oFormValidation->run()) {

                $secret = $this->input->post('mfaSecret', true);
                $code1  = $this->input->post('mfaCode1', true);
                $code2  = $this->input->post('mfaCode2', true);

                //  Verify the inout
                if ($this->auth_model->mfaDeviceSecretValidate($this->mfaUser->id, $secret, $code1, $code2)) {

                    //  Codes have been validated and saved to the DB, sign the user in and move on
                    $status   = 'success';
                    $message  = '<strong>Multi Factor Authentication Enabled!</strong><br />You successfully';
                    $message .= 'associated an MFA device with your account. You will be required ot use it ';
                    $message .= 'the next time you log in.';

                    $oSession->set_flashdata($status, $message);

                    $this->loginUser();

                } else {
                    $this->data['error'] = '<strong>Sorry,</strong> those codes failed to validate. Please try again.';
                }

            } else {
                $this->data['error'] = lang('fv_there_were_errors');
            }
        }

        //  Generate the secret
        $this->data['secret'] = $this->auth_model->mfaDeviceSecretGenerate(
            $this->mfaUser->id,
            $this->input->post('mfaSecret', true)
        );

        if (!$this->data['secret']) {

            $status   = 'error';
            $message  = '<Strong>Sorry,</strong> it has not been possible to get an MFA device set up for this user. ';
            $message .= $this->auth_model->lastError();

            $oSession->set_flashdata($status, $message);

            if ($this->returnTo) {
                redirect('auth/login?return_to=' . $this->returnTo);
            } else {
                redirect('auth/login');
            }
        }

        //  Render the page
        $this->data['page']->title = 'Set up a new MFA device';
{'fraction_non_alphanumeric': 0.19315868060268765, 'fraction_numerical': 0.002986290213112529, 'mean_word_length': 1.518974358974359, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5262759', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1886, 'n_words': 435}