Skip to content

Commit

Permalink
added other security types
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Sep 20, 2022
1 parent 9ff43f6 commit 10b0edd
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __invoke()
->toArray();

if (isset(Scramble::$openApiExtender)) {
$openApi = (Scramble::$openApiExtender)($openApi);
(Scramble::$openApiExtender)($openApi);
}

return $openApi->toArray();
Expand Down
46 changes: 30 additions & 16 deletions src/Support/Generator/SecurityScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,66 @@

namespace Dedoc\Scramble\Support\Generator;

use Dedoc\Scramble\Support\Generator\SecuritySchemes\ApiKeySecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\HttpSecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\Oauth2SecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\OpenIdConnectUrlSecurityScheme;

class SecurityScheme
{
public string $type;

public string $name;

public string $in;

public string $description = '';

public string $schemeName = 'scheme';

public bool $isDefault = false;
public bool $default = false;

private function __construct(string $type)
public function __construct(string $type)
{
$this->type = $type;
}

public static function apiKey(string $in, string $name)
{
$scheme = new self('apiKey');
$scheme->schemeName = 'apiKey';
$scheme->in = $in;
$scheme->name = $name;
return (new ApiKeySecurityScheme($in, $name))->as('apiKey');
}

public static function http(string $scheme, string $bearerFormat = '')
{
return (new HttpSecurityScheme($scheme, $bearerFormat))->as('http');
}

public static function oauth2()
{
return (new Oauth2SecurityScheme)->as('oauth2');
}

return $scheme;
public static function openIdConnect(string $openIdConnectUrl)
{
return (new OpenIdConnectUrlSecurityScheme($openIdConnectUrl))->as('openIdConnect');
}

public static function mutualTLS()
{
return (new static('mutualTLS'))->as('mutualTLS');
}

public function as(string $schemeName): SecurityScheme
public function as(string $schemeName): self
{
$this->schemeName = $schemeName;

return $this;
}

public function setDescription(string $description): SecurityScheme
public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}

public function default(): SecurityScheme
public function default(): self
{
$this->default = true;

Expand All @@ -56,8 +72,6 @@ public function toArray()
{
return array_filter([
'type' => $this->type,
'in' => $this->in,
'name' => $this->name,
'description' => $this->description,
]);
}
Expand Down
28 changes: 28 additions & 0 deletions src/Support/Generator/SecuritySchemes/ApiKeySecurityScheme.php
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 src/Support/Generator/SecuritySchemes/HttpSecurityScheme.php
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,
]);
}
}
47 changes: 47 additions & 0 deletions src/Support/Generator/SecuritySchemes/OAuthFlow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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,
]);
}
}
48 changes: 48 additions & 0 deletions src/Support/Generator/SecuritySchemes/OAuthFlows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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,
])
);
}
}
41 changes: 41 additions & 0 deletions src/Support/Generator/SecuritySchemes/Oauth2SecurityScheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Dedoc\Scramble\Support\Generator\SecuritySchemes;

use Dedoc\Scramble\Support\Generator\SecurityScheme;

class Oauth2SecurityScheme extends SecurityScheme
{
public OAuthFlows $oAuthFlows;

public function __construct()
{
parent::__construct('oauth2');

$this->oAuthFlows = new OAuthFlows;
}

public function flows(callable $flows)
{
$flows($this->oAuthFlows);

return $this;
}

public function flow(string $name, callable $flow)
{
return $this->flows(function (OAuthFlows $flows) use ($flow, $name) {
if (! $flows->$name) {
$flows->$name(new OAuthFlow);
}
$flow($flows->$name);
});
}

public function toArray()
{
return array_merge(parent::toArray(), [
'flows' => $this->oAuthFlows->toArray(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Dedoc\Scramble\Support\Generator\SecuritySchemes;

use Dedoc\Scramble\Support\Generator\SecurityScheme;

class OpenIdConnectUrlSecurityScheme extends SecurityScheme
{
public string $openIdConnectUrl;

public function __construct(string $openIdConnectUrl)
{
parent::__construct('openIdConnect');

$this->openIdConnectUrl = $openIdConnectUrl;
}

public function toArray()
{
return array_merge(parent::toArray(), [
'openIdConnectUrl' => $this->openIdConnectUrl,
]);
}
}
20 changes: 20 additions & 0 deletions tests/OpenApiBuildersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Dedoc\Scramble\Support\Generator\InfoObject;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;
use Dedoc\Scramble\Support\Generator\SecuritySchemes\OAuthFlow;
use function Spatie\Snapshots\assertMatchesSnapshot;

it('builds security scheme', function () {
$openApi = (new OpenApi('3.1.0'))
Expand All @@ -20,3 +22,21 @@
]
]);
});

it('builds oauth2 security scheme', function () {
$openApi = (new OpenApi('3.1.0'))
->setInfo(InfoObject::make('API')->setVersion('0.0.1'));

$openApi->secure(
SecurityScheme::oauth2()
->flow('implicit', function (OAuthFlow $flow) {
$flow
->refreshUrl('https://test.com')
->tokenUrl('https://test.com/token')
->addScope('wow', 'nice');
})
->default()
);

assertMatchesSnapshot($openApi->toArray());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: 3.1.0
info:
title: API
version: 0.0.1
security:
- { oauth2: { } }
components:
securitySchemes: { oauth2: { type: oauth2, flows: { implicit: { tokenUrl: 'https://test.com/token', refreshUrl: 'https://test.com', scopes: { wow: nice } } } } }

0 comments on commit 10b0edd

Please sign in to comment.