Skip to content

Commit

Permalink
Fix session key overlapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekz committed Sep 25, 2017
1 parent b985c36 commit 51c50dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class API {
*/
private $session;

/**
* Key used to cache the session.
* @var string
*/
private $cacheKey = "reflex:paladins:session";

/**
* Getter method for Dev Id
* @return int
Expand Down Expand Up @@ -113,6 +119,13 @@ public function getCache() {
return $this->cache;
}

/**
* @return string
*/
public function getCacheKey() {
return $this->cacheKey;
}

/**
* @return Session
*/
Expand Down Expand Up @@ -222,7 +235,7 @@ public function request($method) {

// check validity of session and create if needed
if ($request->requiresSession() && (!$this->session || $this->session->isExpired())) {
$this->session = new Session($this);
$this->session = new Session($this, $this->getCacheKey());
}

// get all extra args
Expand Down
11 changes: 5 additions & 6 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

class Session {
/**
* Change this to use a different key scheme for caching
* @var string
*/
public static $cachingKey = 'curse:paladins:session';
public $cacheKey;

/**
* Timestamp when session was created
Expand All @@ -28,9 +27,9 @@ class Session {
/**
* @param API $api
*/
function __construct(API $api) {
function __construct(API $api, $cacheKey) {
$this->api = $api;
self::$cachingKey = self::$cachingKey . ':' . md5($api->getPlatform());
$this->cacheKey = $cacheKey . ':' . md5($api->getPlatform());
if (!$this->loadFromCache()) {
$this->createSession();
}
Expand Down Expand Up @@ -67,7 +66,7 @@ private function loadFromCache() {
if (!$this->api->getCache()) {
return false;
}
$data = $this->api->getCache()->fetch(self::$cachingKey);
$data = $this->api->getCache()->fetch($this->cacheKey);
if ($data) {
list($this->sessionKey, $this->sessionTimestamp) = unserialize($data);
return !$this->isExpired();
Expand All @@ -85,7 +84,7 @@ private function saveToCache() {
}
$data = serialize([$this->sessionKey, $this->sessionTimestamp]);
// save for 15 minutes
$this->api->getCache()->save(self::$cachingKey, $data, $this->api->sessionTTL());
$this->api->getCache()->save($this->cacheKey, $data, $this->api->sessionTTL());
}

/**
Expand Down

0 comments on commit 51c50dc

Please sign in to comment.