AuthCodeRepository.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace oauth2\repository;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use oauth2\entity\AuthCodeEntity;
  6. use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
  7. use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
  8. /**
  9. * Class AuthCodeRepository
  10. * @package oauth2
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class AuthCodeRepository implements AuthCodeRepositoryInterface
  14. {
  15. /**
  16. * 创建新授权码时调用方法
  17. *
  18. * @return AuthCodeEntityInterface
  19. */
  20. public function getNewAuthCode()
  21. {
  22. // 需要返回 AuthCodeEntityInterface 对象
  23. return new AuthCodeEntity();
  24. }
  25. /**
  26. * 新的auth代码持久存储到永久存储区
  27. *
  28. * @param AuthCodeEntityInterface $authCodeEntity
  29. */
  30. public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
  31. {
  32. // 获得刷新令牌过期时间
  33. $date = $authCodeEntity->getExpiryDateTime(); // 获得令牌过期时间
  34. $date = Json::decode(Json::encode($date));
  35. // 创建token
  36. Yii::$app->services->oauth2AuthorizationCode->create(
  37. $authCodeEntity->getClient()->getIdentifier(), // 获得客户端标识符
  38. $authCodeEntity->getIdentifier(), // 获得刷新令牌唯一标识符
  39. $date['date'],
  40. $authCodeEntity->getUserIdentifier(), // 获得用户标识符
  41. $authCodeEntity->getScopes()// 获得权限范围
  42. );
  43. }
  44. /**
  45. * Revoke an auth code.
  46. *
  47. * @param string $codeId
  48. */
  49. public function revokeAuthCode($codeId)
  50. {
  51. // 当使用授权码获取访问令牌时调用此方法
  52. // 可以在此时将授权码从持久化数据库中删除
  53. // 参数为授权码唯一标识符
  54. Yii::$app->services->oauth2AuthorizationCode->deleteByAuthorizationCode($codeId);
  55. }
  56. /**
  57. * Check if the auth code has been revoked.
  58. *
  59. * @param string $codeId
  60. *
  61. * @return bool Return true if this code has been revoked
  62. */
  63. public function isAuthCodeRevoked($codeId)
  64. {
  65. // 当使用授权码获取访问令牌时调用此方法
  66. // 用于验证授权码是否已被删除
  67. // return true 已删除,false 未删除
  68. return empty(Yii::$app->services->oauth2AuthorizationCode->findByAuthorizationCode($codeId));
  69. }
  70. }
粤ICP备19079148号