[TwitterBridge] Expire guest token by time (#1606)

* [TwitterBridge] Expire guest token by time

In addition to fetching a new guest token after 100 uses, also expire token after 5 minutes (configurable).
This commit is contained in:
triatic 2020-06-23 14:14:50 +01:00 committed by GitHub
parent 22a01f1093
commit 23c61f5f84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ class TwitterBridge extends BridgeAbstract {
const URI = 'https://twitter.com/'; const URI = 'https://twitter.com/';
const API_URI = 'https://api.twitter.com'; const API_URI = 'https://api.twitter.com';
const GUEST_TOKEN_USES = 100; const GUEST_TOKEN_USES = 100;
const GUEST_TOKEN_EXPIRY = 300; // 5min
const CACHE_TIMEOUT = 300; // 5min const CACHE_TIMEOUT = 300; // 5min
const DESCRIPTION = 'returns tweets'; const DESCRIPTION = 'returns tweets';
const MAINTAINER = 'pmaziere'; const MAINTAINER = 'pmaziere';
@ -350,6 +351,21 @@ EOD;
//This function takes 2 requests, and therefore is cached //This function takes 2 requests, and therefore is cached
private function getApiKey() { private function getApiKey() {
$cacheFac = new CacheFactory();
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
$r_cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
$r_cache->setScope(get_called_class());
$r_cache->setKey(array('refresh'));
$data = $r_cache->loadData();
$refresh = null;
if($data === null) {
$refresh = time();
$r_cache->saveData($refresh);
} else {
$refresh = $data;
}
$cacheFac = new CacheFactory(); $cacheFac = new CacheFactory();
$cacheFac->setWorkingDir(PATH_LIB_CACHES); $cacheFac->setWorkingDir(PATH_LIB_CACHES);
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
@ -382,9 +398,11 @@ EOD;
$guestTokenUses = $gt_cache->loadData(); $guestTokenUses = $gt_cache->loadData();
$guestToken = null; $guestToken = null;
if($guestTokenUses === null || !is_array($guestTokenUses) || count($guestTokenUses) != 2 || $guestTokenUses[0] <= 0) { if($guestTokenUses === null || !is_array($guestTokenUses) || count($guestTokenUses) != 2
|| $guestTokenUses[0] <= 0 || (time() - $refresh) > self::GUEST_TOKEN_EXPIRY) {
$guestToken = $this->getGuestToken(); $guestToken = $this->getGuestToken();
$gt_cache->saveData(array(self::GUEST_TOKEN_USES, $guestToken)); $gt_cache->saveData(array(self::GUEST_TOKEN_USES, $guestToken));
$r_cache->saveData(time());
} else { } else {
$guestTokenUses[0] -= 1; $guestTokenUses[0] -= 1;
$gt_cache->saveData($guestTokenUses); $gt_cache->saveData($guestTokenUses);