1 | <?php |
||
2 | |||
3 | namespace Yansongda\LaravelApi\Guards; |
||
4 | |||
5 | use Illuminate\Auth\GuardHelpers; |
||
6 | use Illuminate\Contracts\Auth\Guard; |
||
7 | use Illuminate\Http\Request; |
||
8 | use Illuminate\Support\Carbon; |
||
9 | use Yansongda\LaravelApi\Exceptions\AccessTokenExpiredException; |
||
10 | use Yansongda\LaravelApi\Exceptions\AccessTokenNotProvidedException; |
||
11 | use Yansongda\LaravelApi\Exceptions\InvalidAccessTokenException; |
||
12 | use Yansongda\LaravelApi\Models\AccessToken; |
||
13 | use Yansongda\LaravelApi\Models\App; |
||
14 | |||
15 | class TokenGuard implements Guard |
||
16 | { |
||
17 | use GuardHelpers; |
||
18 | |||
19 | /** |
||
20 | * The request instance. |
||
21 | * |
||
22 | * @var Request |
||
23 | */ |
||
24 | protected $request; |
||
25 | |||
26 | /** |
||
27 | * The app. |
||
28 | * |
||
29 | * @var App |
||
30 | */ |
||
31 | protected $app; |
||
32 | |||
33 | /** |
||
34 | * The name of the query string item from the request containing the API token. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $inputKey; |
||
39 | |||
40 | /** |
||
41 | * Bootstrap. |
||
42 | * |
||
43 | * @author yansongda <[email protected]> |
||
44 | * |
||
45 | * @param Request $request |
||
46 | */ |
||
47 | public function __construct(Request $request) |
||
48 | { |
||
49 | $this->request = $request; |
||
50 | $this->inputKey = 'access_token'; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get the currently authenticated user. |
||
55 | * |
||
56 | * @author yansongda <[email protected]> |
||
57 | * |
||
58 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
59 | */ |
||
60 | public function user() |
||
61 | { |
||
62 | if (! is_null($this->user)) { |
||
63 | return $this->user; |
||
64 | } |
||
65 | |||
66 | $accessToken = $this->parseAccessToken(); |
||
67 | |||
68 | return $this->user = $accessToken->user; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get the currently app. |
||
73 | * |
||
74 | * @author yansongda <[email protected]> |
||
75 | * |
||
76 | * @return App |
||
77 | */ |
||
78 | public function app() |
||
79 | { |
||
80 | if (! is_null($this->app)) { |
||
81 | return $this->app; |
||
82 | } |
||
83 | |||
84 | $accessToken = $this->parseAccessToken(); |
||
85 | |||
86 | return $this->app = $accessToken->app; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Validate the accessToken. |
||
91 | * |
||
92 | * @author yansongda <[email protected]> |
||
93 | * |
||
94 | * @param array $credentials |
||
95 | * |
||
96 | * @return bool |
||
97 | * @throws AccessTokenExpiredException |
||
98 | * @throws InvalidAccessTokenException |
||
99 | */ |
||
100 | public function validate(array $credentials = []) |
||
101 | { |
||
102 | if ($this->queryAccessToken($credentials['access_token'])) { |
||
103 | return true; |
||
104 | } |
||
105 | |||
106 | return false; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Parse accessToken. |
||
111 | * |
||
112 | * @author yansongda <[email protected]> |
||
113 | * |
||
114 | * @return AccessToken |
||
115 | * @throws AccessTokenExpiredException |
||
116 | * @throws AccessTokenNotProvidedException |
||
117 | * @throws InvalidAccessTokenException |
||
118 | */ |
||
119 | protected function parseAccessToken() |
||
120 | { |
||
121 | $token = $this->findAccessToken(); |
||
122 | |||
123 | return $this->queryAccessToken($token); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Get access_token. |
||
128 | * |
||
129 | * @author yansongda <[email protected]> |
||
130 | * |
||
131 | * @return string |
||
132 | * @throws AccessTokenNotProvidedException |
||
133 | */ |
||
134 | protected function findAccessToken() |
||
135 | { |
||
136 | $token = $this->request->query($this->inputKey); |
||
137 | |||
138 | if (empty($token)) { |
||
139 | $token = $this->request->bearerToken(); |
||
140 | } |
||
141 | |||
142 | if (empty($token)) { |
||
143 | $token = $this->request->getPassword(); |
||
144 | } |
||
145 | |||
146 | if (! empty($token)) { |
||
147 | return $token; |
||
148 | } |
||
149 | |||
150 | throw new AccessTokenNotProvidedException('AccessToken Is Not Provided'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Query accessToken. |
||
155 | * |
||
156 | * @author yansongda <[email protected]> |
||
157 | * |
||
158 | * @param string $token |
||
159 | * |
||
160 | * @return AccessToken |
||
161 | * @throws AccessTokenExpiredException |
||
162 | * @throws InvalidAccessTokenException |
||
163 | */ |
||
164 | protected function queryAccessToken($token) |
||
165 | { |
||
166 | if (is_null($accessToken = AccessToken::where('access_token', $token)->first())) { |
||
167 | throw new InvalidAccessTokenException('AccessToken Is Invalid'); |
||
168 | } |
||
169 | |||
170 | if (Carbon::now()->lte($accessToken->expired_at)) { |
||
171 | return $accessToken; |
||
172 | } |
||
173 | |||
174 | throw new AccessTokenExpiredException( |
||
175 | 'AccessToken Is Expired', |
||
176 | ['now' => Carbon::now(), 'expired' => $accessToken->expired_at] |
||
177 | ); |
||
178 | } |
||
179 | } |
||
180 |