How to implement multiple authentication methods for Portal login?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lj4353
    Senior Member
    • Nov 2023
    • 152

    #1

    How to implement multiple authentication methods for Portal login?

    Hi, I would like to implement multiple authentication methods for EspoCRM Portal login — for example:
    • Normal username + password login
    • External OIDC token-based login (e.g., use a predefined password like oidc-secret to simulate token login)

    To achieve this, I created a custom authentication class DualLoginAuth and set it in data/config.php like this:
    'portalAuthenticationProvider' => '\\Espo\\Custom\\Authentication\\DualLoginAuth',


    File path:custom/Espo/Custom/Authentication/DualLoginAuth.php​

    namespace Espo\Custom\Authentication;

    use Espo\Core\Authentication\AuthenticationInterface;
    use Espo\Core\Authentication\Exception\AuthenticationE xception;
    use Espo\ORM\EntityManager;
    use Espo\Core\Utils\PasswordHasher;
    use Espo\Core\Utils\Log;

    class DualLoginAuth implements AuthenticationInterface
    {
    public function __construct(
    protected EntityManager $entityManager,
    protected PasswordHasher $passwordHasher,
    protected Log $log
    ) {}

    public function authenticate(array $credentials)
    {
    $this->log->info('[DualLoginAuth] Called'); // for debugging

    $userName = $credentials['userName'] ?? null;
    $password = $credentials['password'] ?? null;

    if ($password === 'oidc-secret') {
    $user = $this->entityManager->getRepository('PortalUser')->where(['userName' => $userName])->findOne();
    if ($user) return $user;
    throw new AuthenticationException('OIDC login failed.');
    }

    // fallback to normal login
    $user = $this->entityManager->getRepository('PortalUser')->where(['userName' => $userName])->findOne();
    if (!$user || !$this->passwordHasher->verify($password, $user->get('password'))) {
    throw new AuthenticationException('Invalid credentials.');
    }

    return $user;
    }
    }

    I also did:
    • Cleared cache and rebuilt EspoCRM
    • Enabled logging ($this->log->info(...))
    • Verified the class loads properly
    • Made sure portal user exists

    But my authenticate() method is never called when logging into the portal UI. No logs are triggered. It seems the custom provider is not even being used.

    Is portalAuthenticationProvider still supported in v9.1.5?
    Or is there a different method to support multiple login methods for portal users?

    Thanks for your help!
  • victor
    Active Community Member
    • Aug 2022
    • 920

    #2
    A very similar topic has already been discussed on the forum, it may be useful: https://forum.espocrm.com/forum/gene...047#post113047.

    It is better to create similar topics in the developers section.
    Last edited by victor; Today, 11:43 AM.

    Comment

    Working...