-
Notifications
You must be signed in to change notification settings - Fork 9
Authentication: trusted publishing setup for artifact publishing #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f94b13
Authentication: trusted publishing setup for artifact publishing
glaubinix 22bd57b
Trusted publishing: add docs
glaubinix d406bca
Update tests/HttpClient/Plugin/TrustedPublishingTokenExchangeTest.php
glaubinix 559809a
Assert request made with trusted publishing has proper signature afte…
glaubinix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * (c) Packagist Conductors GmbH <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace PrivatePackagist\ApiClient\HttpClient\Plugin; | ||
|
|
||
| use Http\Client\Common\Plugin; | ||
| use PrivatePackagist\ApiClient\HttpClient\HttpPluginClientBuilder; | ||
| use PrivatePackagist\OIDC\Identities\TokenGeneratorInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class TrustedPublishingTokenExchange implements Plugin | ||
| { | ||
| use Plugin\VersionBridgePlugin; | ||
|
|
||
| /** @var string */ | ||
| private $organizationUrlName; | ||
| /** @var string */ | ||
| private $packageName; | ||
| /** @var HttpPluginClientBuilder $httpPluginClientBuilder */ | ||
| private $httpPluginClientBuilder; | ||
| /** @var TokenGeneratorInterface */ | ||
| private $tokenGenerator; | ||
|
|
||
| public function __construct(string $organizationUrlName, string $packageName, HttpPluginClientBuilder $httpPluginClientBuilder, TokenGeneratorInterface $tokenGenerator) | ||
| { | ||
| $this->organizationUrlName = $organizationUrlName; | ||
| $this->packageName = $packageName; | ||
| $this->httpPluginClientBuilder = $httpPluginClientBuilder; | ||
| $this->tokenGenerator = $tokenGenerator; | ||
| } | ||
|
|
||
| protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) | ||
| { | ||
| $this->httpPluginClientBuilder->removePlugin(self::class); | ||
|
|
||
| $privatePackagistHttpclient = $this->httpPluginClientBuilder->getHttpClient(); | ||
| $audience = json_decode((string) $privatePackagistHttpclient->get('/oidc/audience')->getBody(), true); | ||
| if (!isset($audience['audience'])) { | ||
| throw new \RuntimeException('Unable to get audience'); | ||
| } | ||
|
|
||
| $token = $this->tokenGenerator->generate($audience['audience']); | ||
| if (!$token) { | ||
| throw new \RuntimeException('Unable to generate OIDC token'); | ||
| } | ||
|
|
||
| $apiCredentials = json_decode((string) $privatePackagistHttpclient->post('/oidc/token-exchange/' . $this->organizationUrlName . '/' . $this->packageName, ['Authorization' => 'Bearer ' . $token->token])->getBody(), true); | ||
| if (!isset($apiCredentials['key'], $apiCredentials['secret'])) { | ||
| throw new \RuntimeException('Unable to exchange token'); | ||
| } | ||
|
|
||
| $this->httpPluginClientBuilder->addPlugin($requestSignature = new RequestSignature($apiCredentials['key'], $apiCredentials['secret'])); | ||
|
|
||
| return $requestSignature->handleRequest($request, $next, $first); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 declare(strict_types=1); | ||
|
|
||
| /** | ||
| * (c) Packagist Conductors GmbH <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace PrivatePackagist\ApiClient\HttpClient\Plugin; | ||
|
|
||
| use GuzzleHttp\Psr7\Request; | ||
| use Http\Promise\FulfilledPromise; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class PluginTestCase extends TestCase | ||
| { | ||
| /** @var \Closure */ | ||
| protected $next; | ||
| /** @var \Closure */ | ||
| protected $first; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->next = function (Request $request) { | ||
| return new FulfilledPromise($request); | ||
| }; | ||
| $this->first = function () { | ||
| throw new \RuntimeException('Did not expect plugin to call first'); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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
86 changes: 86 additions & 0 deletions
86
tests/HttpClient/Plugin/TrustedPublishingTokenExchangeTest.php
This file contains hidden or 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,86 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * (c) Packagist Conductors GmbH <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace PrivatePackagist\ApiClient\HttpClient\Plugin; | ||
|
|
||
| use GuzzleHttp\Psr7\Request; | ||
| use GuzzleHttp\Psr7\Response; | ||
| use Http\Mock\Client; | ||
| use Http\Promise\FulfilledPromise; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PrivatePackagist\ApiClient\HttpClient\HttpPluginClientBuilder; | ||
| use PrivatePackagist\OIDC\Identities\Token; | ||
| use PrivatePackagist\OIDC\Identities\TokenGeneratorInterface; | ||
|
|
||
| class TrustedPublishingTokenExchangeTest extends PluginTestCase | ||
| { | ||
| /** @var TrustedPublishingTokenExchange */ | ||
| private $plugin; | ||
| /** @var Client */ | ||
| private $httpClient; | ||
| /** @var TokenGeneratorInterface&MockObject */ | ||
| private $tokenGenerator; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->plugin = new TrustedPublishingTokenExchange( | ||
| 'organization', | ||
| 'acme/package', | ||
| new HttpPluginClientBuilder($this->httpClient = new Client()), | ||
| $this->tokenGenerator = $this->createMock(TokenGeneratorInterface::class) | ||
| ); | ||
| } | ||
|
|
||
| public function testTokenExchange(): void | ||
| { | ||
| $request = new Request('GET', '/api/packages/acme/package'); | ||
|
|
||
| $this->tokenGenerator | ||
| ->expects($this->once()) | ||
| ->method('generate') | ||
| ->with($this->identicalTo('test')) | ||
| ->willReturn(Token::fromTokenString('test.test.test')); | ||
|
|
||
| $this->httpClient->addResponse(new Response(200, [], json_encode(['audience' => 'test']))); | ||
| $this->httpClient->addResponse(new Response(200, [], json_encode(['key' => 'key', 'secret' => 'secret']))); | ||
|
|
||
| $this->plugin->handleRequest($request, function (Request $request) use (&$requestAfterPlugin) { | ||
| $requestAfterPlugin = $request; | ||
|
|
||
| return new FulfilledPromise($request); | ||
| }, $this->first); | ||
|
|
||
| $requests = $this->httpClient->getRequests(); | ||
| $this->assertCount(2, $requests); | ||
| $this->assertSame('/oidc/audience', (string) $requests[0]->getUri()); | ||
| $this->assertSame('/oidc/token-exchange/organization/acme/package', (string) $requests[1]->getUri()); | ||
|
|
||
| $this->assertStringContainsString('PACKAGIST-HMAC-SHA256 Key=key', $requestAfterPlugin->getHeader('Authorization')[0]); | ||
| } | ||
|
|
||
| public function testNoTokenGenerated(): void | ||
| { | ||
| $request = new Request('GET', '/api/packages/acme/package'); | ||
|
|
||
| $this->tokenGenerator | ||
| ->expects($this->once()) | ||
| ->method('generate') | ||
| ->with($this->identicalTo('test')) | ||
| ->willReturn(null); | ||
|
|
||
| $this->httpClient->addResponse(new Response(200, [], json_encode(['audience' => 'test']))); | ||
|
|
||
| $this->expectException(\RuntimeException::class); | ||
| $this->expectExceptionMessage('Unable to generate OIDC token'); | ||
|
|
||
| $this->plugin->handleRequest($request, $this->next, $this->first); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.