OAuth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Module\OAuth\Services;
  3. use App\Module\OAuth\Models\OAuthClient;
  4. use App\Module\OAuth\Models\OAuthAccessToken;
  5. use League\OAuth2\Server\AuthorizationServer;
  6. use League\OAuth2\Server\CryptKey;
  7. use League\OAuth2\Server\Entities\ClientEntityInterface;
  8. use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
  9. use League\OAuth2\Server\Grant\AuthCodeGrant;
  10. use League\OAuth2\Server\Grant\RefreshTokenGrant;
  11. use League\OAuth2\Server\Grant\ClientCredentialsGrant;
  12. use League\OAuth2\Server\Grant\PasswordGrant;
  13. use DateInterval;
  14. use Carbon\Carbon;
  15. class OAuth
  16. {
  17. /**
  18. * 创建客户端
  19. */
  20. public static function createClient(string $name, string $redirectUri, array $grantTypes = [], array $scopes = []): OAuthClient
  21. {
  22. return OAuthClient::create([
  23. 'name' => $name,
  24. 'client_id' => bin2hex(random_bytes(20)),
  25. 'client_secret' => bin2hex(random_bytes(20)),
  26. 'redirect_uri' => $redirectUri,
  27. 'grant_types' => $grantTypes,
  28. 'scope' => $scopes,
  29. ]);
  30. }
  31. /**
  32. * 验证客户端
  33. */
  34. public static function validateClient(string $clientId, string $clientSecret, string $grantType): bool
  35. {
  36. $client = OAuthClient::where('client_id', $clientId)
  37. ->where('client_secret', $clientSecret)
  38. ->first();
  39. if (!$client) {
  40. return false;
  41. }
  42. return in_array($grantType, $client->grant_types);
  43. }
  44. /**
  45. * 创建访问令牌
  46. */
  47. public static function createAccessToken(string $clientId, ?int $userId = null, array $scopes = [], int $expiresIn = 3600): OAuthAccessToken
  48. {
  49. return OAuthAccessToken::create([
  50. 'client_id' => $clientId,
  51. 'user_id' => $userId,
  52. 'access_token' => bin2hex(random_bytes(40)),
  53. 'expires_at' => now()->addSeconds($expiresIn),
  54. 'scope' => $scopes,
  55. ]);
  56. }
  57. /**
  58. * 验证访问令牌
  59. *
  60. * @param string $token 访问令牌
  61. * @return OAuthAccessToken|null 如果令牌有效返回令牌对象,否则返回null
  62. */
  63. public static function validateAccessToken(string $token): ?OAuthAccessToken
  64. {
  65. $accessToken = OAuthAccessToken::where('access_token', $token)
  66. ->where('revoked', false)
  67. ->where('expires_at', '>', Carbon::now())
  68. ->first();
  69. return $accessToken;
  70. }
  71. /**
  72. * 撤销访问令牌
  73. */
  74. public static function revokeToken(string $accessToken): bool
  75. {
  76. return OAuthAccessToken::where('access_token', $accessToken)->delete() > 0;
  77. }
  78. /**
  79. * 获取客户端信息
  80. */
  81. public static function getClient(string $clientId): ?OAuthClient
  82. {
  83. return OAuthClient::where('client_id', $clientId)->first();
  84. }
  85. /**
  86. * 获取用户的所有访问令牌
  87. */
  88. public static function getUserTokens(int $userId): array
  89. {
  90. return OAuthAccessToken::where('user_id', $userId)
  91. ->where('expires_at', '>', now())
  92. ->get()
  93. ->toArray();
  94. }
  95. /**
  96. * 获取客户端的所有访问令牌
  97. */
  98. public static function getClientTokens(string $clientId): array
  99. {
  100. return OAuthAccessToken::where('client_id', $clientId)
  101. ->where('expires_at', '>', now())
  102. ->get()
  103. ->toArray();
  104. }
  105. /**
  106. * 清理过期的令牌
  107. */
  108. public static function cleanExpiredTokens(): int
  109. {
  110. return OAuthAccessToken::where('expires_at', '<=', now())->delete();
  111. }
  112. }