generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from dedoc/feature/issue-11
Added security docs of the API
- Loading branch information
Showing
13 changed files
with
401 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: Security | ||
weight: 4 | ||
--- | ||
|
||
Most likely, your API is protected by some sort of the auth. OpenAPI have plenty of ways to describe the API (see full documentation of spec here https://spec.openapis.org/oas/v3.1.0#security-scheme-object). | ||
|
||
## Adding security scheme | ||
Scramble allows you to document how your API is secured. To document this, you can use `Scramble::extendOpenApi` method and add security information to OpenAPI document using `secure` method. | ||
|
||
You should call `extendOpenApi` in `register` method of some of your service providers. This method accepts a callback that accepts OpenAPI document as a first argument. | ||
|
||
`secure` method on `OpenApi` object accepts security scheme as an argument. It makes the security scheme default for all endpoints. | ||
|
||
```php | ||
namespace App\Providers; | ||
|
||
use Dedoc\Scramble\Support\Generator\OpenApi; | ||
use Dedoc\Scramble\Support\Generator\SecurityScheme; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class DocsServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
Scramble::extendOpenApi(function (OpenApi $openApi) { | ||
$openApi->secure( | ||
SecurityScheme::apiKey('query', 'api_token') | ||
); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## Examples | ||
Here are some common examples of the security schemes object you may have in your API. For full list of available methods check implementation of `SecurityScheme` class. | ||
|
||
### API Key | ||
```php | ||
SecurityScheme::apiKey('query', 'api_token'); | ||
``` | ||
|
||
### Basic HTTP | ||
```php | ||
SecurityScheme::http('basic'); | ||
``` | ||
|
||
### JWT | ||
```php | ||
SecurityScheme::http('bearer', 'JWT'); | ||
``` | ||
|
||
### Oauth2 | ||
```php | ||
SecurityScheme::oauth2() | ||
->flow('implicit', function (OAuthFlow $flow) { | ||
$flow | ||
->authorizationUrl('https://example.com/api/oauth/dialog') | ||
->addScope('write:pets', 'modify pets in your account'); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Support/Generator/SecuritySchemes/ApiKeySecurityScheme.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Generator\SecuritySchemes; | ||
|
||
use Dedoc\Scramble\Support\Generator\SecurityScheme; | ||
|
||
class ApiKeySecurityScheme extends SecurityScheme | ||
{ | ||
public string $name; | ||
|
||
public string $in; | ||
|
||
public function __construct(string $in, string $name) | ||
{ | ||
parent::__construct('apiKey'); | ||
|
||
$this->in = $in; | ||
$this->name = $name; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_merge(parent::toArray(), [ | ||
'in' => $this->in, | ||
'name' => $this->name, | ||
]); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Support/Generator/SecuritySchemes/HttpSecurityScheme.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Generator\SecuritySchemes; | ||
|
||
use Dedoc\Scramble\Support\Generator\SecurityScheme; | ||
|
||
class HttpSecurityScheme extends SecurityScheme | ||
{ | ||
public string $scheme; | ||
|
||
public string $bearerFormat = ''; | ||
|
||
public function __construct(string $scheme, string $bearerFormat = '') | ||
{ | ||
parent::__construct('http'); | ||
|
||
$this->scheme = $scheme; | ||
$this->bearerFormat = $bearerFormat; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_merge(parent::toArray(), [ | ||
'scheme' => $this->scheme, | ||
'bearerFormat' => $this->bearerFormat, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Generator\SecuritySchemes; | ||
|
||
class OAuthFlow | ||
{ | ||
public string $authorizationUrl = ''; | ||
|
||
public string $tokenUrl = ''; | ||
|
||
public string $refreshUrl = ''; | ||
|
||
/** @var array<string, string> */ | ||
public array $scopes = []; | ||
|
||
public function authorizationUrl(string $authorizationUrl): OAuthFlow | ||
{ | ||
$this->authorizationUrl = $authorizationUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function tokenUrl(string $tokenUrl): OAuthFlow | ||
{ | ||
$this->tokenUrl = $tokenUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function refreshUrl(string $refreshUrl): OAuthFlow | ||
{ | ||
$this->refreshUrl = $refreshUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function addScope(string $name, string $description = '') | ||
{ | ||
$this->scopes[$name] = $description; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_filter([ | ||
'authorizationUrl' => $this->authorizationUrl, | ||
'tokenUrl' => $this->tokenUrl, | ||
'refreshUrl' => $this->refreshUrl, | ||
'scopes' => $this->scopes, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Generator\SecuritySchemes; | ||
|
||
class OAuthFlows | ||
{ | ||
public ?OAuthFlow $implicit = null; | ||
|
||
public ?OAuthFlow $password = null; | ||
|
||
public ?OAuthFlow $clientCredentials = null; | ||
|
||
public ?OAuthFlow $authorizationCode = null; | ||
|
||
public function implicit(?OAuthFlow $flow): OAuthFlows | ||
{ | ||
$this->implicit = $flow; | ||
|
||
return $this; | ||
} | ||
|
||
public function password(?OAuthFlow $flow): OAuthFlows | ||
{ | ||
$this->password = $flow; | ||
|
||
return $this; | ||
} | ||
|
||
public function clientCredentials(?OAuthFlow $flow): OAuthFlows | ||
{ | ||
$this->clientCredentials = $flow; | ||
|
||
return $this; | ||
} | ||
|
||
public function authorizationCode(?OAuthFlow $flow): OAuthFlows | ||
{ | ||
$this->authorizationCode = $flow; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return array_map( | ||
fn ($f) => $f->toArray(), | ||
array_filter([ | ||
'implicit' => $this->implicit, | ||
'password' => $this->password, | ||
'clientCredentials' => $this->clientCredentials, | ||
'authorizationCode' => $this->authorizationCode, | ||
]) | ||
); | ||
} | ||
} |
Oops, something went wrong.