ApiAuthenticate.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Middleware;
  11. use App\Repositories\Enums\ResponseCodeEnum;
  12. use App\Support\Traits\LoginLimit;
  13. use Closure;
  14. use Illuminate\Auth\Access\AuthorizationException;
  15. use Illuminate\Contracts\Auth\Factory as Auth;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Log;
  18. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  19. use Tymon\JWTAuth\Exceptions\JWTException;
  20. use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  21. class ApiAuthenticate
  22. {
  23. use LoginLimit;
  24. /**
  25. * The authentication guard factory instance.
  26. *
  27. * @var Auth
  28. */
  29. protected $auth;
  30. /**
  31. * Create a new middleware instance.
  32. *
  33. * @param Auth $auth
  34. */
  35. public function __construct(Auth $auth)
  36. {
  37. $this->auth = $auth;
  38. }
  39. /**
  40. * Handle an incoming request.
  41. *
  42. * @param Request $request
  43. * @param Closure $next
  44. * @param string|null $guard
  45. *
  46. * @return mixed
  47. *
  48. * @throws AuthorizationException
  49. */
  50. public function handle($request, Closure $next, $guard = 'api')
  51. {
  52. try {
  53. if ($this->auth->guard($guard)->guest()) {
  54. abort(ResponseCodeEnum::HTTP_UNAUTHORIZED);
  55. }
  56. $token = \Illuminate\Support\Facades\Auth::getToken();
  57. $user_id = $this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub'];
  58. //单机登录检查
  59. if (!$this->singleLoginCheck($token, $user_id, $guard)) {
  60. Log::error("3");
  61. abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, '该账号已经再其他设备上登录');
  62. }
  63. Log::error("4");
  64. } catch (TokenExpiredException $exception) {
  65. // 3. 此处捕获到了 token 过期所抛出的 TokenExpiredException 异常,我们在这里需要做的是刷新该用户的 token 并将它添加到响应头中
  66. try {
  67. Log::error("6");
  68. // 刷新用户的 token
  69. $token = $this->auth->refresh();
  70. // 使用一次性登录以保证此次请求的成功
  71. $user_id = $this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub'];
  72. Auth::onceUsingId($user_id);
  73. $this->singleLoginSetToken($user_id, $token, $guard);
  74. Log::error("7");
  75. return $this->setAuthenticationHeader($next($request), $token);
  76. } catch (JWTException $exception) {
  77. Log::error("8");
  78. Log::error($exception);
  79. // 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。
  80. abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, 'token令牌过期,请重新登陆。');
  81. }
  82. }
  83. return $next($request);
  84. }
  85. /**
  86. * Set the authentication header.
  87. *
  88. * @param \Illuminate\Http\Response|\Illuminate\Http\JsonResponse $response
  89. * @param string|null $token
  90. *
  91. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  92. */
  93. protected function setAuthenticationHeader($response, $token = null)
  94. {
  95. $token = $token ?: $this->auth->refresh();
  96. $response->headers->set('Authorization', 'Bearer ' . $token);
  97. return $response;
  98. }
  99. /**
  100. * Attempt to authenticate a user via the token in the request.
  101. *
  102. * @param \Illuminate\Http\Request $request
  103. *
  104. * @return void
  105. * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
  106. *
  107. */
  108. public function authenticate(Request $request)
  109. {
  110. $this->checkForToken($request);
  111. try {
  112. if (!$this->auth->parseToken()->authenticate()) {
  113. throw new UnauthorizedHttpException('jwt-auth', 'User not found');
  114. }
  115. } catch (JWTException $e) {
  116. throw new UnauthorizedHttpException('jwt-auth', $e->getMessage(), $e, $e->getCode());
  117. }
  118. }
  119. /**
  120. * Check the request for the presence of a token.
  121. *
  122. * @param \Illuminate\Http\Request $request
  123. *
  124. * @return void
  125. * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
  126. *
  127. */
  128. public function checkForToken(Request $request)
  129. {
  130. if (!$this->auth->parser()->setRequest($request)->hasToken()) {
  131. throw new UnauthorizedHttpException('jwt-auth', 'Token not provided');
  132. }
  133. }
  134. }
  135. //
  136. ///*
  137. // * This file is part of the Jiannei/lumen-api-starter.
  138. // *
  139. // * (c) Jiannei <longjian.huang@foxmail.com>
  140. // *
  141. // * This source file is subject to the MIT license that is bundled
  142. // * with this source code in the file LICENSE.
  143. // */
  144. //
  145. //namespace App\Http\Middleware;
  146. //
  147. //use App\Repositories\Enums\ResponseCodeEnum;
  148. //use App\Support\Traits\LoginLimit;
  149. //use Closure;
  150. //use Illuminate\Auth\Access\AuthorizationException;
  151. //use Illuminate\Http\Request;
  152. //use Illuminate\Support\Facades\Auth;
  153. //use Illuminate\Support\Facades\Log;
  154. //use Tymon\JWTAuth\Exceptions\JWTException;
  155. //use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  156. //use Tymon\JWTAuth\Exceptions\TokenInvalidException;
  157. //use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
  158. //
  159. //class ApiAuthenticate extends BaseMiddleware
  160. //{
  161. // use LoginLimit;
  162. //
  163. // /**
  164. // * Handle an incoming request.
  165. // *
  166. // * @param Request $request
  167. // * @param Closure $next
  168. // * @param string|null $guard
  169. // * @return mixed
  170. // *
  171. // * @throws AuthorizationException
  172. // */
  173. // public function handle($request, Closure $next, $guard = 'api')
  174. // {
  175. // Auth::setDefaultDriver('api');
  176. //
  177. // try {
  178. // // 检查此次请求中是否带有 token,如果没有则抛出异常。
  179. // $this->checkForToken($request);
  180. // } catch (TokenInvalidException $exception) {
  181. // abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, 'token有误');
  182. // }
  183. //
  184. // //1. 格式通过,验证是否是专属于这个的token
  185. // //获取当前守护的名称
  186. //// $guard = Auth::getDefaultDriver();
  187. // Log::error("1");
  188. // //使用 try 包裹,以捕捉 token 过期所抛出的 TokenExpiredException 异常
  189. // //2. 此时进入的都是属于当前guard守护的token
  190. // try {
  191. // // 检测用户的登录状态,如果正常则通过
  192. //// if ($this->auth->parseToken()->authenticate()) {
  193. // if ($this->auth->guest()) {
  194. // Log::error("2");
  195. // $token = Auth::getToken();
  196. // $user_id = $this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub'];
  197. // //单机登录检查
  198. // if (!$this->singleLoginCheck($token, $user_id, $guard)) {
  199. // Log::error("3");
  200. // abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, '该账号已经再其他设备上登录');
  201. // }
  202. // Log::error("4");
  203. // return $next($request);
  204. // }
  205. // Log::error("5");
  206. // abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, 'token过期,请重新登陆。');
  207. // } catch (TokenExpiredException $exception) {
  208. // // 3. 此处捕获到了 token 过期所抛出的 TokenExpiredException 异常,我们在这里需要做的是刷新该用户的 token 并将它添加到响应头中
  209. // try {
  210. // Log::error("6");
  211. // // 刷新用户的 token
  212. // $token = $this->auth->refresh();
  213. // // 使用一次性登录以保证此次请求的成功
  214. // $user_id = $this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub'];
  215. // Auth::onceUsingId($user_id);
  216. //
  217. // $this->singleLoginSetToken($user_id, $token, $guard);
  218. // Log::error("7");
  219. // return $this->setAuthenticationHeader($next($request), $token);
  220. // } catch (JWTException $exception) {
  221. // Log::error("8");
  222. // Log::error($exception);
  223. // // 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。
  224. // abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, 'token令牌过期,请重新登陆。');
  225. // }
  226. // }
  227. // Log::error("9");
  228. // // 在响应头中返回新的 token
  229. // abort(ResponseCodeEnum::HTTP_UNAUTHORIZED, '异常错误-Mead');
  230. //// return $this->setAuthenticationHeader($next($request), $token);
  231. // }
  232. //}