ApiRefreshToken.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\common\models;
  3. use app\common\exceptions\TokenHasExpiredException;
  4. use app\common\exceptions\TokenHasRevokedException;
  5. use app\common\exceptions\TokenNotFoundException;
  6. use Carbon\Carbon;
  7. /**
  8. * Class ErpApiRefreshToken
  9. * @package Yunshop\ErpApi\common\models
  10. * @property bool revoked
  11. * @property Carbon expires_at
  12. * @property string refresh_token
  13. * @property int uniacid
  14. * @property int id
  15. */
  16. class ApiRefreshToken extends BaseModel
  17. {
  18. public $table = 'yz_api_refresh_token';
  19. public $timestamps = true;
  20. protected $guarded = [''];
  21. /**
  22. * @param $uniacid
  23. * @param $token
  24. * @return bool
  25. * @throws TokenHasExpiredException
  26. * @throws TokenHasRevokedException
  27. * @throws TokenNotFoundException
  28. */
  29. static public function verify($uniacid, $token)
  30. {
  31. $refreshToken = self::where([
  32. 'uniacid' => $uniacid,
  33. 'refresh_token' => $token,
  34. ])->first();
  35. if (!$refreshToken) {
  36. throw new TokenNotFoundException();
  37. }
  38. return $refreshToken->valid();
  39. }
  40. /**
  41. * @return bool
  42. * @throws TokenHasExpiredException
  43. * @throws TokenHasRevokedException
  44. */
  45. public function valid()
  46. {
  47. if ($this->expires_at <= time()) {
  48. throw new TokenHasExpiredException();
  49. }
  50. if ($this->revoked) {
  51. throw new TokenHasRevokedException();
  52. }
  53. return true;
  54. }
  55. }