[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
1 changed files with 19 additions and 1 deletions

View File

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