/multi-authenticator

Multi authenticator for Nette Framework

Primary LanguagePHPMIT LicenseMIT

MultiAuthenticator

Build Status Licence Latest stable PHPStan

Installation

The recommended way to is via Composer:

composer require surda/multi-authenticator

After that you have to register extension in config.neon:

extensions:
    multiAuthenticator: Surda\MultiAuthenticator\DI\MultiAuthenticatorExtension

Minimal configuration

multiAuthenticator:
    authenticators:
        ldap: MyLdapAuthenticator
        default: MyDefaultAuthenticator
    rules:
        ldap:
            - '#^.+@ad.domain.com$#'
            - '#^sos\\.+$#'
            - ...

Default Authenticator

multiAuthenticator:
    authenticators:
        default: MyDefaultAuthenticator
        # or
    default: MyDefaultAuthenticator

List of all configuration options:

multiAuthenticator:
    authenticators: # Authenticators for resolve by rules
    rules:
    resolver: \Surda\MultiAuthenticator\AuthenticatorResolver::class
    authenticator: \Surda\MultiAuthenticator\MultiAuthenticator::class
    default: # Default authenticator 

Using via MultiAuthenticator (default)

config.neon

security.authenticator: @Surda\MultiAuthenticator\MultiAuthenticator
try {
    $this->user->login($values->username, $values->password);
} catch (\Nette\Security\AuthenticationException $e) {
    // ...
}

Using via resolver

use Surda\MultiAuthenticator\Resolver\AuthenticatorResolver;
use Surda\MultiAuthenticator\Exception\AuthenticatorNotFoundException;

/** @var AuthenticatorResolver */
private $resolver;

/**
 * @param AuthenticatorResolver $resolver
 */
public function __construct(AuthenticatorResolver $resolver)
{
    $this->resolver = $resolver;
}

try {
    $this->user->setAuthenticator($this->resolver->resolveByUsername('username@ad.domain.com'));
    // or
    $this->user->setAuthenticator($this->resolver->resolveByType('ldap'));
    
    try {
        $this->user->login($values->username, $values->password);
    } catch (\Nette\Security\AuthenticationException $e) {
        // ...
    }
}
catch (AuthenticatorNotFoundException $e) {
    // ...
}