Skip to content

Commit 5764289

Browse files
author
Evan Sims
authored
feat(Management): Add support for client credentials endpoints (#700)
1 parent 5daa7a1 commit 5764289

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

src/API/Management/Clients.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,86 @@ public function delete(
112112
withOptions($options)->
113113
call();
114114
}
115+
116+
public function createCredentials(
117+
string $clientId,
118+
array $body,
119+
?RequestOptions $options = null,
120+
): ResponseInterface {
121+
[$clientId] = Toolkit::filter([$clientId])->string()->trim();
122+
[$body] = Toolkit::filter([$body])->array()->trim();
123+
124+
Toolkit::assert([
125+
[$clientId, \Auth0\SDK\Exception\ArgumentException::missing('clientId')],
126+
])->isString();
127+
128+
/** @var array<mixed> $body */
129+
130+
return $this->getHttpClient()->
131+
method('post')->
132+
addPath('clients', $clientId, 'credentials')->
133+
withBody((object) $body)->
134+
withOptions($options)->
135+
call();
136+
}
137+
138+
public function getCredentials(
139+
string $clientId,
140+
?array $parameters = null,
141+
?RequestOptions $options = null,
142+
): ResponseInterface {
143+
[$clientId] = Toolkit::filter([$clientId])->string()->trim();
144+
[$parameters] = Toolkit::filter([$parameters])->array()->trim();
145+
146+
Toolkit::assert([
147+
[$clientId, \Auth0\SDK\Exception\ArgumentException::missing('clientId')],
148+
])->isString();
149+
150+
/** @var array<int|string|null> $parameters */
151+
152+
return $this->getHttpClient()->
153+
method('get')->
154+
addPath('clients', $clientId, 'credentials')->
155+
withParams($parameters)->
156+
withOptions($options)->
157+
call();
158+
}
159+
160+
public function getCredential(
161+
string $clientId,
162+
string $credentialId,
163+
?RequestOptions $options = null,
164+
): ResponseInterface {
165+
[$clientId, $credentialId] = Toolkit::filter([$clientId, $credentialId])->string()->trim();
166+
167+
Toolkit::assert([
168+
[$clientId, \Auth0\SDK\Exception\ArgumentException::missing('clientId')],
169+
[$credentialId, \Auth0\SDK\Exception\ArgumentException::missing('credentialId')],
170+
])->isString();
171+
172+
return $this->getHttpClient()->
173+
method('get')->
174+
addPath('clients', $clientId, 'credentials', $credentialId)->
175+
withOptions($options)->
176+
call();
177+
}
178+
179+
public function deleteCredential(
180+
string $clientId,
181+
string $credentialId,
182+
?RequestOptions $options = null,
183+
): ResponseInterface {
184+
[$clientId, $credentialId] = Toolkit::filter([$clientId, $credentialId])->string()->trim();
185+
186+
Toolkit::assert([
187+
[$clientId, \Auth0\SDK\Exception\ArgumentException::missing('clientId')],
188+
[$credentialId, \Auth0\SDK\Exception\ArgumentException::missing('credentialId')],
189+
])->isString();
190+
191+
return $this->getHttpClient()->
192+
method('delete')->
193+
addPath('clients', $clientId, 'credentials', $credentialId)->
194+
withOptions($options)->
195+
call();
196+
}
115197
}

src/Contract/API/Management/ClientsInterface.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,75 @@ public function delete(
105105
string $id,
106106
?RequestOptions $options = null,
107107
): ResponseInterface;
108+
109+
/**
110+
* Add new credentials to the specified Client.
111+
*
112+
* @param string $clientId Client to add credentials to
113+
* @param array<mixed> $body Additional body content to pass with the API request. See @see for supported options.
114+
* @param RequestOptions|null $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
115+
*
116+
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
117+
*
118+
* @see https://auth0.com/docs/api/management/v2#!/Client_Credentials/post_client_credentials
119+
*/
120+
public function createCredentials(
121+
string $clientId,
122+
array $body,
123+
?RequestOptions $options = null,
124+
): ResponseInterface;
125+
126+
/**
127+
* Get all credentials from the specified Client.
128+
*
129+
* @param string $clientId Client to retrieve credentials for
130+
* @param array<int|string|null>|null $parameters Optional. Additional query parameters to pass with the API request. See @see for supported options.
131+
* @param RequestOptions|null $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
132+
*
133+
* @throws \Auth0\SDK\Exception\ArgumentException when an invalid `clientId` is provided
134+
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
135+
*
136+
* @see https://auth0.com/docs/api/management/v2#!/Client_Credentials/get_client_credentials
137+
*/
138+
public function getCredentials(
139+
string $clientId,
140+
?array $parameters = null,
141+
?RequestOptions $options = null,
142+
): ResponseInterface;
143+
144+
/**
145+
* Get a specific credential from the specified Client.
146+
*
147+
* @param string $clientId Client to retrieve credential for
148+
* @param string $credentialId credential to retrieve
149+
* @param RequestOptions|null $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
150+
*
151+
* @throws \Auth0\SDK\Exception\ArgumentException when an invalid `clientId` or `credentialId` are provided
152+
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
153+
*
154+
* @see https://auth0.com/docs/api/management/v2#!/Client_Credentials/get_client_credentials_by_id
155+
*/
156+
public function getCredential(
157+
string $clientId,
158+
string $credentialId,
159+
?RequestOptions $options = null,
160+
): ResponseInterface;
161+
162+
/**
163+
* Delete a credential from the specified Client.
164+
*
165+
* @param string $clientId client to delete credential for
166+
* @param string $credentialId credential to delete
167+
* @param RequestOptions|null $options Optional. Additional request options to use, such as a field filtering or pagination. (Not all endpoints support these. See @see for supported options.)
168+
*
169+
* @throws \Auth0\SDK\Exception\ArgumentException when an invalid `clientId` or `credentialId` are provided
170+
* @throws \Auth0\SDK\Exception\NetworkException when the API request fails due to a network error
171+
*
172+
* @see https://auth0.com/docs/api/management/v2#!/Client_Credentials/delete_client_credentials_by_id
173+
*/
174+
public function deleteCredential(
175+
string $clientId,
176+
string $credentialId,
177+
?RequestOptions $options = null,
178+
): ResponseInterface;
108179
}

