forked from fschmtt/keycloak-rest-api-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeycloak.php
More file actions
153 lines (123 loc) · 3.93 KB
/
Copy pathKeycloak.php
File metadata and controls
153 lines (123 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Fschmtt\Keycloak;
use Fschmtt\Keycloak\Http\Client;
use Fschmtt\Keycloak\Http\CommandExecutor;
use Fschmtt\Keycloak\Http\QueryExecutor;
use Fschmtt\Keycloak\OAuth\TokenStorage\InMemory;
use Fschmtt\Keycloak\OAuth\TokenStorageInterface;
use Fschmtt\Keycloak\Resource\AttackDetection;
use Fschmtt\Keycloak\Resource\Clients;
use Fschmtt\Keycloak\Resource\Groups;
use Fschmtt\Keycloak\Resource\Organizations;
use Fschmtt\Keycloak\Resource\Realms;
use Fschmtt\Keycloak\Resource\Resource;
use Fschmtt\Keycloak\Resource\Roles;
use Fschmtt\Keycloak\Resource\ServerInfo;
use Fschmtt\Keycloak\Resource\Users;
use Fschmtt\Keycloak\Serializer\Serializer;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
/**
* @codeCoverageIgnore
*/
class Keycloak
{
private ?string $version = null;
private Client $client;
private Serializer $serializer;
private CommandExecutor $commandExecutor;
private QueryExecutor $queryExecutor;
public function __construct(
private readonly string $baseUrl,
private readonly string $username,
private readonly string $password,
private readonly string $realm = 'master',
private readonly TokenStorageInterface $tokenStorage = new InMemory(),
?ClientInterface $guzzleClient = new GuzzleClient(),
) {
$this->client = new Client($this, $guzzleClient, $this->tokenStorage);
$this->serializer = new Serializer($this->version);
$this->commandExecutor = new CommandExecutor($this->client, $this->serializer);
$this->queryExecutor = new QueryExecutor($this->client, $this->serializer);
}
public function getBaseUrl(): string
{
return $this->baseUrl;
}
public function getUsername(): string
{
return $this->username;
}
public function getPassword(): string
{
return $this->password;
}
public function getVersion(): string
{
$this->fetchVersion();
return $this->version;
}
public function attackDetection(): AttackDetection
{
$this->fetchVersion();
return new AttackDetection($this->commandExecutor, $this->queryExecutor);
}
public function serverInfo(): ServerInfo
{
return new ServerInfo($this->commandExecutor, $this->queryExecutor);
}
public function realms(): Realms
{
$this->fetchVersion();
return new Realms($this->commandExecutor, $this->queryExecutor);
}
public function clients(): Clients
{
$this->fetchVersion();
return new Clients($this->commandExecutor, $this->queryExecutor);
}
public function users(): Users
{
$this->fetchVersion();
return new Users($this->commandExecutor, $this->queryExecutor);
}
public function groups(): Groups
{
$this->fetchVersion();
return new Groups($this->commandExecutor, $this->queryExecutor);
}
public function roles(): Roles
{
$this->fetchVersion();
return new Roles($this->commandExecutor, $this->queryExecutor);
}
public function organizations(): Organizations
{
$this->fetchVersion();
return new Organizations($this->commandExecutor, $this->queryExecutor);
}
/**
* @template T of Resource
* @param class-string<T> $resource
* @return T
*/
public function resource(string $resource): Resource
{
$this->fetchVersion();
return new $resource($this->commandExecutor, $this->queryExecutor);
}
private function fetchVersion(): void
{
if ($this->version) {
return;
}
$this->version = $this->serverInfo()->get()->getSystemInfo()->getVersion();
$this->serializer = new Serializer($this->version);
$this->commandExecutor = new CommandExecutor($this->client, $this->serializer);
}
public function getRealm(): string
{
return $this->realm;
}
}