J'ai implémenté l'authentification jasig/phpCas dans My Silex App. C'est presque terminé, mais je ne peux pas gérer la réponse d'authfailure.CAS SSO avec gestionnaire Silex guard authfailure
$app['app.token_authenticator'] = function ($app) {
return new MyApp\Domain\MyTokenAuthenticator($app['security.encoder_factory'],$app['cas'],$app['dao.usersso']);
};
$app['security.firewalls'] = array(
'default' => array(
'pattern' => '^/.*$',
'anonymous' => true,
'guard' => array(
'authenticators' => array(
'app.token_authenticator'
),
),
'logout' => array ('logout_path' => '/logout', 'target_url' => '/goodbye'),
'form' => array('login_path' =>'/login', 'check_path' =>'/admin/login_check', 'authenticator' => 'time_authenticator'),
'users' => function() use ($app) {
return new MyApp\DAO\UserDAO($app['db']);
},
),
);
classe MyTokenAuthenticator:
class MyTokenAuthenticator extends AbstractGuardAuthenticator
{
private $encoderFactory;
private $cas_settings;
private $sso_dao;
public function __construct(EncoderFactoryInterface $encoderFactory, $cas_settings, MyApp\DAO\UserSsoDAO $userdao)
{
$this->encoderFactory = $encoderFactory;
$this->cas_settings = $cas_settings;
$this->sso_dao = $userdao;
}
public function getCredentials(Request $request)
{
$bSSO = false;
//Test request for sso
if (strpos($request->get("ticket"),"cas-intra") !==false)
$bSSO = true;
if($request->get("sso") == "1")
$bSSO=true;
if ($bSSO)
{
if ($this->cas_settings['debug'])
{
\CAS_phpCAS::setDebug();
\CAS_phpCAS::setVerbose(true);
}
\CAS_phpCAS::client(CAS_VERSION_2_0,
$this->cas_settings['server'],
$this->cas_settings['port'],
$this->cas_settings['context'],
false);
\CAS_phpCAS::setCasServerCACert('../app/config/cas.pem');
// force CAS authentication
\CAS_phpCAS::forceAuthentication();
$username = \CAS_phpCAS::getUser();
return array (
'username' => $username,
'secret' => 'SSO'
);
}
//Nothing to do, skip custom auth
return;
}
/**
* Get User from the SSO database.
* Add it into the MyApp users database (Update if already exists)
* {@inheritDoc}
* @see \Symfony\Component\Security\Guard\GuardAuthenticatorInterface::getUser()
*/
public function getUser($credentials, UserProviderInterface $userProvider)
{
//Get user stuf
....
//return $userProvider->loadUserByUsername($credentials['username']);
return $user;
}
/**
*
* {@inheritDoc}
* @see \Symfony\Component\Security\Guard\GuardAuthenticatorInterface::checkCredentials()
*/
public function checkCredentials($credentials, UserInterface $user)
{
// check credentials - e.g. make sure the password is valid
// return true to cause authentication success
if ($this->sso_dao->isBAllowed($user->getLogin()))
return true;
else
throw new CustomUserMessageAuthenticationException("Sorry, you're not alllowed tu use this app.");
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// on success, let the request continue
return;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$data = array(
'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
// or to translate this message
// $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
);
return new JsonResponse($data,403);
}
problème est lorsqu'un utilisateur valide du SSO est refusée en application. Il affiche une page avec un message json, sans aucun rendu. Ma solution consiste à utiliser une page html minimale avec le lien sso logout comme réponse et session_destroy()
, mais son correctif rapide et sale.
Je voudrais une redenring via twig avec un message d'erreur sympa. Peut-être une autre classe à étendre? La documentation de Silex n'était d'aucune aide. Je vous remercie !
Si vous voulez une erreur de rendu HTML, pourquoi renvoyez-vous un '' '' JsonResponse'''? Est-ce que j'ai râté quelque chose? Si vous voulez juste une réponse HTML, vous pouvez essayer d'injecter twig sur votre classe, puis '' 'retourner une nouvelle réponse ($ this-> twig-> render ('error-template.twig', [" data "=> $ data ]), Response :: HTTP_FORBIDDEN); '' ' – mTorres
C'était un copier/coller bête de [l'exemple de documentation] (http://silex.sensiolabs.org/doc/2.0/cookbook/guard_authentication.html). Comme 'onAuthenticationFailure' a besoin d'une réponse pour une bonne raison (forme préconfigurée?). Votre rendu en tant qu'objet de réponse semble un bon moyen de le faire. Je suis nouveau à Silex et je ne connais pas toutes les possibilités. Je vais essayer. – raphr