Skip to content

Commit 42676ff

Browse files
authored
Merge pull request #50 from packagist/t/add-tokens-api
Authentication Tokens: add API endpoints
2 parents b2cf0b5 + 38cdd9e commit 42676ff

File tree

4 files changed

+196
-2
lines changed

4 files changed

+196
-2
lines changed

README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
* [List all private packages a team has access to](#list-all-private-packages-a-team-has-access-to)
1616
* [Grant a team access to a list of private packages](#grant-a-team-access-to-a-list-of-private-packages)
1717
* [Remove access for a package from a team](#remove-access-for-a-package-from-a-team)
18+
* [Authentication Tokens](#authentication-tokens)
19+
* [List an organization's team authentication tokens](#list-an-organizations-team-authentication-tokens)
20+
* [Create a new team authentication token](#create-a-new-team-authentication-token)
21+
* [Delete a team authentication token](#delete-a-team-authentication-token)
22+
* [Regenerate a team authentication token](#regenerate-a-team-authentication-token)
1823
* [Customer](#customer)
1924
* [List an organization's customers](#list-an-organizations-customers)
2025
* [Show a customer](#show-a-customer)
@@ -102,7 +107,7 @@
102107
* [Validate incoming webhook payloads](#validate-incoming-webhook-payloads)
103108
* [License](#license)
104109

105-
<!-- Added by: wissem, at: Wed May 26 12:28:19 CEST 2021 -->
110+
<!-- Added by: glaubinix, at: Thu 13 Jan 2022 13:34:48 GMT -->
106111

107112
<!--te-->
108113

@@ -176,6 +181,47 @@ $teamId = 1;
176181
$packages = $client->teams()->removePackage($teamId, 'acme-website/package');
177182
```
178183

184+
### Authentication Tokens
185+
186+
#### List an organization's team authentication tokens
187+
```php
188+
$tokens = $client->tokens()->all();
189+
```
190+
Returns an array of team tokens.
191+
192+
#### Create a new team authentication token
193+
```php
194+
// Create a new token with access to all packages
195+
$token = $client->tokens()->create([
196+
'description' => 'New Team Token',
197+
'access' => 'read',
198+
'accessToAllPackages' => true,
199+
]);
200+
201+
// Create a new token with access to packages a team has access to
202+
$token = $client->tokens()->create([
203+
'description' => 'New Team Token',
204+
'access' => 'read',
205+
'teamId' => 1, // Get teamId from the list of teams to determine to which packages the token has access to
206+
]);
207+
```
208+
Returns the created token.
209+
210+
#### Delete a team authentication token
211+
```php
212+
$client->tokens()->remove($tokenId));
213+
```
214+
215+
#### Regenerate a team authentication token
216+
```php
217+
$customerId = 42;
218+
$confirmation = [
219+
'IConfirmOldTokenWillStopWorkingImmediately' => true,
220+
];
221+
$token = $client->tokens()->regenerateToken($tokenId, $confirmation);
222+
```
223+
Returns the regenerated token.
224+
179225
### Customer
180226

181227
#### List an organization's customers

src/Api/Tokens.php

+39
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

+5-1
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

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

0 commit comments

Comments
 (0)