Git Product home page Git Product logo

Comments (5)

Sammyjo20 avatar Sammyjo20 commented on June 2, 2024 1

I'm going to close the issue - but please let me know if you have any questions.

from saloon.

Sammyjo20 avatar Sammyjo20 commented on June 2, 2024

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.

asamofal avatar asamofal commented on June 2, 2024

@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.

Sammyjo20 avatar Sammyjo20 commented on June 2, 2024

You should be able to achieve it by extending the following methods:

  • createOAuthAuthenticatorFromResponse
  • createOAuthAuthenticator

and then implement a refresh token request.

from saloon.

asamofal avatar asamofal commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.