Scramble 0.12.x is here! Manual parameter docs, transformers API and more.
Learn more

Authentication

On this page
Scramble PRO
Comprehensive API documentation generation for Spatie’s Laravel Data and Laravel Query Builder.
Are you a visual learner?
Get most out of Scramble with in-depth screencasts.

OpenAPI has plenty of ways to describe the API authentication: https://spec.openapis.org/oas/v3.1.0#security-scheme-object.

Requiring authentication

To document API authentication, use a document transformer and add the security information to the document using secure method.

The secure method on OpenApi object accepts security scheme as an argument. It makes the security scheme default for all endpoints.

use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Scramble::configure()
->withDocumentTransformers(function (OpenApi $openApi) {
$openApi->secure(
SecurityScheme::http('bearer')
);
});
}

Complex authentication scenario

You can also describe more complex authentication scenarios that are allowed in OpenAPI specification.

For example, imagine the API requires 2 headers for authentication: Authorization (the default bearer token header) and X-Tenant (for tenant identification). To implement such authentication:

  1. Manually define a security scheme for these headers;
  2. Define a security requirement that requires both headers.
use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
public function boot()
{
Scramble::configure()
->withDocumentTransformers(function (OpenApi $openApi) {
$openApi->components->securitySchemes['tenant'] = SecurityScheme::apiKey('header', 'X-Tenant');
$openApi->components->securitySchemes['bearer'] = SecurityScheme::http('bearer');
$openApi->security[] = new SecurityRequirement([
'tenant' => [],
'bearer' => [],
]);
});
}

Security scheme examples

Here are some common examples of security schemes objects you may have in your API. For full list of available methods check implementation of SecurityScheme class.

Bearer

One of the most common ways to add auth protection to the API is to use Authorization header with Bearer $token value. If you use this way of auth, you can document it using:

use Dedoc\Scramble\Support\Generator\SecurityScheme;
SecurityScheme::http('bearer');

API Key

When the API token is passed as a query parameter in URL (https://someapi.com/api/items?api_token=0000_00_0001), the next security schema can be used:

use Dedoc\Scramble\Support\Generator\SecurityScheme;
SecurityScheme::apiKey('query', 'api_token');

Bearer JWT

use Dedoc\Scramble\Support\Generator\SecurityScheme;
SecurityScheme::http('bearer', 'JWT');

Basic HTTP

use Dedoc\Scramble\Support\Generator\SecurityScheme;
SecurityScheme::http('basic');

Oauth2

use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\OAuthFlow;
SecurityScheme::oauth2()
->flow('authorizationCode', function (OAuthFlow $flow) {
$flow
->authorizationUrl(config('app.url').'/oauth/authorize')
->tokenUrl(config('app.url').'/oauth/token')
->addScope('*', 'all');
});

Excluding a route from security requirements

You can exclude a route from security requirements by adding @unauthenticated annotation to the route method’s PHPDoc comment block.

/**
* @unauthenticated
*/
public function index(Request $request)
{
return response()->json(/* some data */);
}
Scramble PRO
Comprehensive API documentation generation for Spatie’s Laravel Data and Laravel Query Builder.