Skip to content

Commit

Permalink
Merge pull request #10 from bhanwarpsrathore/pradeep/refresh-function…
Browse files Browse the repository at this point in the history
…ality

Refresh token functionality updated
  • Loading branch information
dsingh-dev authored Feb 16, 2024
2 parents 2a3334a + 05f15fa commit d8b5cdd
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/LabelcampAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ protected function apiRequest(

return $this->request->api($method, $uri, $options);
} catch (LabelcampAPIException $e) {
if ($this->options['auto_refresh'] && $e->hasExpiredToken()) {
$result = $this->session->refreshAccessToken();
if ($this->options['auto_refresh'] && $e->hasExpiredToken() && $this->session) {
$result = $this->session->requestAccessToken();

if (!$result) {
throw new LabelcampAPIException('Could not refresh access token.');
Expand Down
52 changes: 0 additions & 52 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class Session {

protected string $accessToken = '';
protected string $refreshToken = '';
protected string $username = '';
protected string $password = '';
protected int $expirationTime = 0;
Expand Down Expand Up @@ -53,15 +52,6 @@ public function getAccessToken(): string {
return $this->accessToken;
}

/**
* Get Refresh Token
*
* @return string The refresh token.
*/
public function getRefreshToken(): string {
return $this->refreshToken;
}

/**
* Get the access token expiration time.
*
Expand Down Expand Up @@ -89,7 +79,6 @@ public function requestAccessToken(): bool {

if (isset($response['access_token'])) {
$this->accessToken = $response["access_token"];
$this->refreshToken = isset($response["refresh_token"]) ? $response["refresh_token"] : '';
$this->expirationTime = time() + $response["expires_in"];

return true;
Expand All @@ -98,34 +87,6 @@ public function requestAccessToken(): bool {
return false;
}

/**
* Refresh Access Token
*
* @return boolean True if the token was refreshed.
*/
public function refreshAccessToken(?string $refreshToken = null): bool {
$parameters = [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken ?? $this->refreshToken
];

$response = $this->request->token('POST', '/token', ["form_params" => $parameters]);

if (isset($response["access_token"])) {
$this->accessToken = $response['access_token'];
$this->expirationTime = time() + $response['expires_in'];
$this->refreshToken = $response['refresh_token'];

if (empty($this->refreshToken)) {
$this->refreshToken = $refreshToken;
}

return true;
}

return false;
}

/**
* Set the username
*
Expand All @@ -149,17 +110,4 @@ public function setPassword(string $password): self {

return $this;
}

/**
* Set refresh token in session.
*
* @param string $refreshToken The refresh token.
*
* @return self
*/
public function setRefreshToken(string $refreshToken): self {
$this->refreshToken = $refreshToken;

return $this;
}
}
3 changes: 1 addition & 2 deletions tests/Unit/LabelcampAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

it('can be initiated with options', function () {
$api = new LabelcampAPI([
'auto_refresh' => true,
'auto_retry' => true
'auto_refresh' => true
]);

expect($api)->toBeInstanceOf(LabelcampAPI::class);
Expand Down

0 comments on commit d8b5cdd

Please sign in to comment.