-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseModel.php
160 lines (138 loc) · 4.35 KB
/
BaseModel.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace yuncms\oauth2;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yuncms\oauth2\models\Client;
/**
* Class BaseModel
* @package yuncms\oauth2
*/
abstract class BaseModel extends Model
{
/**
* @var Client
*/
protected $_client;
/**
* @link https://tools.ietf.org/html/rfc6749#section-7.1
* @var string
*/
public $tokenType = 'bearer';
/**
* Authorization Code lifetime
* 30 seconds by default
* @var integer
*/
public $authCodeLifetime = 30;
/**
* Access Token lifetime
* 15 days by default
* @var integer
*/
public $accessTokenLifetime = 1296000;
/**
* Refresh Token lifetime
* 30 days by default
* @var integer
*/
public $refreshTokenLifetime = 2592000;
public function init()
{
$headers = [
'client_id' => 'PHP_AUTH_USER',
'client_secret' => 'PHP_AUTH_PW',
];
foreach ($this->safeAttributes() as $attribute) {
$this->$attribute = self::getRequestValue($attribute, ArrayHelper::getValue($headers, $attribute));
}
}
public function addError($attribute, $error = "")
{
throw new Exception($error, Exception::INVALID_REQUEST);
}
public function errorServer($error, $type = Exception::INVALID_REQUEST)
{
throw new Exception($error, Exception::INVALID_REQUEST);
}
public function errorRedirect($error, $type = Exception::INVALID_REQUEST)
{
$redirectUri = isset($this->redirect_uri) ? $this->redirect_uri : $this->getClient()->redirect_uri;
if ($redirectUri) {
throw new RedirectException($redirectUri, $error, $type, isset($this->state) ? $this->state : null);
} else {
throw new Exception($error, $type);
}
}
abstract function getResponseData();
public static function getRequestValue($param, $header = null)
{
static $request;
if (is_null($request)) {
$request = Yii::$app->request;
}
if ($header && ($result = $request->headers->get($header))) {
return $result;
} else {
return $request->post($param, $request->get($param));
}
}
/**
*
* @return \yuncms\oauth2\models\Client
*/
public function getClient()
{
if (is_null($this->_client)) {
if (empty($this->client_id)) {
$this->errorServer('Unknown client', Exception::INVALID_CLIENT);
}
if (!$this->_client = Client::findOne(['client_id' => $this->client_id])) {
$this->errorServer('Unknown client', Exception::INVALID_CLIENT);
}
}
return $this->_client;
}
public function validateClient_id($attribute, $params)
{
$this->getClient();
}
public function validateClient_secret($attribute, $params)
{
if (!Yii::$app->security->compareString($this->getClient()->client_secret, $this->$attribute)) {
$this->addError($attribute, 'The client credentials are invalid');
}
}
public function validateRedirect_uri($attribute, $params)
{
if (!empty($this->$attribute)) {
$clientRedirectUri = $this->getClient()->redirect_uri;
if (strncasecmp($this->$attribute, $clientRedirectUri, strlen($clientRedirectUri)) !== 0) {
$this->errorServer('The redirect URI provided is missing or does not match', Exception::REDIRECT_URI_MISMATCH);
}
}
}
public function validateScope($attribute, $params)
{
if (!$this->checkSets($this->$attribute, $this->_client->scope)) {
$this->errorRedirect('The requested scope is invalid, unknown, or malformed.', Exception::INVALID_SCOPE);
}
}
/**
* Checks if everything in required set is contained in available set.
*
* @param string|array $requiredSet
* @param string|array $availableSet
* @return boolean
*/
protected function checkSets($requiredSet, $availableSet)
{
if (!is_array($requiredSet)) {
$requiredSet = explode(' ', trim($requiredSet));
}
if (!is_array($availableSet)) {
$availableSet = explode(' ', trim($availableSet));
}
return (count(array_diff($requiredSet, $availableSet)) == 0);
}
}