Skip to content

Commit 92d90ad

Browse files
committed
Add authentication tokens API
1 parent b2cf0b5 commit 92d90ad

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,39 @@ $teamId = 1;
176176
$packages = $client->teams()->removePackage($teamId, 'acme-website/package');
177177
```
178178

179+
### Authentication Tokens
180+
181+
#### List an organization's team authentication tokens
182+
```php
183+
$tokens = $client->tokens()->all();
184+
```
185+
Returns an array of team tokens.
186+
187+
#### Create a new team authentication token
188+
```php
189+
$token = $client->tokens()->create([
190+
'description' => 'New Team Token',
191+
'access' => 'read',
192+
'teamId' => 1, // Get teamId from the list of teams to determine to which packages the token has access to
193+
]);
194+
```
195+
Returns the created token.
196+
197+
#### Delete a team authentication token
198+
```php
199+
$client->tokens()->remove($tokenId));
200+
```
201+
202+
#### Regenerate a team authentication token
203+
```php
204+
$customerId = 42;
205+
$confirmation = [
206+
'IConfirmOldTokenWillStopWorkingImmediately' => true,
207+
];
208+
$token = $client->tokens()->regenerateToken($tokenId, $confirmation);
209+
```
210+
Returns the regenerated token.
211+
179212
### Customer
180213

181214
#### List an organization's customers

src/Api/Tokens.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* (c) Packagist Conductors GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace PrivatePackagist\ApiClient\Api;
11+
12+
use PrivatePackagist\ApiClient\Exception\InvalidArgumentException;
13+
14+
class Tokens extends AbstractApi
15+
{
16+
public function all()
17+
{
18+
return $this->get('/tokens/');
19+
}
20+
21+
public function create(array $tokenData)
22+
{
23+
return $this->post('/tokens/', $tokenData);
24+
}
25+
26+
public function remove($tokenId)
27+
{
28+
return $this->delete(sprintf('/tokens/%s/', $tokenId));
29+
}
30+
31+
public function regenerate($tokenId, array $confirmation)
32+
{
33+
if (!isset($confirmation['IConfirmOldTokenWillStopWorkingImmediately'])) {
34+
throw new InvalidArgumentException('Confirmation is required to regenerate the Composer repository token.');
35+
}
36+
37+
return $this->post(sprintf('/tokens/%s/regenerate', $tokenId), $confirmation);
38+
}
39+
}

src/Client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function securityIssues()
9797
return new Api\SecurityIssues($this, $this->responseMediator);
9898
}
9999

100-
101100
public function jobs()
102101
{
103102
return new Api\Jobs($this, $this->responseMediator);
@@ -108,6 +107,11 @@ public function mirroredRepositories()
108107
return new Api\MirroredRepositories($this, $this->responseMediator);
109108
}
110109

110+
public function tokens()
111+
{
112+
return new Api\Tokens($this, $this->responseMediator);
113+
}
114+
111115
public function getHttpClient()
112116
{
113117
return $this->getHttpClientBuilder()->getHttpClient();

tests/Api/TokensTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PrivatePackagist\ApiClient\Api;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
7+
class TokensTest extends ApiTestCase
8+
{
9+
public function testAll()
10+
{
11+
$expected = [
12+
[
13+
'description' => 'Team Token',
14+
'access' => 'read',
15+
'teamId' => 1,
16+
'url' => 'https://repo.packagist.com/acme-websites/',
17+
'user' => 'token',
18+
'token' => 'password',
19+
'lastUsed' => '2018-03-14T11:36:00+00:00'
20+
],
21+
];
22+
23+
/** @var Tokens&MockObject $api */
24+
$api = $this->getApiMock();
25+
$api->expects($this->once())
26+
->method('get')
27+
->with($this->equalTo('/tokens/'))
28+
->willReturn($expected);
29+
30+
$this->assertSame($expected, $api->all());
31+
}
32+
33+
public function testCreate()
34+
{
35+
$expected = [
36+
'description' => 'Team Token',
37+
'access' => 'read',
38+
'teamId' => 1,
39+
'url' => 'https://repo.packagist.com/acme-websites/',
40+
'user' => 'token',
41+
'token' => 'password',
42+
'lastUsed' => '2018-03-14T11:36:00+00:00'
43+
];
44+
45+
/** @var Tokens&MockObject $api */
46+
$api = $this->getApiMock();
47+
$api->expects($this->once())
48+
->method('post')
49+
->with($this->equalTo('/tokens/'), $this->equalTo([
50+
'description' => 'Team Token',
51+
'access' => 'read',
52+
'teamId' => 1,
53+
]))
54+
->willReturn($expected);
55+
56+
$this->assertSame($expected, $api->create([
57+
'description' => 'Team Token',
58+
'access' => 'read',
59+
'teamId' => 1,
60+
]));
61+
}
62+
63+
public function testRemove()
64+
{
65+
$expected = [];
66+
67+
/** @var Tokens&MockObject $api */
68+
$api = $this->getApiMock();
69+
$api->expects($this->once())
70+
->method('delete')
71+
->with($this->equalTo('/tokens/1/'))
72+
->willReturn($expected);
73+
74+
$this->assertSame($expected, $api->remove(1));
75+
}
76+
77+
public function testRegenerateToken()
78+
{
79+
$expected = [];
80+
81+
/** @var Tokens&MockObject $api */
82+
$api = $this->getApiMock();
83+
$api->expects($this->once())
84+
->method('post')
85+
->with($this->equalTo('/tokens/1/regenerate'), $this->equalTo(['IConfirmOldTokenWillStopWorkingImmediately' => true]))
86+
->willReturn($expected);
87+
88+
$this->assertSame($expected, $api->regenerate(1, ['IConfirmOldTokenWillStopWorkingImmediately' => true]));
89+
}
90+
91+
/**
92+
* @return string
93+
*/
94+
protected function getApiClass()
95+
{
96+
return Tokens::class;
97+
}
98+
}

0 commit comments

Comments
 (0)