Zitadel PHP Client 1.3.6 PHP Client for Zitadel

Loading...
Searching...
No Matches
TOTP.php
Go to the documentation of this file.
1<?php
2
3 namespace ZitadelPhpClient\User\SetupTwoFactorAuth;
4
5
6use Exception;
7use chillerlan\QRCode\QRCode;
8
12 class TOTP
13{
14 private array $settings;
15 private int $userid;
16 private string $secret;
17 private string $totpUri;
18
22 public function __construct(array $settings) {
23 $this->settings = $settings;
24 }
25
30 public function setUserId(int $userid) {
31 $this->userid = $userid;
32 }
33
37 public function getURI(): string {
38 return $this->totpUri;
39 }
40
44 public function getSecret(): string {
45 return $this->secret;
46 }
47
51 public function getQRCode(): string {
52 $qrCode = new QRCode;
53 return $qrCode->render($this->totpUri);
54 }
55
61 public function start() {
62 $token = $this->settings["userToken"];
63 $curl = curl_init();
64
65 curl_setopt_array($curl, array(
66 CURLOPT_URL => $this->settings["domain"] . "/v2beta/users/$this->userid/totp",
67 CURLOPT_RETURNTRANSFER => true,
68 CURLOPT_ENCODING => '',
69 CURLOPT_MAXREDIRS => 10,
70 CURLOPT_TIMEOUT => 0,
71 CURLOPT_FOLLOWLOCATION => true,
72 CURLOPT_CUSTOMREQUEST => 'POST',
73 CURLOPT_POSTFIELDS =>"{}",
74 CURLOPT_HTTPHEADER => array(
75 "Content-Type: application/json",
76 "Accept: application/json",
77 "Authorization: Bearer $token"
78 ),
79 ));
80
81 $response = json_decode(curl_exec($curl));
82 curl_close($curl);
83 if(isset($response->code)) {
84 throw new Exception("Error-Code: " . $response->code . " Message: " . $response->message);
85 } else{
86 $this->totpUri = $response->uri;
87 $this->secret = $response->secret;
88 }
89 }
90
95 public function verify($verifyCode): bool
96 {
97 $token = $this->settings["userToken"];
98 $curl = curl_init();
99
100 curl_setopt_array($curl, array(
101 CURLOPT_URL => $this->settings["domain"] . "/v2beta/users/$this->userid/totp/verify",
102 CURLOPT_RETURNTRANSFER => true,
103 CURLOPT_ENCODING => '',
104 CURLOPT_MAXREDIRS => 10,
105 CURLOPT_TIMEOUT => 0,
106 CURLOPT_FOLLOWLOCATION => true,
107 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
108 CURLOPT_CUSTOMREQUEST => 'POST',
109 CURLOPT_POSTFIELDS => "{
110 \"code\": \"$verifyCode\"
111 }",
112 CURLOPT_HTTPHEADER => array(
113 "Content-Type: application/json",
114 "Accept: application/json",
115 "Authorization: Bearer $token"
116 ),
117 ));
118
119 $response = json_decode(curl_exec($curl));
120 curl_close($curl);
121 if (isset($response->code)) {
122 return false;
123 }
124 return true;
125 }
126
127}
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP
Definition TOTP.php:13
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\setUserId
setUserId(int $userid)
Definition TOTP.php:30
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\getURI
getURI()
Definition TOTP.php:37
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\getQRCode
getQRCode()
Definition TOTP.php:51
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\verify
verify($verifyCode)
Definition TOTP.php:95
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\__construct
__construct(array $settings)
Definition TOTP.php:22
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\getSecret
getSecret()
Definition TOTP.php:44
ZitadelPhpClient\User\SetupTwoFactorAuth\TOTP\start
start()
Definition TOTP.php:61
ZitadelPhpClient\User\SetupTwoFactorAuth
Definition Email.php:3