2 years ago

#24011

test-img

TechFanDan

The action login is not defined in UsersController

I'm attempting to create and implement my own authentication and it keeps trying to reach Users.login.

Since I specify which authenticate class to use, implement my own authenticate() method, why does it keep attempting to load it?

Full error:

Missing Method in UsersController
Cake\Controller\Exception\MissingActionException
Documentation API
The action login is not defined in UsersController
Error: Create UsersController::login() in file: src\Controller\UsersController.php.

AppController

public function initialize()
{
    ...
    $this->loadComponent('Auth');
    $this->Auth->config('authenticate', ['Sso']);
    ...
}

src/Auth/SsoAuthenticate.php

<?php
namespace App\Auth;

use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;
use Cake\Log\Log;

class SsoAuthenticate extends BaseAuthenticate
{
    public function authenticate(ServerRequest $request, Response $response)
    {
        Log::debug('SSO Authenticate()');
        //hard coded for testing
        return false;
    }
}
?>

I had been initially following the lead of the following post: CakePHP 3 Ldap authentication issue and clarification

Edit #1

Swapped to returning a valid user and I'm still getting the same results. With every change, I'm using a private window to ensure no session exists, etc.

public function authenticate(ServerRequest $request, Response $response)
{
    Log::debug('SSO Authenticate()');
    debug('SSO Authenticate()');

    $table = TableRegistry::get('Users');
    $u = $table->get(1);
    debug($u);
    return $u;
}

Edit #2

Having debug statements, error throwing or even exit will not stop this from going through. This must be a configuration issue? Caching?

public function authenticate(ServerRequest $request, Response $response)
{
    Log::debug('SSO Authenticate()');
    debug('SSO Authenticate()');
    throw new NotFoundException(__('Article not found'));
    exit;
}

Output of debug($this->Auth)

object(Cake\Controller\Component\AuthComponent) {

    'components' => [
        (int) 0 => 'RequestHandler',
        (int) 1 => 'Flash'
    ],
    'implementedEvents' => [
        'Controller.initialize' => 'authCheck',
        'Controller.startup' => 'startup'
    ],
    '_config' => [
        'authenticate' => [
            (int) 0 => 'Sso'
        ],
        'authorize' => null,
        'ajaxLogin' => null,
        'flash' => null,
        'loginAction' => null,
        'loginRedirect' => null,
        'logoutRedirect' => null,
        'authError' => null,
        'unauthorizedRedirect' => true,
        'storage' => 'Session',
        'checkAuthIn' => 'Controller.startup'
    ]

}

Edit #3

Seems this goes through at the moment, even though I'm throwing an error and existing in authenticate.

<?php
namespace App\Auth;

use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;
use Cake\Log\Log;

class SsoAuthenticate extends BaseAuthenticate
{

    public function authenticate(ServerRequest $request, Response $response)
    {
        Log::debug('SSO Authenticate()');
        debug('SSO Authenticate()');
        throw new NotFoundException(__('Article not found'));
        exit;
    }
    

    public function getUser(ServerRequest $request)
    {
        $table = TableRegistry::get('Users');
        return $table->get(1)->toArray();
    }
}
?>

Edit #4

Started from a blank app, implemented the following. Would it be acceptable to throw an error like that if my SSO headers aren't present?

src/Controller/AppController.php

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth');
    $this->Auth->config('authenticate', ['Sso']);
}

src/Auth/SsoAuthenticate.php

<?php
namespace App\Auth;

use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
use Cake\ORM\TableRegistry;
use Cake\Log\Log;
use Cake\Core\Exception\Exception;

class SsoAuthenticate extends BaseAuthenticate
{
    public function getUser(ServerRequest $request)
    {
        //return true;      //looks for Users.login
        //return false;     //looks for Users.login
        //return ['name' => 'Dan'];   //works!
        $noHeaders = false; //will add appropriate logic here

        if($noHeaders)  //hard stop!
            throw new Exception("No headers configured!");
        else    //insert or update user based on headers and what's in the current db
            return ['name' => "Myself"];    //
    }

    public function authenticate(ServerRequest $request, Response $response){}
}
?>

php

cakephp

cakephp-3.0

0 Answers

Your Answer

Accepted video resources