Skip to content

Commit d868141

Browse files
authored
Merge pull request #4 from packagist/t/auth-header
Auth: switch to authorization header
2 parents 04ca283 + fd071b9 commit d868141

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/Api/AbstractApi.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ protected function get($path, array $parameters = [], array $headers = [])
3131
}
3232
$response = $this->client->getHttpClient()->get(
3333
$path,
34-
array_merge($headers, ['Accept' => 'application/json'])
34+
array_merge($headers, [
35+
'Accept' => 'application/json',
36+
'Content-Type' => 'application/json',
37+
])
3538
);
3639

3740
return $this->responseMediator->getContent($response);
@@ -47,7 +50,10 @@ protected function post($path, array $parameters = [], array $headers = [])
4750
{
4851
$response = $this->client->getHttpClient()->post(
4952
$path,
50-
array_merge($headers, ['Accept' => 'application/json']),
53+
array_merge($headers, [
54+
'Accept' => 'application/json',
55+
'Content-Type' => 'application/json',
56+
]),
5157
$this->createJsonBody($parameters)
5258
);
5359

@@ -64,7 +70,10 @@ protected function delete($path, array $parameters = [], array $headers = [])
6470
{
6571
$response = $this->client->getHttpClient()->delete(
6672
$path,
67-
array_merge($headers, ['Accept' => 'application/json']),
73+
array_merge($headers, [
74+
'Accept' => 'application/json',
75+
'Content-Type' => 'application/json',
76+
]),
6877
$this->createJsonBody($parameters)
6978
);
7079

src/HttpClient/Plugin/RequestSignature.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,24 @@ public function __construct($token, $secret)
2727
*/
2828
public function handleRequest(RequestInterface $request, callable $next, callable $first)
2929
{
30-
$params = [];
31-
$headers = [
32-
'PRIVATE-PACKAGIST-API-TOKEN' => $params['key'] = $this->token,
33-
'PRIVATE-PACKAGIST-API-TIMESTAMP' => $params['timestamp'] = $this->getTimestamp(),
34-
'PRIVATE-PACKAGIST-API-NONCE' => $params['cnonce'] = $this->getNonce(),
30+
$params = [
31+
'key' => $this->token,
32+
'timestamp' => $this->getTimestamp(),
33+
'cnonce' => $this->getNonce(),
3534
];
3635

37-
foreach ($headers as $header => $value) {
38-
$request = $request->withHeader($header, $value);
39-
}
40-
4136
$content = $request->getBody()->getContents();
4237
if ($content) {
4338
$params['body'] = $content;
4439
}
4540

46-
$request = $request->withHeader('PRIVATE-PACKAGIST-API-SIGNATURE', $this->generateSignature($request, $params));
41+
$request = $request->withHeader('Authorization', sprintf(
42+
'PACKAGIST-HMAC-SHA256 Key=%s, Timestamp=%s, Cnonce=%s, Signature=%s',
43+
$params['key'],
44+
$params['timestamp'],
45+
$params['cnonce'],
46+
$this->generateSignature($request, $params)
47+
));
4748

4849
return $next($request);
4950
}

tests/HttpClient/Plugin/RequestSignatureTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public function testPrefixRequestPath()
3131
'POST',
3232
'/packages/?foo=bar',
3333
[
34-
'PRIVATE-PACKAGIST-API-TOKEN' => $this->token,
35-
'PRIVATE-PACKAGIST-API-TIMESTAMP' => $this->timestamp,
36-
'PRIVATE-PACKAGIST-API-NONCE' => $this->nonce,
37-
'PRIVATE-PACKAGIST-API-SIGNATURE' => 'a6wxBLYrmz4Mwmv/TKBZR5WHFcSCRbsny2frobJMt24=',
34+
'Authorization' => ["PACKAGIST-HMAC-SHA256 Key={$this->token}, Timestamp={$this->timestamp}, Cnonce={$this->nonce}, Signature=a6wxBLYrmz4Mwmv/TKBZR5WHFcSCRbsny2frobJMt24="],
3835
],
3936
json_encode(['foo' => 'bar'])
4037
);
@@ -48,19 +45,10 @@ public function testPrefixRequestPath()
4845
public function testPrefixRequestPathSmoke()
4946
{
5047
$request = new Request('POST', '/packages/?foo=bar', [], json_encode(['foo' => 'bar']));
51-
$expected = [
52-
'PRIVATE-PACKAGIST-API-TOKEN',
53-
'PRIVATE-PACKAGIST-API-TIMESTAMP',
54-
'PRIVATE-PACKAGIST-API-NONCE',
55-
'PRIVATE-PACKAGIST-API-SIGNATURE',
56-
];
5748

5849
$plugin = new RequestSignature($this->token, $this->secret);
59-
$plugin->handleRequest($request, function (Request $actual) use ($expected) {
60-
$headers = $actual->getHeaders();
61-
foreach ($expected as $header) {
62-
$this->assertNotNull($headers[$header][0]);
63-
}
50+
$plugin->handleRequest($request, function (Request $actual) {
51+
$this->assertNotNull($actual->getHeader('Authorization')[0]);
6452
}, function () {
6553
});
6654
}

0 commit comments

Comments
 (0)