Skip to content

Commit

Permalink
Merge pull request #74 from packagist/readme-api-key
Browse files Browse the repository at this point in the history
Use the same wording for key/secret as in the UI
  • Loading branch information
naderman committed Jan 5, 2024
2 parents 32d958b + ad4b020 commit b0b2fd5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Why do you need to require `guzzlehttp/guzzle`? We are decoupled from any HTTP m
require_once __DIR__ . '/vendor/autoload.php';

$client = new \PrivatePackagist\ApiClient\Client();
$client->authenticate('api-token', 'api-secret');
$client->authenticate('api-key', 'api-secret');
$packages = $client->packages()->all();
```

Expand Down
6 changes: 3 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function __construct(HttpPluginClientBuilder $httpClientBuilder = null, $
}

/**
* @param string $token
* @param string $key
* @param string $secret
*/
public function authenticate($token, $secret)
public function authenticate($key, $secret)
{
$this->httpClientBuilder->removePlugin(RequestSignature::class);
$this->httpClientBuilder->addPlugin(new RequestSignature($token, $secret));
$this->httpClientBuilder->addPlugin(new RequestSignature($key, $secret));
}

public function credentials()
Expand Down
14 changes: 7 additions & 7 deletions src/HttpClient/Plugin/RequestSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ class RequestSignature implements Plugin
use Plugin\VersionBridgePlugin;

/** @var string */
private $token;
private $key;
/** @var string */
private $secret;

/**
* @param string $token
* @param string $key
* @param string $secret
*/
public function __construct($token, $secret)
public function __construct($key, $secret)
{
if (!$token || !$secret) {
throw new \InvalidArgumentException('$token and $secret must be set');
if (!$key || !$secret) {
throw new \InvalidArgumentException('$key and $secret must be set');
}

$this->token = $token;
$this->key = $key;
$this->secret = $secret;
}

Expand All @@ -41,7 +41,7 @@ public function __construct($token, $secret)
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
{
$params = [
'key' => $this->token,
'key' => $this->key,
'timestamp' => $this->getTimestamp(),
'cnonce' => $this->getNonce(),
];
Expand Down
18 changes: 9 additions & 9 deletions tests/HttpClient/Plugin/RequestSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class RequestSignatureTest extends TestCase
private $plugin;
private $next;
private $first;
private $token;
private $key;
private $secret;
private $timestamp;
private $nonce;

protected function setUp(): void
{
$this->token = 'token';
$this->key = 'token';
$this->secret = 'secret';
$this->timestamp = 1518721253;
$this->nonce = '78b9869e96cf58b5902154e0228f8576f042e5ac';
$this->plugin = new RequestSignatureMock($this->token, $this->secret);
$this->plugin = new RequestSignatureMock($this->key, $this->secret);
$this->plugin->init($this->timestamp, $this->nonce);
$this->next = function (Request $request) {
return new FulfilledPromise($request);
Expand All @@ -47,7 +47,7 @@ public function testPrefixRequestPath()
'POST',
'/packages/?foo=bar',
[
'Authorization' => ["PACKAGIST-HMAC-SHA256 Key={$this->token}, Timestamp={$this->timestamp}, Cnonce={$this->nonce}, Signature=a6wxBLYrmz4Mwmv/TKBZR5WHFcSCRbsny2frobJMt24="],
'Authorization' => ["PACKAGIST-HMAC-SHA256 Key={$this->key}, Timestamp={$this->timestamp}, Cnonce={$this->nonce}, Signature=a6wxBLYrmz4Mwmv/TKBZR5WHFcSCRbsny2frobJMt24="],
],
json_encode(['foo' => 'bar'])
);
Expand All @@ -67,20 +67,20 @@ public function testPrefixRequestPathSmoke()
}

/**
* @dataProvider tokenSecretProvider
* @dataProvider keySecretProvider
*/
public function testMissingTokenOrSecret(string $token, string $secret): void
public function testMissingTokenOrSecret(string $key, string $secret): void
{
$this->expectException(\InvalidArgumentException::class);

new RequestSignature($token, $secret);
new RequestSignature($key, $secret);
}

public function tokenSecretProvider(): array
public function keySecretProvider(): array
{
return [
['', ''],
['token', ''],
['key', ''],
['', 'secret'],
];
}
Expand Down

0 comments on commit b0b2fd5

Please sign in to comment.