AuthTokenMiddleware.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\outapi\middleware;
  12. use app\Request;
  13. use app\services\out\OutAccountServices;
  14. use app\services\out\OutInterfaceServices;
  15. use crmeb\interfaces\MiddlewareInterface;
  16. use think\facade\Config;
  17. /**
  18. * Class AuthTokenMiddleware
  19. * @package app\outapi\middleware
  20. */
  21. class AuthTokenMiddleware implements MiddlewareInterface
  22. {
  23. /**
  24. * @param Request $request
  25. * @param \Closure $next
  26. * @return mixed
  27. * @throws \Psr\SimpleCache\InvalidArgumentException
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function handle(Request $request, \Closure $next)
  33. {
  34. $token = trim(ltrim($request->header(Config::get('cookie.token_name', 'Authori-zation')), 'Bearer'));
  35. /** @var OutAccountServices $services */
  36. $services = app()->make(OutAccountServices::class);
  37. $outInfo = $services->parseToken($token);
  38. $request->macro('outId', function () use (&$outInfo) {
  39. return (int)$outInfo['id'];
  40. });
  41. $request->macro('outInfo', function () use (&$outInfo) {
  42. return $outInfo;
  43. });
  44. /** @var OutInterfaceServices $outInterfaceServices */
  45. $outInterfaceServices = app()->make(OutInterfaceServices::class);
  46. $outInterfaceServices->verifyAuth($request);
  47. return $next($request);
  48. }
  49. }