Skip to content

Instantly share code, notes, and snippets.

@tech-codivores
Last active February 13, 2025 05:16
Show Gist options
  • Save tech-codivores/ddce2b10a64f06fe0b1bcd4868c17039 to your computer and use it in GitHub Desktop.
Save tech-codivores/ddce2b10a64f06fe0b1bcd4868c17039 to your computer and use it in GitHub Desktop.
Laravel Scramble - Passport Request
<?php
namespace ...;
use Dedoc\Scramble\Extensions\OperationExtension;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\Generator\RequestBodyObject;
use Dedoc\Scramble\Support\Generator\Response;
use Dedoc\Scramble\Support\Generator\Schema;
use Dedoc\Scramble\Support\Generator\Types\IntegerType;
use Dedoc\Scramble\Support\Generator\Types\ObjectType;
use Dedoc\Scramble\Support\Generator\Types\StringType;
use Dedoc\Scramble\Support\RouteInfo;
class LaravelPassportOperationExtension extends OperationExtension
{
public function handle(Operation $operation, RouteInfo $routeInfo): void
{
// Path of the 'passport.token' route.
if ($operation->path === '.../oauth/token') {
// Remove default Security if needed.
$operation->addSecurity([]);
// Add Request body.
$operation->addRequestBodyObject(
RequestBodyObject::make()
->setContent('application/json', Schema::fromType(
(new ObjectType)
->addProperty('client_id', (new StringType)->format('uuid'))
->addProperty('client_secret', new StringType)
->addProperty('username', (new StringType)->format('email'))
->addProperty('password', (new StringType)->format('password'))
->addProperty('grant_type', (new StringType)->default('password'))
->setRequired([
'client_id',
'client_secret',
'username',
'password',
'grant_type',
])
))
->required(true)
);
// Add success, error Responses.
$operation->addResponse(
Response::make(\Symfony\Component\HttpFoundation\Response::HTTP_OK)
->setContent('application/json', Schema::fromType(
(new ObjectType)
->addProperty('token_type', (new StringType)->default('Bearer'))
->addProperty('expires_in', (new IntegerType)->setDescription('token lifetime timestamp'))
->addProperty('access_token', new StringType)
->addProperty('refresh_token', new StringType)
))
);
$passportExceptions = [
\League\OAuth2\Server\Exception\OAuthServerException::invalidClient(new \GuzzleHttp\Psr7\ServerRequest($operation->method, $operation->path)),
\League\OAuth2\Server\Exception\OAuthServerException::invalidCredentials(),
];
foreach ($passportExceptions as $passportException) {
$operation->addResponse(
Response::make($passportException->getHttpStatusCode())
->description($passportException->getMessage())
->setContent('application/json', Schema::fromType(
(new ObjectType)
->addProperty('error', (new StringType)->default($passportException->getErrorType()))
->addProperty('error_description', (new StringType)->default($passportException->getMessage()))
->addProperty('message', (new StringType)->default($passportException->getMessage()))
))
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment