Skip to content

Commit

Permalink
Merge pull request #7 from ssofy/2.x-dev
Browse files Browse the repository at this point in the history
2.x dev
  • Loading branch information
ssofy authored Aug 5, 2023
2 parents 6f60d77 + c8310cb commit 2ad48c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.1.1 - 2023-08-05

* Added OAuth2Client::renewAccessToken().
* Renamed: OAuth2Client::deleteState() -> OAuth2Client::destroy().

## 2.1.0 - 2023-07-30

* Config parameter renames.
Expand Down
11 changes: 11 additions & 0 deletions src/SSOfy/Exceptions/RefreshTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SSOfy\Exceptions;

class RefreshTokenException extends Exception
{
public function __construct()
{
parent::__construct('Token is not renewable');
}
}
36 changes: 34 additions & 2 deletions src/SSOfy/OAuth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use League\OAuth2\Client\Token\AccessTokenInterface;
use SSOfy\Exceptions\AuthErrorException;
use SSOfy\Exceptions\InvalidStateException;
use SSOfy\Exceptions\RefreshTokenException;
use SSOfy\Storage\NullStorage;
use SSOfy\Storage\StorageInterface;

Expand Down Expand Up @@ -171,6 +172,8 @@ public function refreshUserInfo($state)
* @param string $state
* @return AccessTokenInterface
* @throws InvalidStateException
* @throws RefreshTokenException
* @throws AuthErrorException
*/
public function getAccessToken($state)
{
Expand All @@ -191,6 +194,35 @@ public function getAccessToken($state)
return $accessToken;
}

if (is_null($accessToken->getRefreshToken())) {
return null;
}

return $this->renewAccessToken($state);
}

/**
* @param $state
* @return AccessTokenInterface
* @throws InvalidStateException
* @throws RefreshTokenException
* @throws AuthErrorException
*/
public function renewAccessToken($state)
{
$stateData = $this->getState($state);

if (is_null($stateData)) {
throw new InvalidStateException();
}

/** @var AccessTokenInterface $accessToken */
$accessToken = $stateData['token'];

if (is_null($accessToken->getRefreshToken())) {
throw new RefreshTokenException();
}

$config = new OAuth2Config($stateData['config']);

$provider = new GenericProvider($this->buildLeagueConfig($config));
Expand All @@ -204,7 +236,7 @@ public function getAccessToken($state)
'refresh_token' => $accessToken->getRefreshToken()
]);
} catch (IdentityProviderException $exception) {
return null;
throw new AuthErrorException($exception->getMessage());
}

$stateData['token'] = $accessToken;
Expand Down Expand Up @@ -272,7 +304,7 @@ private function saveState($state, $data, $timeout = 0)
* @param string $state
* @return void
*/
public function deleteState($state)
public function destroy($state)
{
$this->stateStore->delete($this->stateStorageKey($state));
}
Expand Down

0 comments on commit 2ad48c8

Please sign in to comment.