OAuthService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Grant\AuthCodeGrant;
  7. use League\OAuth2\Server\Grant\RefreshTokenGrant;
  8. use League\OAuth2\Server\Grant\ClientCredentialsGrant;
  9. use League\OAuth2\Server\Grant\PasswordGrant;
  10. use League\OAuth2\Server\CryptKey;
  11. use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
  12. use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
  13. use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
  14. use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
  15. use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
  16. use League\OAuth2\Server\Repositories\UserRepositoryInterface;
  17. use DateInterval;
  18. class OAuthService
  19. {
  20. protected $server;
  21. public function __construct(
  22. ClientRepositoryInterface $clientRepository,
  23. AccessTokenRepositoryInterface $accessTokenRepository,
  24. ScopeRepositoryInterface $scopeRepository,
  25. AuthCodeRepositoryInterface $authCodeRepository,
  26. RefreshTokenRepositoryInterface $refreshTokenRepository,
  27. UserRepositoryInterface $userRepository
  28. ) {
  29. // 初始化授权服务器
  30. $this->server = new AuthorizationServer(
  31. $clientRepository,
  32. $accessTokenRepository,
  33. $scopeRepository,
  34. new CryptKey(storage_path('oauth/private.key')),
  35. app('encrypter')->getKey()
  36. );
  37. // 配置授权类型
  38. $this->configureGrants(
  39. $authCodeRepository,
  40. $refreshTokenRepository,
  41. $userRepository
  42. );
  43. }
  44. protected function configureGrants(
  45. $authCodeRepository,
  46. $refreshTokenRepository,
  47. $userRepository
  48. ) {
  49. // 授权码模式
  50. $authCodeGrant = new AuthCodeGrant(
  51. $authCodeRepository,
  52. $refreshTokenRepository,
  53. new DateInterval('PT10M') // 授权码10分钟过期
  54. );
  55. $authCodeGrant->setRefreshTokenTTL(new DateInterval('P1M')); // 刷新令牌1个月过期
  56. $this->server->enableGrantType($authCodeGrant);
  57. // 密码模式
  58. $passwordGrant = new PasswordGrant($userRepository, $refreshTokenRepository);
  59. $passwordGrant->setRefreshTokenTTL(new DateInterval('P1M'));
  60. $this->server->enableGrantType($passwordGrant);
  61. // 客户端模式
  62. $clientCredentialsGrant = new ClientCredentialsGrant();
  63. $this->server->enableGrantType($clientCredentialsGrant);
  64. // 刷新令牌
  65. $refreshTokenGrant = new RefreshTokenGrant($refreshTokenRepository);
  66. $refreshTokenGrant->setRefreshTokenTTL(new DateInterval('P1M'));
  67. $this->server->enableGrantType($refreshTokenGrant);
  68. }
  69. public function getServer()
  70. {
  71. return $this->server;
  72. }
  73. public function createClient($name, $redirectUri, array $grantTypes = [], array $scopes = [])
  74. {
  75. return OAuthClient::create([
  76. 'name' => $name,
  77. 'client_id' => bin2hex(random_bytes(20)),
  78. 'client_secret' => bin2hex(random_bytes(20)),
  79. 'redirect_uri' => $redirectUri,
  80. 'grant_types' => $grantTypes,
  81. 'scope' => $scopes,
  82. ]);
  83. }
  84. public function validateToken($accessToken)
  85. {
  86. return OAuthAccessToken::where('access_token', $accessToken)
  87. ->where('expires_at', '>', now())
  88. ->first();
  89. }
  90. }