MicroUserProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 UserItem|Authenticatable|null
  84. * @throws AuthenticationException
  85. */
  86. public function retrieveByCredentials(array $credentials)
  87. {
  88. if (empty($credentials) ||empty($credentials['email']) ||
  89. (count($credentials) === 1 &&
  90. array_key_exists('password', $credentials))) {
  91. return null;
  92. }
  93. try {
  94. $user = $this->userService->getByEmail($credentials['email']);
  95. } catch (RpcException $exception) {
  96. throw new AuthenticationException("认证失败:对应邮箱尚未注册");
  97. }
  98. $model = $this->createModel();
  99. $model->fillAttributes($user);
  100. return $model;
  101. }
  102. /**
  103. * Validate a user against the given credentials.
  104. *
  105. * @param Authenticatable $user
  106. * @param array $credentials
  107. * @return bool
  108. * @throws AuthenticationException
  109. */
  110. public function validateCredentials(Authenticatable $user, array $credentials)
  111. {
  112. try {
  113. if (empty($credentials['jwt_token'])) {
  114. $token = $this->userService->auth($credentials);
  115. } else {
  116. $token = $this->userService->isAuth($credentials['jwt_token']);
  117. }
  118. } catch (RpcException $exception) {
  119. $message = empty($credentials['jwt_token']) ? '注册邮箱与密码不匹配' : '令牌失效';
  120. throw new AuthenticationException("认证失败:" . $message);
  121. }
  122. return $token;
  123. }
  124. /**
  125. * Create a new instance of the model.
  126. *
  127. * @return UserItem
  128. */
  129. public function createModel()
  130. {
  131. $class = '\\'.ltrim($this->model, '\\');
  132. return new $class;
  133. }
  134. /**
  135. * Gets the name of the Eloquent user model.
  136. *
  137. * @return string
  138. */
  139. public function getModel()
  140. {
  141. return $this->model;
  142. }
  143. /**
  144. * Sets the name of the Eloquent user model.
  145. *
  146. * @param string $model
  147. * @return $this
  148. */
  149. public function setModel($model)
  150. {
  151. $this->model = $model;
  152. return $this;
  153. }
  154. }