-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[4.x] Automatically refresh expired tokens (#248)
- Loading branch information
Showing
32 changed files
with
948 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Concerns; | ||
|
||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\RequestOptions; | ||
use Illuminate\Support\Arr; | ||
use JoelButcher\Socialstream\ConnectedAccount; | ||
use JoelButcher\Socialstream\RefreshedCredentials; | ||
use Laravel\Socialite\Two\AbstractProvider; | ||
|
||
/** | ||
* @mixin AbstractProvider&RefreshesOauth2Tokens | ||
*/ | ||
trait RefreshesOauth2Tokens | ||
{ | ||
/** | ||
* Refresh the token for the current provider. | ||
* | ||
* @throws GuzzleException | ||
*/ | ||
public function refreshToken(ConnectedAccount $connectedAccount): RefreshedCredentials | ||
{ | ||
if (is_null($connectedAccount->refresh_token)) { | ||
throw new \RuntimeException('A valid refresh token is required.'); | ||
} | ||
|
||
$response = $this->getHttpClient()->post($this->getTokenUrl(), [ | ||
RequestOptions::HEADERS => $this->getRefreshTokenHeaders(), | ||
RequestOptions::FORM_PARAMS => $this->getRefreshTokenFields($connectedAccount->refresh_token), | ||
]); | ||
|
||
$response = json_decode($response->getBody(), true); | ||
|
||
return new RefreshedCredentials( | ||
token: Arr::get($response, 'access_token'), | ||
refreshToken: Arr::get($response, 'refresh_token'), | ||
expiry: now()->addSeconds(Arr::get($response, 'expires_in')), | ||
); | ||
} | ||
|
||
/** | ||
* Get the headers for the refresh token request. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
protected function getRefreshTokenHeaders(): array | ||
{ | ||
return ['Accept' => 'application/json']; | ||
} | ||
|
||
/** | ||
* Get the POST fields for the refresh token request. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
protected function getRefreshTokenFields(string $refreshToken): array | ||
{ | ||
return [ | ||
'grant_type' => 'refresh_token', | ||
'client_id' => $this->clientId, | ||
'client_secret' => $this->clientSecret, | ||
'refresh_token' => $refreshToken, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Contracts; | ||
|
||
use JoelButcher\Socialstream\ConnectedAccount; | ||
use JoelButcher\Socialstream\RefreshedCredentials; | ||
|
||
interface Oauth2RefreshResolver | ||
{ | ||
/** | ||
* Refreshes the token for the current provider. | ||
*/ | ||
public function refreshToken(ConnectedAccount $connectedAccount): RefreshedCredentials; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace JoelButcher\Socialstream\Contracts; | ||
|
||
use DateTimeInterface; | ||
|
||
interface RefreshedCredentials | ||
{ | ||
/** | ||
* Get token for the credentials. | ||
* | ||
* @return string | ||
*/ | ||
public function getToken(); | ||
|
||
/** | ||
* Get the token secret for the credentials. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getTokenSecret(); | ||
|
||
/** | ||
* Get the refresh token for the credentials. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getRefreshToken(); | ||
|
||
/** | ||
* Get the expiry date for the credentials. | ||
* | ||
* @return DateTimeInterface|null | ||
*/ | ||
public function getExpiry(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.