OAuthAccessToken.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 string $expires_at 过期时间
  16. * @property object|array $scope 权限范围
  17. * @property int $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 $fillable = [
  38. 'client_id',
  39. 'user_id',
  40. 'access_token',
  41. 'refresh_token',
  42. 'expires_at',
  43. 'scope',
  44. 'revoked'
  45. ];
  46. protected $casts = [
  47. 'expires_at' => 'datetime',
  48. 'revoked' => 'boolean',
  49. 'scope' => 'json'
  50. ];
  51. /**
  52. * 获取关联的用户
  53. */
  54. public function user(): BelongsTo
  55. {
  56. return $this->belongsTo(User::class);
  57. }
  58. /**
  59. * 获取关联的客户端
  60. */
  61. public function client(): BelongsTo
  62. {
  63. return $this->belongsTo(OAuthClient::class, 'client_id');
  64. }
  65. /**
  66. * 检查令牌是否已过期
  67. */
  68. public function isExpired(): bool
  69. {
  70. return $this->expires_at->isPast();
  71. }
  72. /**
  73. * 检查令牌是否有效
  74. */
  75. public function isValid(): bool
  76. {
  77. return !$this->isExpired() && !$this->revoked;
  78. }
  79. }