tests/Unit/API/Management/ClientsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use Auth0\Tests\Utilities\TokenGenerator;
6+
57
uses()->group('management', 'management.clients');
68

79
beforeEach(function(): void {
@@ -65,3 +67,65 @@
6567
$this->assertArrayHasKey('name', $body);
6668
expect($body['name'])->toEqual('__test_new_name__');
6769
});
70+
71+
test('getCredentials() issues an appropriate request', function(): void {
72+
$mockClientId = uniqid();
73+
$this->endpoint->getCredentials($mockClientId, ['client_id' => '__test_client_id__', 'app_type' => '__test_app_type__']);
74+
75+
expect($this->api->getRequestMethod())->toEqual('GET');
76+
expect($this->api->getRequestUrl())->toStartWith('https://' . $this->api->mock()->getConfiguration()->getDomain() . '/api/v2/clients/' . $mockClientId . '/credentials');
77+
78+
$query = $this->api->getRequestQuery();
79+
expect($query)->toContain('&client_id=__test_client_id__&app_type=__test_app_type__');
80+
});
81+
82+
test('getCredential() issues an appropriate request', function(): void {
83+
$mockClientId = uniqid();
84+
$mockCredentialId = uniqid();
85+
$this->endpoint->getCredential($mockClientId, $mockCredentialId);
86+
87+
expect($this->api->getRequestMethod())->toEqual('GET');
88+
expect($this->api->getRequestUrl())->toStartWith('https://' . $this->api->mock()->getConfiguration()->getDomain() . '/api/v2/clients/' . $mockClientId . '/credentials/' . $mockCredentialId);
89+
});
90+
91+
test('deleteCredential() issues an appropriate request', function(): void {
92+
$mockClientId = uniqid();
93+
$mockCredentialId = uniqid();
94+
$this->endpoint->deleteCredential($mockClientId, $mockCredentialId);
95+
96+
expect($this->api->getRequestMethod())->toEqual('DELETE');
97+
expect($this->api->getRequestUrl())->toEndWith('/api/v2/clients/' . $mockClientId . '/credentials/' . $mockCredentialId);
98+
});
99+
100+
test('createCredentials() issues an appropriate request', function(): void {
101+
$mockCertificate = TokenGenerator::generateRsaKeyPair();
102+
$mockClientId = uniqid();
103+
$mockRequestBody = [
104+
[
105+
'credential_type' => 'public_key',
106+
'name' => 'my pub key',
107+
'pem' => $mockCertificate['cert'],
108+
]
109+
];
110+
111+
$this->endpoint->createCredentials($mockClientId, $mockRequestBody);
112+
113+
expect($this->api->getRequestMethod())->toEqual('POST');
114+
expect($this->api->getRequestUrl())->toEndWith('/api/v2/clients/' . $mockClientId . '/credentials');
115+
116+
$body = $this->api->getRequestBody();
117+
118+
expect($body)
119+
->toBeArray()
120+
->toHaveCount(1);
121+
122+
expect($body[0])
123+
->toBeArray()
124+
->toHaveKeys(['credential_type', 'name', 'pem'])
125+
->credential_type->toEqual('public_key')
126+
->name->toEqual('my pub key')
127+
->pem->toEqual($mockCertificate['cert']);
128+
129+
$body = $this->api->getRequestBodyAsString();
130+
expect($body)->toEqual(json_encode((object) $mockRequestBody));
131+
});

0 commit comments

Comments
 (0)