Accept a user token

@param string $token
@param int $uid User id
@return Garp_Model_Db Logged in user
178621
public function acceptToken($token, $uid) {
        if (!$token || !$uid) {
            $this->_addError(__('Insufficient data received'));
            return false;
        }
        $authPwlessModel = $this->_getPasswordlessModel();
        $select = $authPwlessModel->select()
            ->where('`token` = ?', $token)
            ->where('user_id = ?', $uid);

        $row = $authPwlessModel->fetchRow($select);
        if (!$row || !$row->Model_User) {
            $this->_addError(__('passwordless token not found'));
            return false;
        }

        // Check wether the user is already logged in. Let's not inconvenience them
        // with security when it's not that important
        if ($this->_userIsAlreadyLoggedIn($row)) {
            return $row->Model_User;
        }

        if (!$this->_tokenIsValid($row)) {
            return false;
        }

        $authPwlessModel->getObserver('Authenticatable')->updateLoginStats(
            $row->Model_User->id, array(
                'claimed' => 1
            )
        );

        return $row->Model_User;
    }
964063
public function loginViaToken($token)
    {
        $user = $this->userDao->getUserByToken($token);
        if ($user) {
            $this->loginWithoutPassword($user->getLogin());
            return true;
        } else {
            return false;
        }
    }
505698
public function userFromTokenAndSecret($token, $secret)
    {
        $tokenCredentials = new TokenCredentials();

        $tokenCredentials->setIdentifier($token);
        $tokenCredentials->setSecret($secret);

        $user = $this->server->getUserDetails(
            $tokenCredentials, $this->shouldBypassCache($token, $secret)
        );

        $instance = (new User)->setRaw($user->extra)
            ->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());

        return $instance->map([
            'id' => $user->uid,
            'nickname' => $user->nickname,
            'name' => $user->name,
            'email' => $user->email,
            'avatar' => $user->imageUrl,
        ]);
    }
841247
public function retrieveByToken($identifier, $token)
    {
        $user = $this->conn->table($this->table)
            ->where('id', $identifier)
            ->where('remember_token', $token)
            ->first();

        if (! is_null($user)) {
            return new GenericUser((array) $user);
        }
    }
852920
public function validateSecureToken($token, $userId)
    {
        $user = User::findOne($userId);
        
        if (!$user) {
            return false;
        }
        
        if ($this->userAttemptBruteForceLockHasExceeded($user)) {
            return false;
        }
        
        if ($user->secure_token == sha1($token) && $user->secure_token_timestamp >= (time() - $this->secureTokenExpirationTime)) {
            return $user;
        }

        return false;
    }
772207
public function sendWelcomeMessage(User $user, Token $token = null)
    {
        $mailVars = [
            '{token}' => $token->url,
            '{legal_link}' => Url::toRoute([
                '/legal/default/view',
                'legalId' => Legal::findOne(['key' => 'buyers'])->id,
                'langId' => Language::getCurrent()->id
            ], true)
        ];

        /**
         * @var $mailTemplate Template
         */
        $mailTemplate = \Yii::$app->get('emailTemplates')
            ->getTemplate('welcome', Language::getCurrent()->id);
        $mailTemplate->parseSubject($mailVars);
        $mailTemplate->parseBody($mailVars);

        return \Yii::$app->shopMailer->compose('mail-body', ['bodyContent' => $mailTemplate->getBody()])
            ->setFrom([\Yii::$app->shopMailer->transport->getUsername() => \Yii::$app->name ?? Url::to(['/'], true)])
            ->setTo($user->email)
            ->setSubject($mailTemplate->getSubject())
            ->send();
    }
1354056
public function retrieveByToken($identifier, $token)
    {
        $userModel = $this->createUserModel();

        return $userModel->newQuery()
            ->where($userModel->getKeyName(), $identifier)
            ->where($userModel->getRememberTokenName(), $token)
            ->first();
    }
