Zitadel PHP Client 1.3.6
PHP Client for Zitadel
Loading...
Searching...
No Matches
IDP.php
Go to the documentation of this file.
1<?php
2
4
5use Exception;
6
10class IDP
11{
12 private array $settings;
13 private int $userid;
14 private string $idpId;
15 private string $idpToken;
16 private string $idpIntentId;
17 private string $successUrl;
18 private string $failureUrl;
19 private string $authUrl;
20 private string $idpUserId;
21 private string $idpUserName;
22 private string $idpAccessToken;
23 private string $idpRawInformation;
24 private string $idpEmail;
25 private bool $isIdpEmailVerified;
26 private string $idpPicture;
27 private string $idpProfile;
28
32 public function __construct(array $settings) {
33 $this->settings = $settings;
34 }
35
40 public function setUserId(int $userid) {
41 $this->userid = $userid;
42 }
43
48 public function setIdpId(string $idpId) {
49 $this->idpId = $idpId;
50 }
51
56 public function setIdpUserId(string $idpUserId) {
57 $this->idpUserId = $idpUserId;
58 }
59
64 public function setIdpIntentId(string $idpIntentId) {
65 $this->idpIntentId = $idpIntentId;
66 }
67
72 public function setIdpToken(string $idpToken) {
73 $this->idpToken = $idpToken;
74 }
75
80 public function setSuccessUrl(string $successUrl) {
81 $this->successUrl = $successUrl;
82 }
83
88 public function setFailureUrl(string $failureUrl) {
89 $this->failureUrl = $failureUrl;
90 }
91
95 public function getAuthUrl(): string {
96 return $this->authUrl;
97 }
98
102 public function getAccessToken(): string {
103 return $this->idpAccessToken;
104 }
105
109 public function getIdpUserId(): string {
110 return $this->idpUserId;
111 }
112
116 public function getIdpUserName(): string {
117 return $this->idpUserName;
118 }
119
123 public function getIdpRawInformation(): string {
124 return $this->idpRawInformation;
125 }
126
130 public function getIdpEmail(): string {
131 return $this->idpEmail;
132 }
133
137 public function isIdpEmailVerified(): bool {
138 return $this->isIdpEmailVerified;
139 }
140
144 public function getIdpPicture(): string {
145 return $this->idpPicture;
146 }
147
151 public function getIdpProfile(): string {
152 return $this->idpProfile;
153 }
154
159 public function startFlow() {
160 $token = $this->settings["userToken"];
161 $curl = curl_init();
162 $request = array(
163 "idpId" => $this->idpId,
164 "urls" => array(
165 "successUrl" => $this->successUrl,
166 "failureUrl" => $this->failureUrl
167 )
168 );
169 curl_setopt_array($curl, array(
170 CURLOPT_URL => $this->settings["domain"] . "/v2beta/idp_intents",
171 CURLOPT_RETURNTRANSFER => true,
172 CURLOPT_ENCODING => '',
173 CURLOPT_MAXREDIRS => 10,
174 CURLOPT_TIMEOUT => 0,
175 CURLOPT_FOLLOWLOCATION => true,
176 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
177 CURLOPT_CUSTOMREQUEST => 'POST',
178 CURLOPT_POSTFIELDS => json_encode($request),
179 CURLOPT_HTTPHEADER => array(
180 "Content-Type: application/json",
181 "Accept: application/json",
182 "Authorization: Bearer $token"
183 ),
184 ));
185
186 $response = json_decode(curl_exec($curl));
187 if(isset($response->code)) {
188 throw new Exception("Error-Code: " . $response->code . " Message: " . $response->message);
189 } else {
190 $this->authUrl = $response->authUrl;
191 }
192 curl_close($curl);
193 }
194
199 public function fetchIdpData() {
200 $token = $this->settings["userToken"];
201 $curl = curl_init();
202 curl_setopt_array($curl, array(
203 CURLOPT_URL => $this->settings["domain"] . "/v2beta/idp_intents/$this->idpIntentId",
204 CURLOPT_RETURNTRANSFER => true,
205 CURLOPT_ENCODING => '',
206 CURLOPT_MAXREDIRS => 10,
207 CURLOPT_TIMEOUT => 0,
208 CURLOPT_FOLLOWLOCATION => true,
209 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
210 CURLOPT_CUSTOMREQUEST => 'POST',
211 CURLOPT_POSTFIELDS => "{
212 \"idpIntentToken\": \"$this->idpToken\"
213 }",
214 CURLOPT_HTTPHEADER => array(
215 "Content-Type: application/json",
216 "Accept: application/json",
217 "Authorization: Bearer $token"
218 ),
219 ));
220
221 $response = json_decode(curl_exec($curl));
222 if(isset($response->code)) {
223 throw new Exception("Error-Code: " . $response->code . " Message: " . $response->message);
224 } else {
225 $this->idpAccessToken = $response->idpInformation->oauth->accessToken;
226 $this->idpUserId = $response->idpInformation->userId;
227 $this->idpUserName = $response->idpInformation->userName;
228 $this->idpEmail = $response->idpInformation->rawInformation->email;
229 $this->isIdpEmailVerified = $response->idpInformation->rawInformation->email_verified;
230 $this->idpPicture = $response->idpInformation->rawInformation->picture;
231 $this->idpProfile = $response->idpInformation->rawInformation->profile;
232 $this->idpRawInformation = json_encode($response->idpInformation->rawInformation);
233 }
234 curl_close($curl);
235 }
236
241 public function linkIdpToUser() {
242 $token = $this->settings["serviceUserToken"];
243 $request = array(
244 "idpLink" => array(
245 "idpId" => $this->idpId,
246 "userId" => $this->idpUserId,
247 "userName" => $this->idpUserName
248 )
249 );
250 $curl = curl_init();
251 curl_setopt_array($curl, array(
252 CURLOPT_URL => $this->settings["domain"] . "/v2beta/users/$this->userid/links",
253 CURLOPT_RETURNTRANSFER => true,
254 CURLOPT_ENCODING => '',
255 CURLOPT_MAXREDIRS => 10,
256 CURLOPT_TIMEOUT => 0,
257 CURLOPT_FOLLOWLOCATION => true,
258 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
259 CURLOPT_CUSTOMREQUEST => 'POST',
260 CURLOPT_POSTFIELDS => json_encode($request),
261 CURLOPT_HTTPHEADER => array(
262 "Content-Type: application/json",
263 "Accept: application/json",
264 "Authorization: Bearer $token"
265 ),
266 ));
267
268 $response = json_decode(curl_exec($curl));
269 if(isset($response->code)) {
270 throw new Exception("Error-Code: " . $response->code . " Message: " . $response->message);
271 }
272 curl_close($curl);
273 }
274}
setIdpToken(string $idpToken)
Definition IDP.php:72
setUserId(int $userid)
Definition IDP.php:40
setIdpIntentId(string $idpIntentId)
Definition IDP.php:64
setSuccessUrl(string $successUrl)
Definition IDP.php:80
__construct(array $settings)
Definition IDP.php:32
setIdpUserId(string $idpUserId)
Definition IDP.php:56
setIdpId(string $idpId)
Definition IDP.php:48
setFailureUrl(string $failureUrl)
Definition IDP.php:88