auth_extension_service = $auth_extension_service; $this->user_service = $user_service; $this->checkpoint_service = $checkpoint_service; $this->user_repository = $user_repository; $this->tx_service = $tx_service; } /** * Retrieve a user by their unique identifier. * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { try { $user = $this->user_repository->getById($identifier); if (!is_null($user)) { return $user; } } catch (Exception $ex) { Log::warning($ex); return null; } return null; } /** * Retrieve a user by the given credentials. * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { return $this->tx_service->transaction(function () use ($credentials) { $user = null; try { if (!isset($credentials['username']) || !isset($credentials['password'])) { throw new AuthenticationException("invalid crendentials"); } $email = $credentials['username']; $password = $credentials['password']; $user = $this->user_repository->getByEmailOrName(trim($email)); if (is_null($user)) //user must exists { throw new AuthenticationException(sprintf("User %s does not exists.", $email)); } if(!$user->canLogin()) { if(!$user->isEmailVerified()) throw new UnverifiedEmailMemberException(sprintf("User %s is not yet verified; check your email and click on the confirmation link before trying to log in again.", $email)); throw new AuthenticationException(sprintf("User %s does not exists.", $email)); } $valid_password = $user->checkPassword($password); if (!$valid_password) { throw new AuthenticationInvalidPasswordAttemptException($user->getId(), sprintf("invalid login attempt for user %s ", $email)); } //check user status... if (!$user->isActive()) { Log::warning(sprintf("user %s is on lock state", $email)); throw new AuthenticationLockedUserLoginAttempt($email, sprintf("User %s is locked.", $email)); } //update user fields $user->setLastLoginDate(new \DateTime('now', new \DateTimeZone('UTC'))); $user->setLoginFailedAttempt(0); $user->activate(); $user->clearResetPasswordRequests(); $auth_extensions = $this->auth_extension_service->getExtensions(); foreach ($auth_extensions as $auth_extension) { if(!$auth_extension instanceof IAuthenticationExtension) continue; $auth_extension->process($user); } } catch(UnverifiedEmailMemberException $ex){ $this->checkpoint_service->trackException($ex); Log::warning($ex); throw $ex; } catch (Exception $ex) { $this->checkpoint_service->trackException($ex); Log::warning($ex); $user = null; } return $user; }); } /** * @param Authenticatable $user * @param array $credentials * @return bool * @throws AuthenticationException */ public function validateCredentials(Authenticatable $user, array $credentials) { if (!isset($credentials['username']) || !isset($credentials['password'])) { throw new AuthenticationException("invalid crendentials"); } try { $email = $credentials['username']; $password = $credentials['password']; $user = $this->user_repository->getByEmailOrName(trim($email)); if (!$user || !$user->canLogin() || !$user->checkPassword($password)) { return false; } if (is_null($user) || !$user->isActive()) { return false; } } catch (Exception $ex) { Log::warning($ex); return false; } return true; } /** * Retrieve a user by by their unique identifier and "remember me" token. * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { return $this->user_repository->getByToken($token); } /** * @param Authenticatable $user * @param string $token * @throws Exception */ public function updateRememberToken(Authenticatable $user, $token) { $this->tx_service->transaction(function () use ($user, $token) { $dbUser = $this->user_repository->getById($user->getAuthIdentifier()); if(is_null($dbUser)) return; $dbUser->setRememberToken($user->getRememberToken()); }); } }