Skip to content

Commit 8f68349

Browse files
authored
Merge pull request #100 from fschmtt/add-user-credentials-endpoint
Add user credentials endpoint
2 parents 1008856 + 653ea57 commit 8f68349

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/Resource/Users.php

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Fschmtt\Keycloak\Resource;
66

7+
use Fschmtt\Keycloak\Collection\CredentialCollection;
78
use Fschmtt\Keycloak\Collection\GroupCollection;
89
use Fschmtt\Keycloak\Collection\RoleCollection;
910
use Fschmtt\Keycloak\Collection\UserCollection;
@@ -216,4 +217,18 @@ public function executeActionsEmail(string $realm, string $userId): void
216217
)
217218
);
218219
}
220+
221+
public function credentials(string $realm, string $userId): CredentialCollection
222+
{
223+
return $this->queryExecutor->executeQuery(
224+
new Query(
225+
'/admin/realms/{realm}/users/{userId}/credentials',
226+
CredentialCollection::class,
227+
[
228+
'realm' => $realm,
229+
'userId' => $userId,
230+
]
231+
)
232+
);
233+
}
219234
}

tests/Integration/Resource/UsersTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ public function testCreateUserWithPasswordCredential(): void
167167
static::assertNull($user);
168168
}
169169

170+
public function testGetUserCredentials(): void
171+
{
172+
$users = $this->getKeycloak()->users();
173+
$username = Uuid::uuid4()->toString();
174+
175+
$users->create('master', new User(
176+
credentials: new CredentialCollection([$this->createPasswordCredential('p4ssw0rd')]),
177+
username: $username,
178+
));
179+
180+
$user = $this->searchUserByUsername($username);
181+
static::assertInstanceOf(User::class, $user);
182+
183+
$credentials = $users->credentials('master', $user->getId());
184+
static::assertInstanceOf(CredentialCollection::class, $credentials);
185+
186+
$users->delete('master', $user->getId());
187+
188+
$user = $this->searchUserByUsername($username);
189+
static::assertNull($user);
190+
}
191+
170192
private function searchUserByUsername(string $username, string $realm = 'master'): ?User
171193
{
172194
/** @var User|null $user */

tests/Unit/Resource/UsersTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Fschmtt\Keycloak\Test\Unit\Resource;
66

7+
use Fschmtt\Keycloak\Collection\CredentialCollection;
78
use Fschmtt\Keycloak\Collection\GroupCollection;
89
use Fschmtt\Keycloak\Collection\RoleCollection;
910
use Fschmtt\Keycloak\Collection\UserCollection;
@@ -423,4 +424,29 @@ public function testExecuteActionsEmail(): void
423424

424425
$users->executeActionsEmail('test-realm', 'test-user-id');
425426
}
427+
428+
public function testCredentials(): void
429+
{
430+
$query = new Query(
431+
'/admin/realms/{realm}/users/{userId}/credentials',
432+
CredentialCollection::class,
433+
[
434+
'realm' => 'test-realm',
435+
'userId' => 'test-user-id',
436+
],
437+
);
438+
439+
$queryExecutor = $this->createMock(QueryExecutor::class);
440+
$queryExecutor->expects(static::once())
441+
->method('executeQuery')
442+
->with($query)
443+
->willReturn(new CredentialCollection());
444+
445+
$users = new Users(
446+
$this->createMock(CommandExecutor::class),
447+
$queryExecutor,
448+
);
449+
450+
$users->credentials('test-realm', 'test-user-id');
451+
}
426452
}

0 commit comments

Comments
 (0)