You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pixelfed/app/Http/Controllers/OAuth/OobAuthorizationController.php

92 lines
3.0 KiB
PHTML

<?php
namespace App\Http\Controllers\OAuth;
use Illuminate\Http\Request;
9 months ago
use Laravel\Passport\Http\Controllers\ApproveAuthorizationController;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
7 months ago
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
class OobAuthorizationController extends ApproveAuthorizationController
{
/**
* Approve the authorization request.
*/
7 months ago
public function approve(Request $request, ResponseInterface $psrResponse): Response
{
$authRequest = $this->getAuthRequestFromSession($request);
$authRequest->setAuthorizationApproved(true);
7 months ago
return $this->withErrorHandling(function () use ($authRequest, $psrResponse) {
$response = $this->server->completeAuthorizationRequest($authRequest, $psrResponse);
9 months ago
if ($this->isOutOfBandRequest($authRequest)) {
$code = $this->extractAuthorizationCode($response);
9 months ago
return response()->json([
'code' => $code,
9 months ago
'state' => $authRequest->getState(),
]);
}
return $this->convertResponse($response);
7 months ago
}, $authRequest->getGrantTypeId() === 'implicit');
}
/**
* Check if the request is an out-of-band OAuth request.
*
* @param AuthorizationRequest $authRequest
* @return bool
*/
protected function isOutOfBandRequest($authRequest)
{
$redirectUri = $authRequest->getRedirectUri();
if ($redirectUri === 'urn:ietf:wg:oauth:2.0:oob') {
return true;
}
// RFC 6749 §3.1.2.3 permits a client with a single registered redirect
// URI to omit redirect_uri on the authorize request, in which case the
// league server leaves the auth request's redirect URI null. Fall back
// to the client's registered redirect URIs to still detect an OOB-only
// client. Passport's client entity types getRedirectUri() as string|array.
if ($redirectUri === null) {
$registered = $authRequest->getClient()->getRedirectUri();
$registered = is_array($registered) ? $registered : [$registered];
return count($registered) === 1 && $registered[0] === 'urn:ietf:wg:oauth:2.0:oob';
}
return false;
}
/**
* Extract the authorization code from the PSR-7 response.
*
* @param ResponseInterface $response
* @return string
9 months ago
*
* @throws OAuthServerException
*/
protected function extractAuthorizationCode($response)
{
$location = $response->getHeader('Location')[0] ?? '';
9 months ago
if (empty($location)) {
throw OAuthServerException::serverError('Missing authorization code in response');
}
parse_str(parse_url($location, PHP_URL_QUERY), $params);
9 months ago
if (! isset($params['code'])) {
throw OAuthServerException::serverError('Invalid authorization code format');
}
return $params['code'];
}
}