Skip to content

Commit 6728f43

Browse files
committed
Added reqest back to security validation callback
1 parent 4099b68 commit 6728f43

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ $options = [
9595
```
9696

9797
#### validateSecurity
98-
If defined, the callback can return Psr\Http\Message\ResponseInterface if the operation is not allowed. `$type` can be none, http or apiKey.
98+
If defined, the callback can return Psr\Http\Message\ResponseInterface if the operation is not allowed. `$type` can be `none`, `http` or `apiKey`.
9999

100100
```php
101101
$options = [
102-
'validateSecurity' => function (string $type, string $token = '', ?array $scopes) : ?\Psr\Http\Message\ResponseInterface {
102+
'validateSecurity' => function (ServerRequestInterface $request, string $type, string $token = '', ?array $scopes) : ?\Psr\Http\Message\ResponseInterface {
103103
// if user is authorized
104104
return null;
105105

src/OpenApiValidation.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ public function __construct($schema, array $options = [])
8181
}
8282

8383
$this->validator = new Validator();
84-
$this->validator->setMaxErrors(99);
84+
$this->validator->setMaxErrors(99);
8585

8686
$this->formatResolver = $this->validator->parser()->getFormatResolver();
8787

8888
// Password validator only checks that it's a string, as format=password only is a hint to the UI
89-
$this->formatResolver->register("string", "password", new OpenApiValidation\Formats\PasswordValidator());
90-
89+
$this->formatResolver->register('string', 'password', new OpenApiValidation\Formats\PasswordValidator());
9190
}
9291

9392
public function addFormat(string $type, string $name, \Opis\JsonSchema\Format $format)
@@ -160,7 +159,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
160159
public function validateSecurity(string $path, string $method, ServerRequestInterface $request) : ?ResponseInterface
161160
{
162161
$securityRequirements = $this->openapi->getOperationSecurity($path, $method);
163-
if (!count($securityRequirements)) return null;
162+
if (!count($securityRequirements)) {
163+
return null;
164+
}
164165

165166
$callback = $this->options['validateSecurity'];
166167

@@ -174,7 +175,7 @@ public function validateSecurity(string $path, string $method, ServerRequestInte
174175
if ($authorizationHeader) {
175176
// Remove basic or bearer
176177
$token = str_replace(ucwords($securitySceme->scheme).' ', '', $authorizationHeader);
177-
return $callback('http', $token, $scopes);
178+
return $callback($request, 'http', $token, $scopes);
178179
}
179180
break;
180181
case 'apiKey':
@@ -192,7 +193,7 @@ public function validateSecurity(string $path, string $method, ServerRequestInte
192193
break;
193194
}
194195
if ($token) {
195-
return $callback('apiKey', $token, $scopes);
196+
return $callback($request, 'apiKey', $token, $scopes);
196197
}
197198
break;
198199
}
@@ -431,7 +432,7 @@ private function validateProperties(array $properties) : ?array
431432
}
432433
if (isset($result) && $result->hasError()) {
433434
$error = $this->parseErrors($result->error(), $property->name, $property->in, $property);
434-
435+
435436
foreach ($error as $parsedError) {
436437
// As all query param values are strings type errors should be discarded
437438
$discard = false;
@@ -451,7 +452,7 @@ private function validateProperties(array $properties) : ?array
451452
}
452453
}
453454
}
454-
}
455+
}
455456

456457
return $errors;
457458
}
@@ -467,7 +468,7 @@ private function validateObject(array $schema, string $value) : array
467468
try {
468469
$value = json_decode($value);
469470
$schema = json_decode(json_encode($schema, JSON_PRESERVE_ZERO_FRACTION));
470-
$result = $this->validator->validate($value, $schema,);
471+
$result = $this->validator->validate($value, $schema, );
471472
} catch (Exception $e) {
472473
return [[
473474
'name' => 'server',
@@ -591,7 +592,7 @@ private function error(int $code, string $message, array $errors = []) : Respons
591592
return $response->withBody((new StreamFactory())->createStream(json_encode($json, JSON_PRESERVE_ZERO_FRACTION)));
592593
}
593594

594-
private function parseErrors(ValidationError $error, $name = null, $in = null, ?Property $property = null ) : array
595+
private function parseErrors(ValidationError $error, $name = null, $in = null, ?Property $property = null) : array
595596
{
596597
$errors = [];
597598
if ($error->subErrors()) {

0 commit comments

Comments
 (0)