Use this function only within \Gems_Tracker!!

@return \Gems_Tracker_Token_TokenLibrary
977152
public function getTokenLibrary()
    {
        if (! $this->_tokenLibrary) {
            $this->_tokenLibrary = $this->_loadClass('Token_TokenLibrary', true);
        }

        return $this->_tokenLibrary;
    }
1109431
public function processTokenData(\Gems_Tracker_Token $token)
    {
        if ($token->getReceptionCode()->isSuccess() && $token->isCompleted()) {
            $respTrack = $token->getRespondentTrack();
            $fields    = $respTrack->getCodeFields();
            $answers   = $token->getRawAnswers();
            $newFields = [];

            foreach ($fields as $code => $value) {
                if (isset($answers[$code]) && ($answers[$code] != $value)) {
                    $newFields[$code] = $answers[$code];
                }
            }

            if ($newFields) {
                // \MUtil_Echo::track($answers, $newFields);
                $respTrack->setFieldData($newFields);
            }
        }

        return false;
    }
301133
public function createToken()
    {
        $time = microtime(true);
        $token = $this->generate();

        $store = $this->getStore();

        $pool = $this->invalidate($store->getTokens());

        $pool[] = [
            'ts' => $time,
            'token' => $token
        ];

        $store->setTokens($pool);

        return $token;
    }
1245666
public function afterRegistry()
    {
        parent::afterRegistry();

        if (! $this->tokenLibrary instanceof \Gems_Tracker_Token_TokenLibrary) {
            $this->tokenLibrary = $this->loader->getTracker()->getTokenLibrary();
        }
    }
685738
public function getAndStoreToken()
    {
        $token = $this->generateToken(6);
        
        $this->setAttribute('secure_token', sha1($token));
        $this->setAttribute('secure_token_timestamp', time());
        $this->update(false);

        return $token;
    }
547162
public function token()
    {
        $response = $this->oauthServer->handleTokenRequest(Request::createFromGlobals());
        if (!$response->isSuccessful()) {
            return $this->response->sendError(
                ResponseCodes::AUTH_ERROR,
                $response->getStatusCode(),
                $response->getParameter('error_description')
            );
        } else {
            return $this->response->sendSuccess($response->getParameters());
        }
    }
1081938
public function getToken(){
        if(!$this->token){
            if($this->getHeader('Authorization') != NULL){
                $this->token= $this->getHeader('Authorization');
            }
            if(!$this->token && $this->getParam('token')){
                $this->token= $this->getParam('token');
            }
        }
        return $this->token;
    }
883928
public function getSyncToken()
    {
        if (
            $this->carddavBackend instanceof Backend\SyncSupport &&
            isset($this->addressBookInfo['{DAV:}sync-token'])
        ) {
            return $this->addressBookInfo['{DAV:}sync-token'];
        }
        if (
            $this->carddavBackend instanceof Backend\SyncSupport &&
            isset($this->addressBookInfo['{http://sabredav.org/ns}sync-token'])
        ) {
            return $this->addressBookInfo['{http://sabredav.org/ns}sync-token'];
        }
    }
394425
public function getToken()
    {
        if ($this->csrfToken != '') {
            return $this->csrfToken;
        }

        $this->csrfToken = wp_generate_password(64, false);

        $this->session->set('tiga_csrf_token', $this->csrfToken);

        return $this->csrfToken;
    }
289895
public function processTokenInsertion(\Gems_Tracker_Token $token)
    {
        $this->token = $token;

        if ($token->getReceptionCode()->isSuccess() && (!$token->isCompleted())) {
            // Read questioncodes
            $questions = $token->getSurvey()->getQuestionList(null);
            $fields    = [];
            $results   = [];

            // Check if they match a prefix schema
            foreach ($questions as $code => $text) {
                $upperField = strtoupper($code);
                $prefix     = substr($upperField, 0, $this->prefixLength);
                if (array_key_exists($prefix, $this->prefixes)) {
                    $fields[$prefix][$code] = substr($upperField, $this->prefixLength);
                }
            }

            if (count($fields) > 0) {
                foreach ($fields as $prefix => $requests) {
                    // Find which method to call
                    $method  = $this->prefixes[$prefix];
                    $results = $results + $this->$method($requests);
                }
            }

            return $results;
        }
    }
