Replace current token after successful authentication
@param mixed  $credential
@param string $token
@param string $persistentToken
@param int    $expire
779086
public function replaceTriplet($credential, $token, $persistentToken, $expire = 0)
    {
        try {
            $this->connection->beginTransaction();
            $this->cleanTriplet($credential, $persistentToken);
            $this->storeTriplet($credential, $token, $persistentToken, $expire);
            $this->connection->commit();
        } catch (\PDOException $e) {
            $this->connection->rollBack();
            throw $e;
        }
    }
1110813
public function setCredential($credential)
    {
        $credential = (string) $credential;
        if ($credential === '') {
            throw new \InvalidArgumentException('$credential cannot be empty');
        }

        $this->credential = $credential;

        return $this;
    }
528435
public function updateRememberToken(AuthenticatableContract $user, $token) {
	    if(!empty($user)) {
	    	// make sure there is a remember_token field available for
	    	// updating before trying to update; otherwise we run into
	    	// an uncatchable exception
	    	if($user->canHaveRememberToken()) {
	    		$user->remember_token = $token;
	    		$user->save();
	    	}
	    }
    }
852558
public function setToken($token, $tokenSecret)
    {
        $this->token = $token;
        $this->tokenSecret = $tokenSecret;

        return $this;
    }
501826
public function setAccessTokenCredentials($token, $tokenSecret)
    {
        $this->credentials->setToken($token);
        $this->credentials->setTokenSecret($tokenSecret);
        $this->initOauthRequestClient();
    }
459835
public function authenticate(Credential $credential)
    {
        if (!$credential->username || !$credential->password) {
            $this->status = Authentication::EMPTY_CREDENTIAL;

            return false;
        }

        $datamapper = new DataMapper('users');

        $user = $datamapper->findOne(['username' => $credential->username]);

        if ($user->isNull()) {
            $this->status = Authentication::USER_NOT_FOUND;

            return false;
        }

        if (!Hasher::verify($credential->password, $user->password)) {
            $this->status = Authentication::INVALID_CREDENTIAL;

            return false;
        }

        $credential->bind($user);

        $this->status = Authentication::SUCCESS;

        return true;
    }
1200037
public function update_credential($id, $recipient_name = null, $recipient_email = null, $course_id = null, $issued_on = null, $expired_on = null, $custom_attributes = null){

		$data = array(
		    "credential" => array(
		    	"group_id" => $course_id,
		        "recipient" => array(
		            "name" => $recipient_name,
		            "email" => $recipient_email
		        ),
		        "issued_on" => $issued_on,
		        "expired_on" => $expired_on,
		        "custom_attributes" => $custom_attributes
		    )
		);
		$data = $this->strip_empty_keys($data);

		$client = new \GuzzleHttp\Client();

        $params = array('Authorization' => 'Token token="'.$this->getAPIKey().'"');

		$response = $client->put($this->api_endpoint.'credentials/'.$id, array(
		    'headers' =>  $params,
		    'json' => $data
		));

		$result = json_decode($response->getBody());

		return $result;
	}
1243141
public function updateRememberToken(UserContract $user, $token)
    {
        $this->query->filterById($user->getAuthIdentifier())
            ->update(['RememberToken' => $token]);
    }
430500
def refresh_oauth_credential(self):
        """"""
        credential = self.session.oauth2credential

        if credential.is_stale():
            refresh_session = refresh_access_token(credential)
            self.session = refresh_session
1321012
protected function refreshToken(TokenInterface $expiredToken, ResourceOwnerInterface $resourceOwner)
    {
        if (!$expiredToken->getRefreshToken()) {
            return $expiredToken;
        }

        $token = new OAuthToken($resourceOwner->refreshAccessToken($expiredToken->getRefreshToken()));
        $token->setRefreshToken($expiredToken->getRefreshToken());
        $this->tokenStorage->setToken($token);

        return $token;
    }
815140
public function updateRememberToken( Account $acct, $token )
	{
		$this->conn->table( $this->table )->where( 'id', $acct->getId() )->update( ['remember_token' => $token] );
	}
