OAuthAccessToken.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Module\OAuth\Models;
  3. use App\Models\User;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. /**
  7. * App\Module\OAuth\Models\OAuthAccessToken
  8. *
  9. * field start
  10. * @property int $id
  11. * @property string $client_id 客户端ID
  12. * @property int $user_id 用户ID
  13. * @property string $access_token 访问令牌
  14. * @property string $refresh_token
  15. * @property \Carbon\Carbon $expires_at 过期时间
  16. * @property array $scope 权限范围
  17. * @property bool $revoked
  18. * @property \Carbon\Carbon $created_at
  19. * @property \Carbon\Carbon $updated_at
  20. * field end
  21. */
  22. class OAuthAccessToken extends Model
  23. {
  24. protected $table = 'oauth_access_tokens';
  25. // attrlist start
  26. protected $fillable = [
  27. 'id',
  28. 'client_id',
  29. 'user_id',
  30. 'access_token',
  31. 'refresh_token',
  32. 'expires_at',
  33. 'scope',
  34. 'revoked',
  35. ];
  36. // attrlist end
  37. protected $casts = [
  38. 'expires_at' => 'datetime',
  39. 'revoked' => 'boolean',
  40. 'scope' => 'json'
  41. ];
  42. /**
  43. * 获取关联的用户
  44. */
  45. public function user(): BelongsTo
  46. {
  47. return $this->belongsTo(User::class);
  48. }
  49. /**
  50. * 获取关联的客户端
  51. */
  52. public function client(): BelongsTo
  53. {
  54. return $this->belongsTo(OAuthClient::class, 'client_id');
  55. }
  56. /**
  57. * 检查令牌是否已过期
  58. */
  59. public function isExpired(): bool
  60. {
  61. return $this->expires_at->isPast();
  62. }
  63. /**
  64. * 检查令牌是否有效
  65. */
  66. public function isValid(): bool
  67. {
  68. return !$this->isExpired() && !$this->revoked;
  69. }
  70. }