public static function get_login_token() {
        global $CFG, $SESSION;

        $state = false;

        if (!isset($SESSION->logintoken)) {
            $SESSION->logintoken = [];
        }

        if (array_key_exists(self::$logintokenkey, $SESSION->logintoken)) {
            $state = $SESSION->logintoken[self::$logintokenkey];
        }
        if (empty($state)) {
            $state = self::create_login_token();
        }

        // Check token lifespan.
        if ($state['created'] < (time() - $CFG->sessiontimeout)) {
            $state = self::create_login_token();
        }

        // Return the current session login token.
        if (array_key_exists('token', $state)) {
            return $state['token'];
        } else {
            return false;
        }
    }