248540
public function updateRememberToken(Authenticatable $user, $token)
    {
        $user->setRememberToken($token);
        $this->em->persist($user);
        $this->em->flush($user);
    }
47509
public function create($token, $expireTime, $sessionId, $redirectUri)
    {
        $this->getDbConnection()->insert('oauth_auth_code', [
            'auth_code' => $token,
            'client_redirect_uri' => $redirectUri,
            'session_id' => $sessionId,
            'expire_time' => $expireTime,
        ]);
    }
1067476
protected function authenticate_by_token($tokentype){
        global $DB;

        $loginfaileddefaultparams = array(
            'other' => array(
                'method' => $this->authmethod,
                'reason' => null
            )
        );

        if (!$token = $DB->get_record('external_tokens', array('token'=>$this->token, 'tokentype'=>$tokentype))) {
            // Log failed login attempts.
            $params = $loginfaileddefaultparams;
            $params['other']['reason'] = 'invalid_token';
            $event = \core\event\webservice_login_failed::create($params);
            $event->set_legacy_logdata(array(SITEID, 'webservice', get_string('tokenauthlog', 'webservice'), '' ,
                get_string('failedtolog', 'webservice').": ".$this->token. " - ".getremoteaddr() , 0));
            $event->trigger();
            throw new moodle_exception('invalidtoken', 'webservice');
        }

        if ($token->validuntil and $token->validuntil < time()) {
            $DB->delete_records('external_tokens', array('token'=>$this->token, 'tokentype'=>$tokentype));
            throw new webservice_access_exception('Invalid token - token expired - check validuntil time for the token');
        }

        if ($token->sid){//assumes that if sid is set then there must be a valid associated session no matter the token type
            if (!\core\session\manager::session_exists($token->sid)){
                $DB->delete_records('external_tokens', array('sid'=>$token->sid));
                throw new webservice_access_exception('Invalid session based token - session not found or expired');
            }
        }

        if ($token->iprestriction and !address_in_subnet(getremoteaddr(), $token->iprestriction)) {
            $params = $loginfaileddefaultparams;
            $params['other']['reason'] = 'ip_restricted';
            $params['other']['tokenid'] = $token->id;
            $event = \core\event\webservice_login_failed::create($params);
            $event->add_record_snapshot('external_tokens', $token);
            $event->set_legacy_logdata(array(SITEID, 'webservice', get_string('tokenauthlog', 'webservice'), '' ,
                get_string('failedtolog', 'webservice').": ".getremoteaddr() , 0));
            $event->trigger();
            throw new webservice_access_exception('Invalid service - IP:' . getremoteaddr()
                    . ' is not supported - check this allowed user');
        }

        $this->restricted_context = context::instance_by_id($token->contextid);
        $this->restricted_serviceid = $token->externalserviceid;

        $user = $DB->get_record('user', array('id'=>$token->userid), '*', MUST_EXIST);

        // log token access
        webservice::update_token_lastaccess($token);

        return $user;

    }
1333971
private void refreshAccessToken() {
    final Long expiresIn = credential.getExpiresInSeconds();

    // trigger refresh if token is about to expire
    String accessToken = credential.getAccessToken();
    if (accessToken == null || expiresIn != null && expiresIn <= 60) {
      try {
        credential.refreshToken();
        accessToken = credential.getAccessToken();
      } catch (final IOException e) {
        log.error("Failed to fetch access token", e);
      }
    }
    if (accessToken != null) {
      this.accessToken = accessToken;
    }
  }
980987
public function authenticateWithToken(TokenInterface $token, Request $request, string $providerKey = null)
    {
        $this->migrateSession($request, $token, $providerKey);
        $this->tokenStorage->setToken($token);

        if (null !== $this->dispatcher) {
            $loginEvent = new InteractiveLoginEvent($request, $token);
            $this->dispatcher->dispatch($loginEvent, SecurityEvents::INTERACTIVE_LOGIN);
        }
    }
909259
public function invalidate(Token $token)
    {
        if (! $this->blacklistEnabled) {
            throw new JWTException('You must have the blacklist enabled to invalidate a token.');
        }

        return $this->blacklist->add($this->decode($token));
    }