ClientRepository.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace oauth2\repository;
  3. use Yii;
  4. use yii\web\UnprocessableEntityHttpException;
  5. use oauth2\entity\ClientEntity;
  6. use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
  7. use League\OAuth2\Server\Entities\ClientEntityInterface;
  8. /**
  9. * Class ClientRepository
  10. * @package oauth2
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class ClientRepository implements ClientRepositoryInterface
  14. {
  15. /**
  16. * Get a client.
  17. *
  18. * @param string $clientIdentifier The client's identifier
  19. *
  20. * @return ClientEntityInterface|null
  21. */
  22. public function getClientEntity($clientIdentifier)
  23. {
  24. if (!($clientModel = Yii::$app->services->oauth2Client->findByClientId($clientIdentifier))) {
  25. throw new UnprocessableEntityHttpException('找不到 Client Id');
  26. }
  27. // 返回客户端信息
  28. $client = new ClientEntity();
  29. $client->setIdentifier($clientIdentifier);
  30. $client->setName($clientModel['title']);
  31. // 校验回调域名
  32. if (!($redirect_uri = Yii::$app->request->get('redirect_uri'))) {
  33. $redirect_uri = Yii::$app->request->post('redirect_uri');
  34. }
  35. if (!$redirect_uri) {
  36. $client->setRedirectUri($clientModel['redirect_uri']);
  37. } else {
  38. $client->setRedirectUri($redirect_uri);
  39. }
  40. // $client->setGrantType($grantType);
  41. return $client;
  42. }
  43. /**
  44. * Validate a client's secret.
  45. *
  46. * @param string $clientIdentifier 客户端唯一标识符
  47. * @param null|string $clientSecret 代表客户端密钥,是客户端事先在授权服务器中注册时得到的
  48. * @param null|string $grantType 代表授权类型,根据类型不同,验证方式也不同
  49. *
  50. * @return bool
  51. */
  52. public function validateClient($clientIdentifier, $clientSecret, $grantType)
  53. {
  54. if (!($clientModel = Yii::$app->services->oauth2Client->findByClientId($clientIdentifier))) {
  55. throw new UnprocessableEntityHttpException('找不到 Client Id');
  56. }
  57. if ($clientModel['client_secret'] !== $clientSecret) {
  58. throw new UnprocessableEntityHttpException('Client Secret 错误');
  59. }
  60. return true;
  61. }
  62. }
粤ICP备19079148号