Skip to content

Commit

Permalink
Fixed unsetting access token stored inside session on error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber committed Sep 1, 2024
1 parent 2e3ff4b commit 3233bd6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.1

* fixed unsetting access token stored inside session on error

## 2.0.0

* added funnel endpoints
Expand Down
10 changes: 7 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Client {
'utm_source'
);

const SESSION_TOKEN_KEY = 'pirsch_access_token';

private $clientID;
private $clientSecret;
private $client;
Expand Down Expand Up @@ -440,6 +442,8 @@ private function performGet($url, Filter $filter, $retry = true) {

private function refreshToken() {
try {
unset($_SESSION[self::SESSION_TOKEN_KEY]);

if (empty($this->clientID)) {
throw new \Exception('Single access tokens cannot be refreshed');
}
Expand All @@ -460,7 +464,7 @@ private function refreshToken() {
}

$resp = json_decode($response->getBody());
$_SESSION['pirsch_access_token'] = $resp->access_token;
$_SESSION[self::SESSION_TOKEN_KEY] = $resp->access_token;
} catch(\GuzzleHttp\Exception\RequestException $e) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error refreshing token '.!is_null($e->getResponse()) && $e->getResponse()->getStatusCode().': '.$e->getResponse()->getBody());
Expand All @@ -478,8 +482,8 @@ private function getRequestHeader() {
private function getAccessToken() {
if (empty($this->clientID)) {
return $this->clientSecret;
} else if (isset($_SESSION['pirsch_access_token'])) {
return $_SESSION['pirsch_access_token'];
} else if (isset($_SESSION[self::SESSION_TOKEN_KEY])) {
return $_SESSION[self::SESSION_TOKEN_KEY];
}

return '';
Expand Down

0 comments on commit 3233bd6

Please sign in to comment.