2017-04-05 4 views
0

My Silex Appでjasig/phpCas認証を実装しました。 これはほぼ完了ですが、私はauthfailure Response correcltyを処理することはできません。CAS SSO with Silexガード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']); 
      }, 
    ), 
); 

MyTokenAuthenticatorクラス:SSOの有効なユーザーがアプリで拒否された場合

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); 

} 

問題があります。これは、レンダリングなしで、 jsonメッセージのあるページを表示します。 私の回避策は、レスポンスとしてssoログアウトリンクを指定した最小限のhtmlページとsession_destroy()を使用することですが、迅速かつ厄介な修正です。

いいえ、エラーメッセージが表示されています。おそらく他のクラスを拡張するのでしょうか? Silexのドキュメンテーションは役に立たなかった。ありがとうございました !

+0

HTMLレンダリングエラーが発生した場合は、なぜ '' 'JsonResponse'''を返すのですか?私はここに何かを逃していますかHTMLレスポンスがほしいのであれば、あなたのクラスにtwigを注入してから '' '新しいレスポンスを返すことができます($ this-> twig-> render( 'error-template.twig'、[" data "=> $ data ])、Response :: HTTP_FORBIDDEN); '' ' – mTorres

+0

これは[documentation example](http://silex.sensiolabs.org/doc/2.0/cookbook/guard_authentication.html)のダムコピー/ペーストでした。 'onAuthenticationFailure'は何らかの理由で応答が必要です(事前設定されたフォーム?)。 レスポンスオブジェクトとしてレンダリングするのは良い方法です。 私はシレックスを新しくしており、すべての可能性を知りません。私は試してみます。 – raphr

答えて

0

私は開発者の他の人たちのことだったので、この質問に戻ってください。 @mTorresの解決策が働いています。私はtwigがサービスレジストリでこの時点で設定されていないので、コンストラクタ経由でアプリケーションオブジェクト全体を格納しなければなりませんでした。

class MyTokenAuthenticator extends AbstractGuardAuthenticator 
{ 
    private $app; 

    public function __construct($app) 
    { 
     $this->app=$app; 
    } 

、カスタムイベント

public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
{ 
    return new \Symfony\Component\HttpFoundation\Response(
      $this->app['twig']->render('logout.html.twig',array(
       'error'   => $data, 
      )); 
} 

多くの感謝!