-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSODemo.php
288 lines (241 loc) · 10.3 KB
/
SSODemo.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
require __DIR__ . '/vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
// =============================================== SSO SERVICES ========================================================
// =====================================================================================================================
const CLIENT_ID = 'YOUR_CLIENT_ID';
const CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
const REDIRECT_URI = 'YOUR_REDIRECT_URI';
const API_TOKEN = 'YOUR_API_TOKEN';
# required classes
use Pod\Base\Service\Exception\PodException;
use Pod\Base\Service\Exception\ValidationException;
use Pod\Sso\Service\SSOService;
use Pod\Base\Service\BaseInfo;
use Pod\Base\Service\ClientInfo;
// for most service you need set client id and client secret so you can set these parameters once
$clientInfo = new ClientInfo();
$clientInfo->setClientId(CLIENT_ID);
$clientInfo->setClientSecret(CLIENT_SECRET);
// for get Access Token and Refresh Access Token you need set redirect uri
$clientInfo->setRedirectUri(REDIRECT_URI);
# instantiates a SSOService
$SSOService = new SSOService($clientInfo);
// ================================================== get Login Url ====================================================
function getLoginUrl()
{
echo "======================================= get Login Url ===================================" .PHP_EOL;
global $SSOService;
$params = [
// "client_id" => "" , # if it is set in $clientInfo dont need to set again else it is required to set
// "redirect_uri" => "", # if it is set in $clientInfo dont need to set again else it is required to set
## ================================ *Optional Parameters ======================================
// "scope" => "profile", #default: profile , separate scopes with "+" or space
// "response_type" => "code", # #default: code
// "prompt" => "",
// "state" => "",
// "code_challenge" => "",
// "code_challenge_method" => "",
];
try {
$result = $SSOService->getLoginUrl($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// ================================================== get Access Token =================================================
function getAccessToken()
{
echo "======================================= get Access Token ===================================" .PHP_EOL;
global $SSOService;
$params = [
"code" => "put code here" # use code that you receive in url
];
try {
$result = $SSOService->getAccessToken($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// unComment next line to get Access Token
// getAccessToken();
// die;
// =========================================== refresh Access Token ====================================================
function refreshToken()
{
echo "================================ refresh Access Token =====================================" .PHP_EOL;
global $SSOService;
// set refresh token that is received when getting access token
$params = [
"refresh_token" => "put refresh token"
];
try {
$result = $SSOService->refreshAccessToken($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// refreshToken();
// ===================================================== revoke Token ==================================================
function revokeToken()
{
echo "========================================== revoke Token ==================================" . PHP_EOL;
global $SSOService;
# set token and token_type_hint
$params = [
"token" => API_TOKEN,
"token_type_hint" => "put token type to `refresh_token` or `access_token`",
];
try {
$result = $SSOService->revokeToken($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// revokeToken();
// ============================================== get Token Info =======================================================
function TokenInfo()
{
echo "=================================== get Token Info ========================================" . PHP_EOL;
global $SSOService;
# set token and token_type_hint
$params = [
"token" => API_TOKEN,
"token_type_hint" => "put token type to `refresh_token` or `access_token`",
];
try {
$result = $SSOService->getTokenInfo($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// TokenInfo();
// ============================================= OTP Login ===========================================================
// ============================================= Handshake ===========================================================
function handshake() {
echo "======================================= Handshake ======================================" . PHP_EOL;
global $SSOService;
$params = [
## =========================================== *Required Parameters ============================================
"api_token" => API_TOKEN,
"device_uid" => 'put device unique id here', # Device unique id ,maximum 255 character
## =========================================== *Optional Parameters ============================================
"device_name" => 'put device name here',
"device_type" => 'put device type here',
"device_lat" => 'put device latitude here',
"device_lon" => 'put device longitude here',
"device_os" => 'put device os here',
"device_os_version" => 'put device os version here',
];
try {
$result = $SSOService->handshake($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// handshake();
// die;
// ============================================== signature Authorize ================================================
function signatureAuthorize() {
echo "=================================== signature Authorize =====================================" . PHP_EOL;
global $SSOService;
$privateKey = file_get_contents('put your private key file name');
$params = [
## =========================================== *Required Parameters ============================================
"privateKey" => $privateKey,
"keyId" => 'put key id that you receive from handshake here',
// "algorithm" => OPENSSL_ALGO_SHA256,
"identity" => 'put identity [mobile phone number or email address] here',
"response_type" => 'code', # code | token | id_token
## =========================================== *Optional Parameters ============================================
"identityType" => 'put PHONE identity type [PHONE | EMAIL]',
"loginAsUserId" => 'put loginAsUserId',
"state" => 'put state',
"client_id" => 'put client_id',
"redirect_uri" => 'put redirect_uri',
"callback_uri" => 'put callback_uri',
"scope" => 'put scope',
"code_challenge" => 'put code_challenge',
"code_challenge_method" => 'put code_challenge_method',
"referrer" => 'put referrer',
"referrerType" => 'put referrerType',
];
try {
$result = $SSOService->signatureAuthorize($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
// $result = signatureAuthorize();
// die;
// ================================================= verify OTP ======================================================
function verifyOTP($signature) {
echo "====================================== verify OTP =========================================" . PHP_EOL;
global $SSOService;
$params = [
## =========================================== *Required Parameters ============================================
"keyId" => 'put key id that you received from handshake here',
"signature" => $signature,
"otp" => 'put otp that is received from user here',
"identity" => 'put identity [phone or email] here',
];
try {
$result = $SSOService->verifyOTP($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}
$signature = "put signature here";
// verifyOTP($signature);
// die;
// ============================================== get Access Token By OTP ==============================================
function getAccessTokenByOTP()
{
echo "=================================== get Access Token By OTP ===================================" .PHP_EOL;
global $SSOService;
$params = [
"code" => "put code", # use code that you received after verify otp
];
try {
$result = $SSOService->getAccessTokenByOTP($params);
print_r($result);
} catch (ValidationException $e) {
print_r($e->getResult());
print_r($e->getErrorsAsArray());
} catch (PodException $e) {
print_r($e->getResult());
}
}