Lonare
Lonare

Reputation: 4703

auth_override_class_method_http not working with HMVC

Hi I have a config setting like below:

$config['auth_override_class_method_http']['admin']['auth']['post'] = 'none';

I am using this library for my HMVC: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

But when I checked with error handling on REST_Controller.php

it seems like /admin/auth is not recognised with REST

here is my REST code:

<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */
//To Solve File REST_Controller not found
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php'; 

class Auth extends REST_Controller {

    function __construct()
    {
        // Construct the parent class
        parent::__construct(); 

        $this->load->model('Auth_model', 'Entity_model');
        //$this->load->library('form_validation');
        $this->load->library('validation');

        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");



    }


    public function index_get()
    { 


      $return_message = [
                         'error' => 102,   
                         'message' => "Entity Not Found"
                        ];
      $this->set_response($return_message, REST_Controller::HTTP_NOT_FOUND);  

    }


    public function index_post()
    {
        /* Request, Response https://i.vgy.me/APy9PT.png */


            $message = [
                'status' => true,
                'error_code' => 102,  
                'error_message' => 'success'
            ];

            $this->set_response($message, REST_Controller::HTTP_CREATED); 
            // CREATED (201) being the HTTP response code

    }



}

Although all of my GET/POST requests are working fine as it should be on the endpoint.

Could anyone suggest what I should do in order to get it working i.e.,

I want to use authentication on several API request but not on admin/auth [POST]

Upvotes: 0

Views: 81

Answers (1)

Lonare
Lonare

Reputation: 4703

After days of debugging I realised that because I was using HMVC the router class in REST_Controller was not getting proper index.

So I edited my REST_controller to work with HMVC

Line 1498 : Add

if ( ! empty($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method]))
            {
                // None auth override found, prepare nothing but send back a TRUE override flag
                if ($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method] === 'none')
                {
                    return TRUE;
                }

                // Basic auth override found, prepare basic
                if ($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method] === 'basic')
                {
                    $this->_prepare_basic_auth();

                    return TRUE;
                }

                // Digest auth override found, prepare digest
                if ($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method] === 'digest')
                {
                    $this->_prepare_digest_auth();

                    return TRUE;
                }

                // Session auth override found, check session
                if ($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method] === 'session')
                {
                    $this->_check_php_session();

                    return TRUE;
                }

                // Whitelist auth override found, check client's ip against config whitelist
                if ($auth_override_class_method_http[$this->router->module][$this->router->class][$this->request->method] === 'whitelist')
                {
                    $this->_check_whitelist_auth();

                    return TRUE;
                }
            }

Upvotes: 0

Related Questions