MicroUserProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Services\Auth;
  3. use App\MicroApi\Exceptions\RpcException;
  4. use App\MicroApi\Items\UserItem;
  5. use App\MicroApi\Services\UserService;
  6. use Firebase\JWT\JWT;
  7. use Illuminate\Auth\AuthenticationException;
  8. use Illuminate\Contracts\Auth\Authenticatable;
  9. use Illuminate\Contracts\Auth\UserProvider;
  10. class MicroUserProvider implements UserProvider
  11. {
  12. /**
  13. * @var UserService
  14. */
  15. protected $userService;
  16. /**
  17. * The auth user model.
  18. *
  19. * @var string
  20. */
  21. protected $model;
  22. /**
  23. * Create a new auth user provider.
  24. *
  25. * @param string $model
  26. * @return void
  27. */
  28. public function __construct($model)
  29. {
  30. $this->model = $model;
  31. $this->userService = resolve('microUserService');
  32. }
  33. /**
  34. * Retrieve a user by their unique identifier.
  35. *
  36. * @param mixed $identifier
  37. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  38. * @throws RpcException
  39. */
  40. public function retrieveById($identifier)
  41. {
  42. $user = $this->userService->getById($identifier);
  43. if ($user) {
  44. $model = $this->createModel();
  45. $model->fillAttributes($user);
  46. } else {
  47. $model = null;
  48. }
  49. return $model;
  50. }
  51. /**
  52. * Retrieve a user by their unique identifier and "remember me" token.
  53. *
  54. * @param mixed $identifier
  55. * @param string $token
  56. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  57. */
  58. public function retrieveByToken($identifier, $token)
  59. {
  60. $model = $this->createModel();
  61. $data = JWT::decode($token, config('services.micro.jwt_key'), [config('services.micro.jwt_algorithms')]);
  62. if ($data->exp <= time()) {
  63. return null; // Token 过期
  64. }
  65. $model->fillAttributes($data->User);
  66. return $model;
  67. }
  68. /**
  69. * Update the "remember me" token for the given user in storage.
  70. *
  71. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  72. * @param string $token
  73. * @return void
  74. */
  75. public function updateRememberToken(Authenticatable $user, $token)
  76. {
  77. // TODO: Implement updateRememberToken() method.
  78. }
  79. /**
  80. * Retrieve a user by the given credentials.
  81. *
  82. * @param array $credentials
  83. * @return string
  84. */
  85. public function retrieveByCredentials(array $credentials)
  86. {
  87. if (empty($credentials) ||
  88. (count($credentials) === 1 &&
  89. array_key_exists('password', $credentials))) {
  90. return;
  91. }
  92. try {
  93. $token = $this->userService->auth($credentials);
  94. } catch (RpcException $exception) {
  95. throw new AuthenticationException("认证失败:邮箱和密码不匹配");
  96. }
  97. return $token;
  98. }
  99. /**
  100. * Validate a user against the given credentials.
  101. *
  102. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  103. * @param array $credentials
  104. * @return bool
  105. */
  106. public function validateCredentials(Authenticatable $user, array $credentials)
  107. {
  108. if (empty($credentials['token'])) {
  109. return false;
  110. }
  111. try {
  112. $valid = $this->userService->isAuth($credentials['token']);
  113. } catch (RpcException $exception) {
  114. throw new AuthenticationException("认证失败:令牌失效,请重新认证");
  115. }
  116. return $valid;
  117. }
  118. /**
  119. * Create a new instance of the model.
  120. *
  121. * @return UserItem
  122. */
  123. public function createModel()
  124. {
  125. $class = '\\'.ltrim($this->model, '\\');
  126. return new $class;
  127. }
  128. /**
  129. * Gets the name of the Eloquent user model.
  130. *
  131. * @return string
  132. */
  133. public function getModel()
  134. {
  135. return $this->model;
  136. }
  137. /**
  138. * Sets the name of the Eloquent user model.
  139. *
  140. * @param string $model
  141. * @return $this
  142. */
  143. public function setModel($model)
  144. {
  145. $this->model = $model;
  146. return $this;
  147. }
  148. }