AuthenticatorTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace fphammerle\yii2\auth\clientcert\tests\integration;
  3. use \fphammerle\yii2\auth\clientcert\Authenticator;
  4. use \fphammerle\yii2\auth\clientcert\tests\TestCase;
  5. class AuthenticatorTest extends TestCase
  6. {
  7. protected $alice;
  8. protected $bob;
  9. protected function setUp()
  10. {
  11. $this->mockApplication();
  12. $this->createSubjectTable();
  13. $this->alice = $this->createUser('alice');
  14. $this->bob = $this->createUser('bob');
  15. $this->createSubject($this->alice, 'CN=Alice,C=AT');
  16. $this->createSubject($this->alice, 'CN=Alice,O=Office,C=AT');
  17. $this->createSubject($this->bob, 'CN=Bob,C=AT');
  18. }
  19. /**
  20. * @dataProvider loginByClientCertProvider
  21. */
  22. public function testLoginByClientCert($request_params, $username)
  23. {
  24. $_SERVER = array_replace_recursive($_SERVER, $request_params);
  25. $app = $this->mockApplication([
  26. 'bootstrap' => ['clientCertAuth'],
  27. 'components' => [
  28. 'db' => \Yii::$app->db,
  29. 'clientCertAuth' => Authenticator::className(),
  30. ],
  31. ]);
  32. if($username) {
  33. $this->assertEquals($username, $this->getIdentity()->username);
  34. } else {
  35. $this->assertNull($this->getIdentity());
  36. }
  37. }
  38. public function loginByClientCertProvider()
  39. {
  40. return [
  41. [['SSL_CLIENT_VERIFY' => null, 'SSL_CLIENT_S_DN' => null], null],
  42. [['SSL_CLIENT_VERIFY' => null, 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], null],
  43. [['SSL_CLIENT_VERIFY' => 'FAILED', 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], null],
  44. [['SSL_CLIENT_VERIFY' => 'NONE', 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], null],
  45. [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => null], null],
  46. [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => ''], null],
  47. [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => 'CN=Alice,C=AT'], 'alice'],
  48. [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => 'CN=Alice,O=Office,C=AT'], 'alice'],
  49. [['SSL_CLIENT_VERIFY' => 'SUCCESS', 'SSL_CLIENT_S_DN' => 'CN=Bob,C=AT'], 'bob'],
  50. ];
  51. }
  52. }