Skip to content

Commit 3ed74a9

Browse files
committed
fix: backwards compatibility
1 parent 5a27e7c commit 3ed74a9

5 files changed

Lines changed: 81 additions & 13 deletions

File tree

src/Builder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public function build(): Keycloak
6868
throw new BuilderException('Grant type is not set');
6969
}
7070

71+
// @phpstan-ignore method.deprecated
7172
return new Keycloak(
7273
baseUrl: $this->baseUrl,
73-
grantType: $this->grantType,
7474
tokenStorage: $this->tokenStorage,
7575
httpClient: $this->httpClient,
76+
grantType: $this->grantType,
7677
);
7778
}
7879
}

src/Http/Client.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use DateTime;
88
use Fschmtt\Keycloak\Keycloak;
9+
use Fschmtt\Keycloak\OAuth\GrantType;
910
use Fschmtt\Keycloak\OAuth\GrantType\RefreshToken;
1011
use Fschmtt\Keycloak\OAuth\TokenStorageInterface;
1112
use GuzzleHttp\ClientInterface;
@@ -18,11 +19,27 @@
1819
*/
1920
class Client
2021
{
22+
private GrantType $grantType;
23+
2124
public function __construct(
2225
private readonly Keycloak $keycloak,
2326
private readonly ClientInterface $httpClient,
2427
private readonly TokenStorageInterface $tokenStorage,
25-
) {}
28+
) {
29+
if ($grantType = $this->keycloak->getGrantType()) {
30+
$this->grantType = $grantType;
31+
} else {
32+
$this->grantType = GrantType::password(
33+
// @phpstan-ignore method.deprecated
34+
$this->keycloak->getUsername() ?? throw new \InvalidArgumentException('Username must be provided'),
35+
// @phpstan-ignore method.deprecated
36+
$this->keycloak->getPassword() ?? throw new \InvalidArgumentException('Password must be provided'),
37+
'admin-cli',
38+
// @phpstan-ignore method.deprecated
39+
$this->keycloak->getRealm() ?? throw new \InvalidArgumentException('Realm must be provided'),
40+
);
41+
}
42+
}
2643

2744
/**
2845
* @param array<string, mixed> $options
@@ -70,25 +87,25 @@ private function fetchTokens(): array
7087
{
7188
if ($refreshToken = $this->tokenStorage->retrieveRefreshToken()) {
7289
$refreshTokenGrantType = new RefreshToken(
73-
$this->keycloak->getGrantType()->clientId,
90+
$this->grantType->clientId,
7491
$refreshToken->toString(),
75-
$this->keycloak->getGrantType()->clientSecret,
76-
$this->keycloak->getGrantType()->scope,
92+
$this->grantType->clientSecret,
93+
$this->grantType->scope,
7794
);
7895

7996
$response = $this->httpClient->request(
8097
'POST',
81-
$this->keycloak->getBaseUrl() . '/realms/' . $this->keycloak->getGrantType()->realm . '/protocol/openid-connect/token',
98+
$this->keycloak->getBaseUrl() . '/realms/' . $this->grantType->realm . '/protocol/openid-connect/token',
8299
[
83100
'form_params' => $refreshTokenGrantType->toRequestParams(),
84101
],
85102
);
86103
} else {
87104
$response = $this->httpClient->request(
88105
'POST',
89-
$this->keycloak->getBaseUrl() . '/realms/' . $this->keycloak->getGrantType()->realm . '/protocol/openid-connect/token',
106+
$this->keycloak->getBaseUrl() . '/realms/' . $this->grantType->realm . '/protocol/openid-connect/token',
90107
[
91-
'form_params' => $this->keycloak->getGrantType()->toRequestParams(),
108+
'form_params' => $this->grantType->toRequestParams(),
92109
],
93110
);
94111
}

src/Keycloak.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,30 @@ class Keycloak
3434
private CommandExecutor $commandExecutor;
3535
private QueryExecutor $queryExecutor;
3636

37+
/**
38+
* @deprecated tag:v1.0.0 Use the Builder class to create Keycloak instances instead.
39+
* @see \Fschmtt\Keycloak\Builder
40+
*/
3741
public function __construct(
3842
private readonly string $baseUrl,
39-
private readonly GrantType $grantType,
43+
/** @deprecated tag:v1.0.0 Will be removed. */
44+
private readonly ?string $username = null,
45+
/** @deprecated tag:v1.0.0 Will be removed. */
46+
private readonly ?string $password = null,
47+
/** @deprecated tag:v1.0.0 Will be removed. */
48+
private readonly ?string $realm = null,
4049
private readonly TokenStorageInterface $tokenStorage = new InMemory(),
4150
?ClientInterface $httpClient = new GuzzleClient(),
51+
private readonly ?GrantType $grantType = null,
4252
) {
53+
if ($this->username || $this->password || $this->realm) {
54+
trigger_deprecation(
55+
'fschmtt/keycloak-rest-api-client-php',
56+
'v1.0.0',
57+
'Passing a password grant type (username, password and realm) to the Keycloak instance is deprecated. Use Builder::withGrantType() instead.',
58+
);
59+
}
60+
4361
$this->client = new Client($this, $httpClient, $this->tokenStorage);
4462
$this->serializer = new Serializer($this->version);
4563
$this->commandExecutor = new CommandExecutor($this->client, $this->serializer);
@@ -51,7 +69,25 @@ public function getBaseUrl(): string
5169
return $this->baseUrl;
5270
}
5371

