-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpBasicAuthComponent.php
More file actions
98 lines (83 loc) · 2.19 KB
/
HttpBasicAuthComponent.php
File metadata and controls
98 lines (83 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* @author Semenov Alexander <[email protected]>
* @link http://skeeks.com/
* @copyright 2010 SkeekS (СкикС)
* @date 17.04.2016
*/
namespace skeeks\yii2\httpBasicAuth;
use yii\base\Component;
/**
* Class HttpBasicAuthComponent
* @package skeeks\yii2\httpBasicAuth
*/
class HttpBasicAuthComponent extends Component
{
/**
* @var string
*/
public $login = 'login';
/**
* @var string
*/
public $password = 'password';
/**
* @var bool
*/
public $usePasswordHash = false;
/**
* @var null
*/
public $viewFail = null;
public function verify()
{
if (\Yii::$app->request->authUser != $this->login || !$this->_verifyPassword())
{
$this->_fail();
}
}
/**
* @return bool
*/
protected function _verifyPassword()
{
if ($this->usePasswordHash)
{
return (bool) (md5(\Yii::$app->request->authPassword) == $this->password);
} else
{
return (bool) (\Yii::$app->request->authPassword == $this->password);
}
}
protected function _fail()
{
$appName = \Yii::$app->name;
$user = \Yii::$app->request->authUser;
$password = \Yii::$app->request->authPassword;
$ip = \Yii::$app->request->userIP;
if ($this->viewFail)
{
Header("WWW-Authenticate: Basic realm=\"{$appName}\"");
Header("HTTP/1.0 401 Unauthorized");
\Yii::error("Fail http basic auth $user@$password ({$ip})", self::className());
echo \Yii::$app->view->render($this->viewFail);
} else
{
Header("WWW-Authenticate: Basic realm=\"{$appName}\"");
Header("HTTP/1.0 401 Unauthorized");
\Yii::error("Fail http basic auth $user@$password ({$ip})", self::className());
echo <<<HTML
<style>
.sx-title
{
text-align: center;
font-size: 20px;
padding: 200px;
}
</style>
<p class="sx-title">{$appName} Authorization required.</p>
HTML;
}
exit;
}
}