2017-03-10 23 views
1

SymfonyファイアウォールのLDAP認証を取得しようとしていますが、問題が発生しています。主な問題は、Symfony LdapUserProviderに由来しているようです。ldap_bind()を試してみると、ユーザーが入力したユーザー名とパスワードは使用されません。Symfony LDAP auth bindユーザ名とパスワード

だから、私は私のファイアウォールの設定としてこれを持っている:

$app->register(new SilexProvider\SecurityServiceProvider(), [ 
    'security.firewalls' => [ 
     'secured' => [ 
      'pattern' => '^.*$', 
      'http' => true, 
      'users' => new \Symfony\Component\Security\Core\User\LdapUserProvider(
       \Symfony\Component\Ldap\Ldap::create('ext_ldap', [ 
        'connection_string' => 'ldap://MY_LDAP_DOMAIN', 
       ]), 
       'dc=MY_DC_1,dc=MY_DC_2', 
       'uid={username},cn=users,cn=accounts,dc=MY_DC_1,dc=MY_DC_2' 
      ), 
     ], 
    ], 
]); 

しかしldap_bindメソッドが呼び出されたときに、私の{username}一部は、ユーザー提供されたユーザ名に置き換えられません。そのため、ldap_bindに渡されたdn文字列は、文字通りuid={username},cn=users,cn=accounts,dc=MY_DC_1,dc=MY_DC_2です。ユーザー名は置き換えられません。

このコードは予期していますが、LdapUserProvider->loadUserByUsername()bindを呼び出してから文字列の置換を行います。もう1つの問題は、ユーザーが入力したパスワードがずっと後にわかることがないため、bindコールに再度ユーザーが入力したパスワードがないことです。

dnとパスワードを適切に置き換えるように設定するにはどうすればよいですか?私はこれらの2つの基本的なライン($dataが有効なユーザーの配列です)を使用する場合:

$ldap = ldap_connect('MY_LDAP_DOMAIN'); 
$bind = ldap_bind($ldap, 'uid=' . $data['username'] . ',cn=users,cn=accounts,dc=MY_DC_1,dc=MY_DC_2', $data['password']); 

をそれからそれは完全にバインドします。この2行をSymfonyファイアウォールが理解できる状況に変換するにはどうすればよいですか?

答えて

3

あなたは、既存のコードで主な問題があります:あなたは内蔵使用している代わりに、オープンLDAPのuid={username}

  • sAMAccountName={username}

    1. デフォルトでsymfonyのLdapUserProviderコンポーネントは、Active Directory(Windowsの場合)スキーマを使用していますhttpセキュリティファイアウォールでは、デフォルトでDaoAuthenticationProvider認証プロバイダが使用されます。 LDAP認証の場合は、代わりにLdapBindAuthenticationProviderを使用する必要があります。

    最初の問題LdapUserProviderにユーザ識別子キーを渡すことで解決することができます:LdapBindAuthenticationProviderが最初に呼び出されます:彼らは使用されることはありませんので、

    $app['ldap.users'] = function() use ($app) { 
        return new LdapUserProvider(
         // your LDAP adapter 
         $app['ldap'], 
         // base DN 
         'dc=example,dc=com', 
         // you don't need search DN 
         null, 
         // you don't need search password 
         null, 
         // list of default roles, can be empty array 
         ['ROLE_USER'], 
         // user identifier key for LDAP 
         // this identitfer must be set explicitly 
         'uid' 
        ); 
    }; 
    

    お知らせの3番目と4番目のパラメータは、nullすることができ、LDAP接続は既にバインドされています。

    第2の問題は、少しのコーディングが必要です。 Symfonyには、お客様の要件に完全に適合するhttp_basic_ldap認証プロバイダが組み込まれています。残念ながら、Silexには1つもないので、あなた自身でそれを行う必要があります。参考のためにSilexのドキュメントを使用してください。Defining a custom Authentication Provider

    これは、私の例であるform_login_ldapのSilex実装です。 登録するすべてのLDAP関連サービス:LDAPフォームのためのセキュリティ認証リスナー工場用

    use Pimple\Container; 
    use Pimple\ServiceProviderInterface; 
    use Symfony\Component\Security\Core\User\LdapUserProvider; 
    
    class LdapUsersServiceProvider implements ServiceProviderInterface 
    { 
        public function register(Container $app) 
        { 
         $app['ldap.users'] = function() use ($app) { 
          return new LdapUserProvider(
           $app['ldap'], 
           'dc=example,dc=com', 
           null, 
           null, 
           ['ROLE_USER'], 
           'uid' 
          ); 
         }; 
        } 
    } 
    

    サービスプロバイダLDAPユーザのLDAPアダプタの

    $app // register other services 
        ->register(new LdapServiceProvider()) 
        ->register(new LdapUsersServiceProvider()) 
        ->register(new LdapSecurityServiceProvider()) 
        ->register(new \Silex\Provider\SecurityServiceProvider(), [ 
         'security.firewalls' => [ 
          'login' => [ 
           'pattern' => '^/login$', 
          ], 
          'secured' => [ 
           'pattern' => '^.*$', 
           'form_login_ldap' => [ 
            'login_path' => 'login', 
            'check_path' => 'login_check', 
            'default_target_path' => 'backoffice', 
           ], 
           'users' => $this['ldap.users'], 
          ], 
         ], 
        ]) 
    ; 
    

    サービスプロバイダ

    use Pimple\Container; 
    use Pimple\ServiceProviderInterface; 
    use Symfony\Component\Ldap\Ldap; 
    
    class LdapServiceProvider implements ServiceProviderInterface 
    { 
        public function register(Container $app) 
        { 
         $app['ldap'] = function() { 
          return Ldap::create('ext_ldap', [ 
           'connection_string' => 'ldap.example.com', 
          ]); 
         }; 
        } 
    } 
    

    サービスプロバイダ(あなたのための最も興味深い部分)

    use Pimple\Container; 
    use Pimple\ServiceProviderInterface; 
    use Symfony\Component\Security\Core\Authentication\Provider\LdapBindAuthenticationProvider; 
    
    class LdapSecurityServiceProvider implements ServiceProviderInterface 
    { 
        public function register(Container $app) 
        { 
         $app['security.authentication_listener.factory.form_login_ldap'] = $app->protect(function ($name, $options) use ($app) { 
          // define the authentication provider object 
          $app['security.authentication_provider.'.$name.'.form_login_ldap'] = function() use ($app, $name) { 
           return new LdapBindAuthenticationProvider(
            $app['security.user_provider.'.$name], 
            $app['security.user_checker'], 
            $name, 
            $app['ldap'], 
            'uid={username},dc=example,dc=com', 
            $app['security.hide_user_not_found'] 
           ); 
          }; 
    
          // define the authentication listener object 
          $app['security.authentication_listener.'.$name.'.form_login_ldap'] = $app['security.authentication_listener.form._proto']($name, $options); 
    
          // define the entry point object 
          $app[$entryPoint = 'security.entry_point.'.$name.'.form_login_ldap'] = $app['security.entry_point.form._proto']($name, array()); 
    
          return array(
           // the authentication provider id 
           'security.authentication_provider.'.$name.'.form_login_ldap', 
           // the authentication listener id 
           'security.authentication_listener.'.$name.'.form_login_ldap', 
           // the entry point id 
           $entryPoint, 
           // the position of the listener in the stack 
           'form' 
          ); 
         }); 
        } 
    } 
    
  • 関連する問題