Skip to content

Commit

Permalink
chore: improve redirect callback
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaur committed Mar 26, 2023
1 parent a82251e commit c165109
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Http/Controllers/CallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CallbackController
public function __invoke(Request $request): Response
{
return app(CallbackResponseContract::class)(
accessCode: (string) $request->query('code'),
state: $request->query('state'),
accessCode: (string) $request->query('code', ''),
state: (string) $request->query('state', ''),
internalState: Cache::pull('pinterest_api::oauth_state')
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Http/Responses/CallbackResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class CallbackResponse implements CallbackResponseContract
{
public function __invoke(string $accessCode, string $state, ?string $internalState): Response
{
if (blank($state)) {
abort(Response::HTTP_NOT_FOUND);
}

if ($internalState === null) {
return new Response(
content: 'Your request has expired! Please try again by running `php artisan pinterest:generate-access-code-link`.',
Expand All @@ -21,10 +25,9 @@ public function __invoke(string $accessCode, string $state, ?string $internalSta
}

if ($state !== $internalState) {
return new Response(
content: 'Not good! Request has been tampered!',
status: Response::HTTP_FORBIDDEN
);
report('Pinterest callback request has been tampered!');

abort(Response::HTTP_NOT_FOUND);
}

event(new CredentialsRetrieved(OAuthData::from(['access_code' => $accessCode])));
Expand Down

0 comments on commit c165109

Please sign in to comment.