1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Requests\GetTokenRequest; |
5
|
|
|
use Firebase\JWT\JWT; |
6
|
|
|
use App\Model\User; |
7
|
|
|
|
8
|
|
|
use Slim\Http\Request; |
9
|
|
|
use Slim\Http\Response; |
10
|
|
|
|
11
|
|
|
use App\Common\JsonException; |
12
|
|
|
|
13
|
|
|
final class TokenController extends BaseController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param int $tokenExpire |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
protected static function createToken(Request $request, $tokenExpire = 3600) |
22
|
|
|
{ |
23
|
|
|
$secret_key = getenv('SECRET_KEY'); |
24
|
|
|
$token = [ |
25
|
|
|
'iss' => getenv('AUTH_ISS'), |
26
|
|
|
'aud' => $request->getUri()->getHost(), |
27
|
|
|
'iat' => time(), |
28
|
|
|
'exp' => time() + $tokenExpire, |
29
|
|
|
]; |
30
|
|
|
$jwt = JWT::encode($token, $secret_key); |
31
|
|
|
return $jwt; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $token |
36
|
|
|
* @param array $whiteList |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public static function validateToken($token, $whiteList = []) |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
$payload = JWT::decode($token, getenv('SECRET_KEY'), ['HS256']); |
44
|
|
|
return in_array($payload->aud, $whiteList); |
45
|
|
|
} catch (\Exception $e){ |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @api {post} /token Получение токена |
52
|
|
|
* @apiName CreateToken |
53
|
|
|
* @apiGroup Token |
54
|
|
|
* |
55
|
|
|
* @apiDescription Метод для получения авторизационного токена. Он отправляется в заголовке запроса: |
56
|
|
|
* |
57
|
|
|
* Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
58
|
|
|
* |
59
|
|
|
* @apiParam {String} username Логин |
60
|
|
|
* @apiParam {String} password Пароль |
61
|
|
|
* |
62
|
|
|
* @apiParamExample {json} Пример запроса: |
63
|
|
|
* { |
64
|
|
|
* "data":{ |
65
|
|
|
* "attributes":{ |
66
|
|
|
* "username":"[email protected]", |
67
|
|
|
* "password": "qwerty" |
68
|
|
|
* } |
69
|
|
|
* } |
70
|
|
|
* } |
71
|
|
|
* |
72
|
|
|
* @apiSuccessExample {json} Успешно (200) |
73
|
|
|
* HTTP/1.1 200 OK |
74
|
|
|
* { |
75
|
|
|
* "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOmZhbHNlLCJhdWQiOiJza2VsZXRvbi5kZXYiLCJpYXQiOjE0NzY0Mjk4NjksImV4cCI6MTQ3NjQzMzQ2OX0.NJn_-lK28kEZyZqygLr6B-FZ2zC2-1unStayTGicP5g", |
76
|
|
|
* "user": { |
77
|
|
|
* "id": 1, |
78
|
|
|
* "email": "[email protected]", |
79
|
|
|
* "full_name": "Тестовый пользоатель", |
80
|
|
|
* "role_id": "1", |
81
|
|
|
* "created_by": 0, |
82
|
|
|
* "updated_by": null, |
83
|
|
|
* "created_at": "2016-07-24 14:07:54", |
84
|
|
|
* "updated_at": "2016-10-14 10:24:29", |
85
|
|
|
* "deleted_at": null, |
86
|
|
|
* "status": 1 |
87
|
|
|
* } |
88
|
|
|
* } |
89
|
|
|
* |
90
|
|
|
* @apiUse StandardErrors |
91
|
|
|
*/ |
92
|
|
|
/** |
93
|
|
|
* @param Request $request |
94
|
|
|
* @param Response $response |
95
|
|
|
* |
96
|
|
|
* @return mixed |
|
|
|
|
97
|
|
|
* @throws JsonException |
98
|
|
|
*/ |
99
|
|
|
public function auth(Request $request, Response $response) |
100
|
|
|
{ |
101
|
|
|
$params = $request->getParsedBody(); |
102
|
|
|
|
103
|
|
|
$this->validationRequest($params, 'token', new GetTokenRequest()); |
104
|
|
|
|
105
|
|
|
$user = User::findUserByEmail($params['data']['attributes']['username']); |
106
|
|
|
|
107
|
|
|
if ($user && password_verify($params['data']['attributes']['password'], $user->password)) { |
108
|
|
|
$token = self::createToken($request, $this->settings['params']['tokenExpire']); |
109
|
|
|
$user->access_token = md5($token); |
110
|
|
|
$user->save(); |
111
|
|
|
} else { |
112
|
|
|
throw new JsonException('token', 400, 'Invalid Attribute', 'Invalid password or username'); |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
$result = [ |
116
|
|
|
'access_token' => $token, |
117
|
|
|
'user' => $user->toArray() |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
return $this->renderer->jsonApiRender($response, 200, json_encode($result)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.