154432
public function authenticate(TokenInterface $token)
    {
        if (!$this->supports($token)) {
            return null;
        }

        $user = $token->getUser();

        if (!($user instanceof UserInterface)) {
            return null;
        }

        $user = $this->userProvider->refreshUser($user);
        $this->userChecker->checkPostAuth($user);

        $authenticatedToken = new JoomlaToken(
            $user,
            $user->getPassword(),
            $this->providerKey,
            $user->getRoles()
        );
        $authenticatedToken->setAttributes($token->getAttributes());

        return $authenticatedToken;
    }
1242191
protected function getUserByToken($token)
    {
        return $this->sdkClient()
                    ->useCustomApiEndpoint($this->getEnvironmentEndpoint('api'))
                    ->setAccessToken($token)
                    ->uses('Profile')
                    ->get()
                    ->toArray();
    }
507739
protected function addCurrentUser(TokenInterface $token, array $sids)
    {
        if (!$token instanceof AnonymousToken) {
            try {
                $sids[] = UserSecurityIdentity::fromToken($token);
            } catch (\InvalidArgumentException $e) {
                // ignore, user has no user security identity
            }
        }

        return $sids;
    }
61678
private function refreshUser(TokenInterface $token)
    {
        $user = $token->getUser();
        if (!$user instanceof UserInterface) {
            return $token;
        }

        if (null !== $this->logger) {
            $this->logger->debug(sprintf('Reloading user from user provider.'));
        }

        foreach ($this->userProviders as $provider) {
            try {
                $refreshedUser = $provider->refreshUser($user);
                $token->setUser($refreshedUser);

                if (null !== $this->logger) {
                    $this->logger->debug(sprintf('Username "%s" was reloaded from user provider.', $refreshedUser->getUsername()));
                }

                return $token;
            } catch (UnsupportedUserException $unsupported) {
                // let's try the next user provider
            } catch (UsernameNotFoundException $notFound) {
                if (null !== $this->logger) {
                    $this->logger->warning(sprintf('Username "%s" could not be found.', $notFound->getUsername()));
                }

                return;
            }
        }

        throw new \RuntimeException(sprintf('There is no user provider for user "%s".', get_class($user)));
    }
1086925
public function uid(string $uid): Builder
    {
        $this->claims[Claims::USER_ID] = $uid;

        return $this;
    }
296090
public function verifyToken($token, $userId)
	{
		$user = $this->repository->get($userId);
		$now = new DateTime();

		if ($user) {
			$dateDiff = $now->diff($user->reset_password_requested_at);

			$totalHours = ($dateDiff->d * 24) + $dateDiff->h;

			// remove token older than 2h
			if ($totalHours > 2) {
				$update = [
					'reset_password_requested_at' => NULL,
					'reset_password_token' => NULL,
				];
				$this->repository->update($userId, $update);

				return FALSE;
			}

			if (Passwords::verify($token, $user->reset_password_token)) {
				$update = [
					'reset_password_requested_at' => NULL,
					'reset_password_token' => NULL,
				];
				$this->repository->update($userId, $update);

				return TRUE;
			}
		}

		return FALSE;
	}
1198950
public function createCustomToken($uid, array $claims = [], \DateTimeInterface $expiresAt = null): Token
    {
        if (count($claims)) {
            $this->builder->set('claims', $claims);
        }

        $this->builder->set('uid', (string) $uid);

        $now = time();
        $expiration = $expiresAt ? $expiresAt->getTimestamp() : $now + (60 * 60);

        $token = $this->builder
            ->setIssuedAt($now)
            ->setExpiration($expiration)
            ->sign($this->signer, $this->privateKey)
            ->getToken();

        $this->builder->unsign();

        return $token;
    }
953570
public function getUserIdFromToken(string $token): string
    {
        if (!$this->isTokenValid($token)) {
            throw new \InvalidArgumentException('This token is invalid');
        }
        $tokenData = $this->decryptToken($token);

        return $tokenData['user_id'];
    }
492646
public function setAuthToken($token)
    {
        $this->apiClient
            ->setShopId(null)
            ->setShopPassword(null)
            ->setBearerToken($token);

        return $this;
    }
113785
protected function refreshUser(TokenInterface $token)
    {
        $user = $token->getUser();

        if (!$user instanceof UserInterface) {
            return;
        }

        $refreshedUser = $this->userProvider->refreshUser($user);
        $token->setUser($refreshedUser);

        return $token;
    }