Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysLees committed Feb 17, 2025
1 parent d5056e0 commit 78a63e2
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 56 deletions.
9 changes: 7 additions & 2 deletions src/Authenticator/InstagramAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodebarAg\LaravelInstagram\Authenticator;

use DateTimeImmutable;
use Illuminate\Support\Carbon;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Http\PendingRequest;

Expand Down Expand Up @@ -52,10 +53,12 @@ public function getAccessToken(): string

/**
* Get the refresh token
*
* @throws \Exception
*/
public function getRefreshToken(): ?string
{
return $this->refreshToken;
throw new \Exception('Instagram does not provide refresh tokens. use getAccessToken() instead.');
}

/**
Expand All @@ -71,7 +74,9 @@ public function getExpiresAt(): ?DateTimeImmutable
*/
public function isRefreshable(): bool
{
return isset($this->refreshToken);
Carbon::createFromTimestamp($this->getExpiresAt()->getTimestamp())->diffInDays();

return now()->subHours(24)->gt($this->getExpiresAt());
}

/**
Expand Down
55 changes: 1 addition & 54 deletions src/Connectors/InstagramConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
namespace CodebarAg\LaravelInstagram\Connectors;

use CodebarAg\LaravelInstagram\Authenticator\InstagramAuthenticator;
use CodebarAg\LaravelInstagram\Requests\Authentication\GetAccessTokenRequest;
use CodebarAg\LaravelInstagram\Requests\Authentication\GetShortLivedAccessTokenRequest;
use CodebarAg\LaravelInstagram\Traits\AuthorizationCodeGrant;
use DateTimeImmutable;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Exceptions\InvalidStateException;
use Saloon\Exceptions\OAuthConfigValidationException;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\Connector;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\OAuth2\AuthorizationCodeGrant;

class InstagramConnector extends Connector
{
Expand Down Expand Up @@ -61,51 +55,4 @@ protected function defaultOauthConfig(): OAuthConfig
->setTokenEndpoint('https://api.instagram.com/oauth/access_token')
->setUserEndpoint('/me');
}

/**
* Get the short lived access token.
*
* @template TRequest of \Saloon\Http\Request
*
* @param callable(TRequest): (void)|null $requestModifier
*
* @throws \Saloon\Exceptions\InvalidStateException
* @throws OAuthConfigValidationException
*/
public function getShortLivedAccessToken(string $code, ?string $state = null, ?string $expectedState = null, bool $returnResponse = false, ?callable $requestModifier = null): OAuthAuthenticator|Response
{
$this->oauthConfig()->validate();

if (! empty($state) && ! empty($expectedState) && $state !== $expectedState) {
throw new InvalidStateException;
}

$request = $this->resolveShortLivedAccessTokenRequest($code, $this->oauthConfig());

$request = $this->oauthConfig()->invokeRequestModifier($request);

if (is_callable($requestModifier)) {
$requestModifier($request);
}

$response = $this->send($request);

if ($returnResponse === true) {
return $response;
}

$response->throw();

return $this->createOAuthAuthenticatorFromResponse($response);
}

protected function resolveAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
{
return new GetAccessTokenRequest($code, $oauthConfig);
}

protected function resolveShortLivedAccessTokenRequest(string $code, OAuthConfig $oauthConfig): Request
{
return new GetShortLivedAccessTokenRequest($code, $oauthConfig);
}
}
49 changes: 49 additions & 0 deletions src/Requests/Authentication/GetRefreshAccessTokenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace CodebarAg\LaravelInstagram\Requests\Authentication;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Plugins\AcceptsJson;

class GetRefreshAccessTokenRequest extends Request
{
use AcceptsJson;

/**
* Define the method that the request will use.
*/
protected Method $method = Method::GET;

/**
* Define the endpoint for the request.
*/
public function resolveEndpoint(): string
{
return 'https://graph.instagram.com/refresh_access_token';
}

/**
* Requires the authorization code and OAuth 2 config.
*/
public function __construct(protected string $code) {}

/**
* Register the default data.
*
* @return array{
* grant_type: string,
* access_token: string,
* client_secret: string,
* }
*/
public function defaultQuery(): array
{
return [

Check failure on line 44 in src/Requests/Authentication/GetRefreshAccessTokenRequest.php

View workflow job for this annotation

GitHub Actions / phpstan

Method CodebarAg\LaravelInstagram\Requests\Authentication\GetRefreshAccessTokenRequest::defaultQuery() should return array{grant_type: string, access_token: string, client_secret: string} but returns array{grant_type: 'ig_exchange_token', access_token: string}.
'grant_type' => 'ig_exchange_token',
'access_token' => $this->code,
];
}
}
Loading

0 comments on commit 78a63e2

Please sign in to comment.