Comments (5)
I'm going to close the issue - but please let me know if you have any questions.
from saloon.
Hey @asamofal
This is not something I originally anticipated when building the client credential support but looking at the RFC 6749 spec it does look like "Issuing a refresh token is optional at the discretion of the
authorization server.".
I would recommend writing a function on your connector to manually refresh this access token and build a separate request for it. I'll also take a note to add this to v4 when I start working on it.
from saloon.
@Sammyjo20
Okay, good to know 👍 Probably shouldn't be too hard in implementation, looks pretty similar to what we already have in AuthorizationCodeGrant
...
from saloon.
You should be able to achieve it by extending the following methods:
createOAuthAuthenticatorFromResponse
createOAuthAuthenticator
and then implement a refresh token request.
from saloon.
This is my workaround for anyone who will face the same issue as me. Will be glad if one of the new Saloon versions supports it out of the box.
class MyConnector extends Connector
{
use ClientCredentialsGrant;
use AcceptsJson;
private function __construct(
private readonly string $clientKey,
) {
$authenticator = $this->getAccessToken();
$this->authenticate($authenticator);
}
public function resolveBaseUrl(): string
{
return '';
}
protected function defaultOauthConfig(): OAuthConfig
{
return OAuthConfig::make()
->setClientId('')
->setClientSecret('')
->setAuthorizeEndpoint('authorize')
->setTokenEndpoint('/oauth/token');
}
/**
* @throws JsonException
*/
protected function createOAuthAuthenticatorFromResponse(Response $response): OAuthAuthenticator
{
$responseData = $response->object();
$accessToken = $responseData->access_token;
$expiresAt = null;
if (isset($responseData->expires_in) && is_numeric($responseData->expires_in)) {
$expiresAt = (new DateTimeImmutable())->add(
DateInterval::createFromDateString((int) $responseData->expires_in . ' seconds')
);
}
$refreshToken = $responseData->refresh_token ?: null;
return new AccessTokenAuthenticator($accessToken, $refreshToken, $expiresAt);
}
/**
* @throws FatalRequestException
* @throws RequestException
* @throws JsonException
*/
public function send(Request $request, MockClient $mockClient = null, callable $handleRetry = null): Response
{
if ($this->authenticator?->hasExpired()) {
$refreshTokenResponse = parent::send(
new GetRefreshTokenRequest(
$this->oauthConfig,
$this->authenticator->getRefreshToken()
)
);
$authenticator = $this->createOAuthAuthenticatorFromResponse($refreshTokenResponse);
$this->authenticate($authenticator);
}
return parent::send($request, $mockClient, $handleRetry);
}
}
@Sammyjo20 I even think I can prepare a PR for this for v3 (this should not break anything) but I'm missing one piece - what calls refreshAccessToken
method from AuthorizationCodeGrant
?
from saloon.
Related Issues (20)
- Cannot Have Request with `headers` HOT 4
- Skipping authentication when request is cached HOT 5
- Skipping AuthenticatePendingRequest when fetching a new accessToken using OAuth2 plugins HOT 3
- OAuth2 - Method not allowed (405) when getting the access Token HOT 8
- Missing methods updates for MockResponse HOT 1
- ERROR There are no commands defined in the "saloon" namespace. HOT 1
- Rate Limit Plugin: Issue with 'allow(1)->everySeconds(1)->sleep()' Exceeding One Request per Second
- Inteliphense cannot recognize the merge() method on request HOT 3
- Connector default headers overwrite request body headers
- Remove authentication on a specific request HOT 1
- Some properties are removed from the response body
- Using non-standard HTTP methods HOT 2
- Generating Documentation Files for GPT on ChatGPT
- Tests using Fixtures with custom Authenticators record incorrect response HOT 1
- async request with multi connector
- Allow partial overriding of fixtures HOT 1
- Asserting a request is sent by closure parameter
- Connector object() throws an exception but json returns null|object?
- Feature Request: Requests have the ability to return a Colleciton HOT 1
- Connector does not support `defaultBody()`?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from saloon.