benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.34k stars 1.14k forks source link

If requests coming from PHP CLI #1510

Closed Slamoth closed 3 years ago

Slamoth commented 3 years ago

I am using IonAuth for CI4. I came across a problem where I needed to access the controlllers from PHP CLI. When using PHP CLI Ion-Auth needs to be logged in too. There is a functon caller is_cli() integrated into CI4 already which gives the output as TRUE of FALSE. https://codeigniter4.github.io/userguide/general/common_functions.html#is_cli The output of var_dump(is_cli()); is bool(false)

What I needed was an option when requests coming from CLI needed to be passed without requiring username and password since everytime I call a script I have to use CURL to login and do whats needed and use CURL again to logout. Since this requires username and password in plain text it is not really what I want.

So my feature request is this: Can you put an option in config file and allow requests coming from PHP CLI to pass thorugh without authentication ?

Slamoth commented 3 years ago

We kinda figured it out.... No need to modify ion_auth code... (but if you prefer you can get the Events.php part !) Add these pieces on CI 4 config files.

Events.php file app/config

if ((!$ion_auth->loggedIn() && $request->uri->getPath() !== "auth/login" && $request->uri->getPath() !== "") {
    header('Location: /auth/login');
    exit();
  }

Constants.php file in app/config (before )

if ( ! isset($_SERVER['HTTP_HOST'])) {
    $_SERVER['HTTP_HOST'] = shell_exec("hostname|tr -d '\n'");
}

$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'];
defined('BASE') || define('BASE', $protocol);

after those changes follow the path on https://codeigniter4.github.io/userguide/cli/cli.html.
Hope this helps to someone for CodeIgniter 4 CLI error using ion_auth

contributers : @kubilaycagri