803823
protected function _exportTrackTokens(\Gems_Tracker_RespondentTrack $track)
    {
        $groupSurveys = $this->_group;
        $token        = $track->getFirstToken();
        $engine       = $track->getTrackEngine();
        $surveys      = array();

        $table = $this->html->table(array('class' => 'browser table'));
        $table->th($this->_('Survey'))
              ->th($this->_('Round'))
              ->th($this->_('Token'))
              ->th($this->_('Status'));
        $this->html->br();

        while ($token) {
            //Should this token be in the list?
            if (!$this->_isTokenInFilter($token)) {
                $token = $token->getNextToken();
                continue;
            }

            $table->tr()->td($token->getSurveyName())
                        ->td(($engine->getTrackType() == 'T' ? $token->getRoundDescription() : $this->_('Single Survey')))
                        ->td(strtoupper($token->getTokenId()))
                        ->td($token->getStatus());

            //Should we display the answers?
            if (!$this->_displayToken($token)) {
                $token = $token->getNextToken();
                continue;
            }

            $showToken = false;
            if (! $groupSurveys) {
                // For single survey tracks or when $groupSurvey === false we show all tokens
                $showToken = true;
            } else {
                // For multi survey tracks and $groupSurveys === true, we show only the first token
                // as the snippet takes care of showing the other tokens
                if (!isset($surveys[$token->getSurveyId()])) {
                    $showToken = true;
                    $surveys[$token->getSurveyId()] = 1;
                }
            }

            if ($showToken) {
                $params = array(
                    'token'          => $token,
                    'tokenId'        => $token->getTokenId(),
                    'showHeaders'    => false,
                    'showButtons'    => false,
                    'showSelected'   => false,
                    'showTakeButton' => false,
                    'grouped'        => $groupSurveys);

                $snippets = $token->getAnswerSnippetNames();

                if (!is_array($snippets)) {
                    $snippets = array($snippets);
                }

                list($snippets, $snippetParams) = \MUtil_Ra::keySplit($snippets);
                $params = $params + $snippetParams;

                $this->html->snippet('Export_SurveyHeaderSnippet', 'token', $token);

                foreach($snippets as $snippet) {
                    $this->html->snippet($snippet, $params);
                }

                $this->html->br();
            }

            $token = $token->getNextToken();
        }
    }
779781
public function token()
    {
        if (empty($this->csrfToken) && !is_null($this->session)) {
            $this->csrfToken = $this->session->token();
        }

        return $this->hidden('_token', $this->csrfToken);
    }
1091458
protected function generateCsrfToken()
    {
        $token = $this->app->getSecurity()->generateRandomString();
        if ($this->enableCsrfCookie) {
            $cookie = $this->createCsrfCookie($token);
            $this->app->getResponse()->getCookies()->add($cookie);
        } else {
            $this->app->getSession()->set($this->csrfParam, $token);
        }

        return $token;
    }
209522
public function getCopiedFrom()
    {
        if (null === $this->_copiedFromTokenId) {
            $this->_copiedFromTokenId = $this->db->fetchOne(
                    "SELECT gtrp_id_token_old FROM gems__token_replacements WHERE gtrp_id_token_new = ?",
                    $this->_tokenId
                    );
        }

        return $this->_copiedFromTokenId;
    }
74964
public function getToken()
    {
        if (!$this->token) {
            $session = new Session('Auth');
            if (!$session->token) {
                $session->token = uniqid();
            }
            $this->token = $session->token;
        }
        return $this->token;
    }
530604
public static function token()
    {
        self::$csrfToken = !empty(self::$csrfToken) ? self::$csrfToken : Session::token();

        return self::hidden('_token', self::$csrfToken);
    }
1039014
public function getCsrfToken()
    {
        if (!$this->csrfToken) {
            $segment = $this->getSegment(self::CSRF_TOKEN);
            $this->csrfToken = new CsrfToken($segment);
        }

        return $this->csrfToken;
    }