-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckToken.php
More file actions
77 lines (66 loc) · 2.11 KB
/
CheckToken.php
File metadata and controls
77 lines (66 loc) · 2.11 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
<?php
namespace gud3\restAuth;
use Yii;
use yii\filters\auth\AuthMethod;
use yii\redis\Session;
use gud3\restAuth\models\Auth;
/**
* Class CheckToken
* @package gud3\restAuth
*/
class CheckToken extends AuthMethod
{
const DELETE = 1;
private $status;
/**
* @param \yii\web\User $user
* @param \yii\web\Request $request
* @param \yii\web\Response $response
* @return null|\yii\web\IdentityInterface
* @throws \Exception
* @throws \Throwable
*/
public function authenticate($user, $request, $response)
{
if ($key = self::isAuth()) {
$data_auth = Auth::find()->where(['series' => $key[1]])->one();
if ($data_auth) {
$session = new Session();
$session->open();
if ($data_auth->token !== $key[0]) {
$this->status = self::DELETE;
} elseif ($data_auth->date_end <= time()) {
$this->status = self::DELETE;
} elseif ($data_auth->token === $key[0]) {
//Identity user
$identity = $user->loginByAccessToken($data_auth->user_id, get_class($this));
if ($identity === null || $identity === false) {
$this->status = self::DELETE;
} else {
//Compare token with session in redis
if ($session->get('token') != $data_auth->token) {
$data_auth->changeToken(true);
}
return $identity;
}
}
$this->status === self::DELETE ? $data_auth->delete() : null;
}
}
return null;
}
/**
* @return array|bool
*/
public static function isAuth()
{
$accessToken = Yii::$app->request->headers->get('Authorization');
if (is_string($accessToken)) {
$key = explode(';', $accessToken);
//$key[0] = token
//$key[1] = series
return $key;
}
return false;
}
}