Repository: hwi/HWIOAuthBundle (php)

Issue:
Expired token does not refresh
Hi,

After the commit below, the expired token is never refreshed. 

https://github.com/hwi/HWIOAuthBundle/commit/07832663737521d932416626fdc08ca3ca34f17c#diff-b6dd8e26135237df333dee640dafed3c7821bbb728e868df0e251cce60084a88

The suggested solution is to check if the token is still valid.

```
if ($token->isAuthenticated() && !$token->isExpired()) {
    return $token;
}

```

Pull request description:
Force token refresh if authenticated token was expired already
Fixes #1711

Patch:
diff --git a/Security/Core/Authentication/Provider/OAuthProvider.php b/Security/Core/Authentication/Provider/OAuthProvider.php
index 6468206..5e2719e 100644
--- a/Security/Core/Authentication/Provider/OAuthProvider.php
+++ b/Security/Core/Authentication/Provider/OAuthProvider.php
@@ -80,7 +80,7 @@ class OAuthProvider implements AuthenticationProviderInterface
 
         // If token is authenticated, re-create it to reload user details
         /** @var OAuthToken $token */
-        if ($token->isAuthenticated()) {
+        if ($token->isAuthenticated() && !$token->isExpired()) {
             /** @var UserInterface $user */
             $user = $token->getUser();
 


Test patch:
diff --git a/Tests/Security/Core/Authentication/Provider/OAuthProviderTest.php b/Tests/Security/Core/Authentication/Provider/OAuthProviderTest.php
index c8d61e2..d367d0d 100644
--- a/Tests/Security/Core/Authentication/Provider/OAuthProviderTest.php
+++ b/Tests/Security/Core/Authentication/Provider/OAuthProviderTest.php
@@ -161,7 +161,10 @@ class OAuthProviderTest extends TestCase
         }
     }
 
-    public function testTokenRefreshesWhenExpired()
+    /**
+     * @dataProvider provideAuthenticationData
+     */
+    public function testTokenRefreshesWhenExpired(bool $authenticated)
     {
         $expiredToken = [
             'access_token' => 'access_token',
@@ -217,6 +220,7 @@ class OAuthProviderTest extends TestCase
         $oauthProvider = new OAuthProvider($userProviderMock, $resourceOwnerMapMock, $userCheckerMock, $tokenStorageMock);
 
         $oauthToken = new OAuthToken($refreshedToken);
+        $oauthToken->setAuthenticated($authenticated);
         $oauthToken->setResourceOwnerName('github');
         $oauthToken->setRefreshToken($expiredToken['refresh_token']);
         $oauthToken->setExpiresIn(30);
@@ -240,6 +244,12 @@ class OAuthProviderTest extends TestCase
         $this->assertEquals('ROLE_TEST', $roles[0]);
     }
 
+    public function provideAuthenticationData(): iterable
+    {
+        yield 'authenticated' => [true];
+        yield 'not authenticated' => [false];
+    }
+
     protected function getOAuthAwareUserProviderMock()
     {
         return $this->createMock(OAuthAwareUserProviderInterface::class);