Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better Bearer handling & Added Basic auth 🎉
- Loading branch information
Showing
19 changed files
with
360 additions
and
22 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
File renamed without changes.
32 changes: 32 additions & 0 deletions
32
.../Tests/fixtures/authentication-http-basic/expected/Authentication/BasicAuthentication.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,32 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected\Authentication; | ||
|
||
class BasicAuthentication implements \Jane\OpenApiRuntime\Client\Authentication | ||
{ | ||
private $username; | ||
private $password; | ||
public function __construct(string $username, string $password) | ||
{ | ||
$this->{'username'} = $username; | ||
$this->{'password'} = $password; | ||
} | ||
public function getPlugin() : \Http\Client\Common\Plugin | ||
{ | ||
return new \Http\Client\Common\Plugin\AuthenticationPlugin(new class($this->{'token'}) implements \Http\Message\Authentication | ||
{ | ||
private $username; | ||
private $password; | ||
public function __construct(string $username, string $password) | ||
{ | ||
$this->{'username'} = $username; | ||
$this->{'password'} = $password; | ||
} | ||
public function authenticate(\Psr\Http\Message\RequestInterface $request) | ||
{ | ||
$header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->{'username'}, $this->{'password'}))); | ||
return $request->withHeader('Authorization', $header); | ||
} | ||
}); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
"securitySchemes": { | ||
"Basic": { | ||
"type": "http", | ||
"scheme": "Bearer" | ||
"scheme": "basic" | ||
} | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
src/OpenApi/Tests/fixtures/authentication-http-bearer/.jane-openapi
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,9 @@ | ||
<?php | ||
|
||
return [ | ||
'openapi-file' => __DIR__ . '/swagger.json', | ||
'namespace' => 'Jane\OpenApi\Tests\Expected', | ||
'directory' => __DIR__ . '/generated', | ||
'use-fixer' => false, | ||
// 'client' => 'psr18', | ||
]; |
28 changes: 28 additions & 0 deletions
28
...ests/fixtures/authentication-http-bearer/expected/Authentication/BearerAuthentication.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 Jane\OpenApi\Tests\Expected\Authentication; | ||
|
||
class BearerAuthentication implements \Jane\OpenApiRuntime\Client\Authentication | ||
{ | ||
private $token; | ||
public function __construct(string $token) | ||
{ | ||
$this->{'token'} = $token; | ||
} | ||
public function getPlugin() : \Http\Client\Common\Plugin | ||
{ | ||
return new \Http\Client\Common\Plugin\AuthenticationPlugin(new class($this->{'token'}) implements \Http\Message\Authentication | ||
{ | ||
private $token; | ||
public function __construct(string $token) | ||
{ | ||
$this->{'token'} = $token; | ||
} | ||
public function authenticate(\Psr\Http\Message\RequestInterface $request) | ||
{ | ||
$header = sprintf('Bearer %s', $this->{'token'}); | ||
return $request->withHeader('Authorization', $header); | ||
} | ||
}); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/OpenApi/Tests/fixtures/authentication-http-bearer/expected/Client.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,34 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected; | ||
|
||
class Client extends \Jane\OpenApiRuntime\Client\Psr7HttplugClient | ||
{ | ||
/** | ||
* @param string $fetch Fetch mode to use (can be OBJECT or RESPONSE) | ||
* | ||
* @return null|\Jane\OpenApi\Tests\Expected\Model\Foo|\Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function getFoo(string $fetch = self::FETCH_OBJECT) | ||
{ | ||
return $this->executePsr7Endpoint(new \Jane\OpenApi\Tests\Expected\Endpoint\GetFoo(), $fetch); | ||
} | ||
public static function create($httpClient = null, \Jane\OpenApiRuntime\Client\Authentication $authentication = null) | ||
{ | ||
if (null === $httpClient) { | ||
$httpClient = \Http\Discovery\HttpClientDiscovery::find(); | ||
$plugins = array(); | ||
$uri = \Http\Discovery\UriFactoryDiscovery::find()->createUri('https://www.foo-host.com/base-path'); | ||
$plugins[] = new \Http\Client\Common\Plugin\AddHostPlugin($uri); | ||
$plugins[] = new \Http\Client\Common\Plugin\AddPathPlugin($uri); | ||
if (null !== $authentication) { | ||
$plugins[] = $authentication->getPlugin(); | ||
} | ||
$httpClient = new \Http\Client\Common\PluginClient($httpClient, $plugins); | ||
} | ||
$messageFactory = \Http\Discovery\MessageFactoryDiscovery::find(); | ||
$streamFactory = \Http\Discovery\StreamFactoryDiscovery::find(); | ||
$serializer = new \Symfony\Component\Serializer\Serializer(\Jane\OpenApi\Tests\Expected\Normalizer\NormalizerFactory::create(), array(new \Symfony\Component\Serializer\Encoder\JsonEncoder(new \Symfony\Component\Serializer\Encoder\JsonEncode(), new \Symfony\Component\Serializer\Encoder\JsonDecode()))); | ||
return new static($httpClient, $messageFactory, $serializer, $streamFactory); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/OpenApi/Tests/fixtures/authentication-http-bearer/expected/Endpoint/GetFoo.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,36 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected\Endpoint; | ||
|
||
class GetFoo extends \Jane\OpenApiRuntime\Client\BaseEndpoint implements \Jane\OpenApiRuntime\Client\Psr7Endpoint | ||
{ | ||
use \Jane\OpenApiRuntime\Client\Psr7EndpointTrait; | ||
public function getMethod() : string | ||
{ | ||
return 'GET'; | ||
} | ||
public function getUri() : string | ||
{ | ||
return '/foo'; | ||
} | ||
public function getBody(\Symfony\Component\Serializer\SerializerInterface $serializer, $streamFactory = null) : array | ||
{ | ||
return array(array(), null); | ||
} | ||
public function getExtraHeaders() : array | ||
{ | ||
return array('Accept' => array('application/json')); | ||
} | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* | ||
* @return null|\Jane\OpenApi\Tests\Expected\Model\Foo | ||
*/ | ||
protected function transformResponseBody(string $body, int $status, \Symfony\Component\Serializer\SerializerInterface $serializer, ?string $contentType = null) | ||
{ | ||
if (200 === $status && mb_strpos($contentType, 'application/json') !== false) { | ||
return $serializer->deserialize($body, 'Jane\\OpenApi\\Tests\\Expected\\Model\\Foo', 'json'); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/OpenApi/Tests/fixtures/authentication-http-bearer/expected/Model/Foo.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,34 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected\Model; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* | ||
* | ||
* @var string | ||
*/ | ||
protected $foo; | ||
/** | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public function getFoo() : string | ||
{ | ||
return $this->foo; | ||
} | ||
/** | ||
* | ||
* | ||
* @param string $foo | ||
* | ||
* @return self | ||
*/ | ||
public function setFoo(string $foo) : self | ||
{ | ||
$this->foo = $foo; | ||
return $this; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/OpenApi/Tests/fixtures/authentication-http-bearer/expected/Normalizer/FooNormalizer.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,42 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected\Normalizer; | ||
|
||
use Jane\JsonSchemaRuntime\Reference; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
class FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface | ||
{ | ||
use DenormalizerAwareTrait; | ||
use NormalizerAwareTrait; | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
return $type === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Foo'; | ||
} | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return is_object($data) && get_class($data) === 'Jane\\OpenApi\\Tests\\Expected\\Model\\Foo'; | ||
} | ||
public function denormalize($data, $class, $format = null, array $context = array()) | ||
{ | ||
if (!is_object($data)) { | ||
throw new InvalidArgumentException(); | ||
} | ||
$object = new \Jane\OpenApi\Tests\Expected\Model\Foo(); | ||
if (property_exists($data, 'foo')) { | ||
$object->setFoo($data->{'foo'}); | ||
} | ||
return $object; | ||
} | ||
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
$data = new \stdClass(); | ||
$data->{'foo'} = $object->getFoo(); | ||
return $data; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...enApi/Tests/fixtures/authentication-http-bearer/expected/Normalizer/NormalizerFactory.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,14 @@ | ||
<?php | ||
|
||
namespace Jane\OpenApi\Tests\Expected\Normalizer; | ||
|
||
class NormalizerFactory | ||
{ | ||
public static function create() | ||
{ | ||
$normalizers = array(); | ||
$normalizers[] = new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer(); | ||
$normalizers[] = new FooNormalizer(); | ||
return $normalizers; | ||
} | ||
} |
Oops, something went wrong.