54-
public function getGrantType(): GrantType
72+
/**
73+
* @deprecated tag:v1.0.0 Use Keycloak::getGrantType() to access grant type details.
74+
* @see Keycloak::getGrantType()
75+
*/
76+
public function getUsername(): ?string
77+
{
78+
return $this->username;
79+
}
80+
81+
/**
82+
* @deprecated tag:v1.0.0 Use Keycloak::getGrantType() to access grant type details.
83+
* @see Keycloak::getGrantType()
84+
*/
85+
public function getPassword(): ?string
86+
{
87+
return $this->password;
88+
}
89+
90+
public function getGrantType(): ?GrantType
5591
{
5692
return $this->grantType;
5793
}
@@ -63,6 +99,14 @@ public function getVersion(): string
6399
return $this->version;
64100
}
65101

102+
/**
103+
* @deprecated tag:v1.0.0
104+
*/
105+
public function getRealm(): ?string
106+
{
107+
return $this->realm;
108+
}
109+
66110
public function attackDetection(): AttackDetection
67111
{
68112
$this->fetchVersion();

tests/Integration/IntegrationTestBehaviour.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ trait IntegrationTestBehaviour
1414
public static function getKeycloak(): Keycloak
1515
{
1616
if (!self::$keycloak) {
17+
// @phpstan-ignore method.deprecated
1718
self::$keycloak = new Keycloak(
1819
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
19-
GrantType::password('admin', 'admin'),
20+
grantType: GrantType::password('admin', 'admin'),
2021
);
2122
}
2223

tests/Unit/Http/ClientTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class ClientTest extends TestCase
2929

3030
protected function setUp(): void
3131
{
32-
$this->keycloak = new Keycloak('http://keycloak:8080', new Password());
32+
// @phpstan-ignore method.deprecated
33+
$this->keycloak = new Keycloak(
34+
'http://keycloak:8080',
35+
grantType: new Password(),
36+
);
3337
}
3438

3539
public function testAuthorizesBeforeSendingRequest(): void
@@ -154,9 +158,10 @@ public function testAuthenticatesUsingConfiguredRealm(): void
154158

155159
$httpClient = new GuzzleClient(['handler' => $handlerStack]);
156160

161+
// @phpstan-ignore method.deprecated
157162
$keycloak = new Keycloak(
158163
'http://keycloak:8080',
159-
new Password(realm: 'custom-realm'),
164+
grantType: new Password(realm: 'custom-realm'),
160165
);
161166

162167
$client = new Client($keycloak, $httpClient, new InMemoryTokenStorage());

0 commit comments

Comments
 (0)