Skip to content

Commit

Permalink
Add refreshToken method
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinelame committed Nov 24, 2023
1 parent 7cec8ba commit 9b6c7a7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Two/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ protected function getTokenFields($code)
return $fields;
}

/**
* Refresh a user access token with a refresh token.
*
* @param string $refreshToken
* @return Token
*/
public function refreshToken($refreshToken)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::FORM_PARAMS => [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
]
]);

$response = json_decode($response->getBody(), true);

return (new Token)
->setToken(Arr::get($response, 'access_token'))
->setRefreshToken(Arr::get($response, 'refresh_token'))
->setExpiresIn(Arr::get($response, 'expires_in'))
->setApprovedScopes(explode($this->scopeSeparator, Arr::get($response, 'scope', '')));
}

/**
* Get the code from the request.
*
Expand Down
86 changes: 86 additions & 0 deletions src/Two/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Laravel\Socialite\Two;

class Token
{
/**
* The user's access token.
*
* @var string
*/
public $token;

/**
* The refresh token that can be exchanged for a new access token.
*
* @var string
*/
public $refreshToken;

/**
* The number of seconds the access token is valid for.
*
* @var int
*/
public $expiresIn;

/**
* The scopes the user authorized. The approved scopes may be a subset of the requested scopes.
*
* @var array
*/
public $approvedScopes;

/**
* Set the token on the user.
*
* @param string $token
* @return $this
*/
public function setToken($token)
{
$this->token = $token;

return $this;
}

/**
* Set the refresh token required to obtain a new access token.
*
* @param string $refreshToken
* @return $this
*/
public function setRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;

return $this;
}

/**
* Set the number of seconds the access token is valid for.
*
* @param int $expiresIn
* @return $this
*/
public function setExpiresIn($expiresIn)
{
$this->expiresIn = $expiresIn;

return $this;
}

/**
* Set the scopes that were approved by the user during authentication.
*
* @param array $approvedScopes
* @return $this
*/
public function setApprovedScopes($approvedScopes)
{
$this->approvedScopes = $approvedScopes;

return $this;
}
}
20 changes: 20 additions & 0 deletions tests/OAuthTwoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laravel\Socialite\Tests\Fixtures\OAuthTwoTestProviderStub;
use Laravel\Socialite\Tests\Fixtures\OAuthTwoWithPKCETestProviderStub;
use Laravel\Socialite\Two\InvalidStateException;
use Laravel\Socialite\Two\Token;
use Laravel\Socialite\Two\User;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -178,4 +179,23 @@ public function testExceptionIsThrownIfStateIsNotSet()
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
$provider->user();
}

public function testUserRefreshesToken()
{
$request = Request::create('/');
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
$provider->http = m::mock(stdClass::class);
$provider->http->expects('post')->with('http://token.url', [
'headers' => ['Accept' => 'application/json'],
'form_params' => ['grant_type' => 'refresh_token', 'client_id' => 'client_id', 'client_secret' => 'client_secret', 'refresh_token' => 'refresh_token'],
])->andReturns($response = m::mock(stdClass::class));
$response->expects('getBody')->andReturns('{ "access_token" : "access_token", "refresh_token" : "refresh_token", "expires_in" : 3600, "scope" : "scope1,scope2" }');
$tokenResponse = $provider->refreshToken('refresh_token');

$this->assertInstanceOf(Token::class, $tokenResponse);
$this->assertSame('access_token', $tokenResponse->token);
$this->assertSame('refresh_token', $tokenResponse->refreshToken);
$this->assertSame(3600, $tokenResponse->expiresIn);
$this->assertSame(['scope1', 'scope2'], $tokenResponse->approvedScopes);
}
}

0 comments on commit 9b6c7a7

Please sign in to comment.