Skip to content

Commit 6c3e288

Browse files
Stable release
1 parent a49abc6 commit 6c3e288

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CHANGELOG
22
==============
33

4-
1.0.0-alpha
4+
1.0.0
55
-----------------
6-
* Can be used
6+
* Stable release

HttpBasicAuthComponent.php

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,65 @@ class HttpBasicAuthComponent extends Component
2424
*/
2525
public $password = 'password';
2626

27+
/**
28+
* @var bool
29+
*/
30+
public $usePasswordHash = false;
31+
32+
/**
33+
* @var null
34+
*/
35+
public $viewFail = null;
2736

28-
public function execute()
37+
38+
public function verify()
2939
{
30-
if (!isset($_SERVER['PHP_AUTH_PW']))
40+
if (\Yii::$app->request->authUser != $this->login || !$this->_verifyPassword())
3141
{
32-
$this->_authentificateHttp();
42+
$this->_fail();
3343
}
44+
}
3445

35-
if(!@$_SERVER['PHP_AUTH_PW'])
46+
/**
47+
* @return bool
48+
*/
49+
protected function _verifyPassword()
50+
{
51+
if ($this->usePasswordHash)
3652
{
37-
$this->_authentificateHttp();
38-
}
39-
40-
else
53+
return (bool) (md5(\Yii::$app->request->authPassword) == $this->password);
54+
} else
4155
{
42-
if (@$_SERVER['PHP_AUTH_USER'] != $this->login || @$_SERVER['PHP_AUTH_PW'] != $this->password)
43-
{
44-
$this->_authentificateHttp();
45-
}
56+
return (bool) (\Yii::$app->request->authPassword == $this->password);
4657
}
4758
}
4859

49-
protected function _authentificateHttp()
60+
protected function _fail()
5061
{
51-
$appName = \Yii::$app->id;
52-
Header("WWW-Authenticate: Basic realm=\"{$appName}\"");
53-
Header("HTTP/1.0 401 Unauthorized");
54-
echo <<<HTML
55-
<style>
56-
.sx-title
57-
{
58-
text-align: center;
59-
font-size: 20px;
60-
padding: 200px;
61-
}
62-
</style>
63-
<p class="sx-title">{$appName} Authorization required.</p>
62+
$appName = \Yii::$app->name;
63+
64+
if ($this->viewFail)
65+
{
66+
Header("WWW-Authenticate: Basic realm=\"{$appName}\"");
67+
Header("HTTP/1.0 401 Unauthorized");
68+
69+
echo \Yii::$app->view->render($this->viewFail);
70+
} else
71+
{
72+
Header("WWW-Authenticate: Basic realm=\"{$appName}\"");
73+
Header("HTTP/1.0 401 Unauthorized");
74+
echo <<<HTML
75+
<style>
76+
.sx-title
77+
{
78+
text-align: center;
79+
font-size: 20px;
80+
padding: 200px;
81+
}
82+
</style>
83+
<p class="sx-title">{$appName} Authorization required.</p>
6484
HTML;
85+
}
6586

6687
exit;
6788
}

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,22 @@ Configuration app
2323

2424
```php
2525

26-
26+
'on beforeRequest' => function ($event) {
27+
\Yii::$app->httpBasicAuth->verify();
28+
},
29+
30+
'components' =>
31+
[
32+
'httpBasicAuth' =>
33+
[
34+
'class' => 'skeeks\yii2\httpBasicAuth\HttpBasicAuthComponent',
35+
'login' => 'login',
36+
'password' => 'password',
37+
'usePasswordHash' => false, //optionality
38+
'viewFail' => '@app/views/http-basic-auth-fail' //optionality
39+
],
40+
41+
]
2742

2843
```
2944

_ide/YiiApplication.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
*
4+
* The pseudo-only IDE tips
5+
*
6+
* @author Semenov Alexander <semenov@skeeks.com>
7+
* @link http://skeeks.com/
8+
* @copyright 2010 SkeekS (СкикС)
9+
* @date 10.09.2015
10+
*/
11+
namespace yii\web;
12+
use skeeks\yii2\httpBasicAuth\HttpBasicAuthComponent;
13+
14+
/**
15+
* @property HttpBasicAuthComponent $httpBasicAuth
16+
17+
* Class Application
18+
* @package yii\web
19+
*/
20+
class Application
21+
{}

0 commit comments

Comments
 (0)