RefreshTokenService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace services\oauth2;
  3. use common\components\Service;
  4. use common\models\oauth2\RefreshToken;
  5. /**
  6. * Class RefreshTokenService
  7. * @package services\oauth2
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class RefreshTokenService extends Service
  11. {
  12. /**
  13. * @param $client_id
  14. * @param $grant_type
  15. * @param $refresh_token
  16. * @param $expires
  17. * @param $member_id
  18. * @param $scopes
  19. */
  20. public function create($client_id, $grant_type, $refresh_token, $expires, $member_id, $scopes)
  21. {
  22. if (!($model = $this->findByClientId($client_id, $grant_type, $member_id))) {
  23. $model = new RefreshToken();
  24. $model->client_id = $client_id;
  25. $model->grant_type = $grant_type;
  26. $model->member_id = (string)$member_id;
  27. }
  28. $model->expires = $expires;
  29. $model->refresh_token = $refresh_token;
  30. $model->scope = $scopes;
  31. $model->save();
  32. }
  33. /**
  34. * @param $tokenId
  35. */
  36. public function deleteByRefreshToken($tokenId)
  37. {
  38. RefreshToken::deleteAll(['refresh_token' => $tokenId]);
  39. }
  40. /**
  41. * @param $tokenId
  42. * @return array|null|\yii\db\ActiveRecord
  43. */
  44. public function findByRefreshToken($tokenId)
  45. {
  46. return RefreshToken::find()
  47. ->where(['refresh_token' => $tokenId])
  48. ->one();
  49. }
  50. /**
  51. * @param $client_id
  52. * @param $grant_type
  53. * @return array|\yii\db\ActiveRecord|null
  54. */
  55. public function findByClientId($client_id, $grant_type, $member_id)
  56. {
  57. return RefreshToken::find()
  58. ->where([
  59. 'client_id' => $client_id,
  60. 'grant_type' => $grant_type,
  61. 'member_id' => $member_id
  62. ])
  63. ->one();
  64. }
  65. }
粤ICP备19079148号