Browse Source

Authenticator: added getClientCertVerified()

Fabian Peter Hammerle 8 years ago
parent
commit
40c3e27c92
2 changed files with 34 additions and 2 deletions
  1. 12 2
      Authenticator.php
  2. 22 0
      tests/AuthenticatorTest.php

+ 12 - 2
Authenticator.php

@@ -4,6 +4,8 @@ namespace fphammerle\yii2\auth\clientcert;
 
 class Authenticator extends \yii\base\Component
 {
+    use \fphammerle\helpers\PropertyAccessTrait;
+
     public function init()
     {
         parent::init();
@@ -30,13 +32,21 @@ class Authenticator extends \yii\base\Component
         }
     }
 
+    /**
+     * @return bool
+     */
+    public function getClientCertVerified()
+    {
+        return isset($_SERVER['SSL_CLIENT_VERIFY'])
+            && $_SERVER['SSL_CLIENT_VERIFY'] == 'SUCCESS';
+    }
+
     /**
      * @return IdentityInterface|null
      */
     public function loginByClientCertficiate()
     {
-        if(isset($_SERVER['SSL_CLIENT_VERIFY'])
-            && $_SERVER['SSL_CLIENT_VERIFY'] == 'SUCCESS') {
+        if($this->getClientCertVerified()) {
             // Subject DN in client certificate
             return $this->loginByDistinguishedName($_SERVER["SSL_CLIENT_S_DN"]);
         } else {

+ 22 - 0
tests/AuthenticatorTest.php

@@ -50,6 +50,28 @@ class AuthenticatorTest extends TestCase
         $this->assertEquals($this->bob->id, $this->getIdentity()->id);
     }
 
+    /**
+     * @dataProvider getClientCertVerifiedProvider
+     */
+    public function testGetClientCertVerified($request_params, $client_cert_certified)
+    {
+        $a = new Authenticator;
+        $_SERVER = $request_params;
+        $this->assertEquals($client_cert_certified, $a->getClientCertVerified());
+        $this->assertEquals($client_cert_certified, $a->clientCertVerified);
+    }
+
+    public function getClientCertVerifiedProvider()
+    {
+        return [
+            [[], false],
+            [['SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], false],
+            [['SSL_CLIENT_VERIFY' => 'FAILED', 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], false],
+            [['SSL_CLIENT_VERIFY' => 'NONE', 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], false],
+            [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => null], true],
+        ];
+    }
+
     /**
      * @dataProvider loginByClientCertProvider
      */