-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathuser.php
More file actions
75 lines (57 loc) · 1.85 KB
/
Copy pathuser.php
File metadata and controls
75 lines (57 loc) · 1.85 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
<?php
use VK\OAuth\User\DTO\AuthorizeUrlParams;
use VK\OAuth\User\DTO\TokensParams;
use VK\OAuth\User\User;
require 'vendor/autoload.php';
$clientId = 1234567;
$clientSecret = 'secret';
$redirectUri = 'https://domain.tld';
$state = 'abc';
// @see https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
$verifier = 'very_very_very_very_very_very_big_random_string';
$auth = new User();
$authParams = (new AuthorizeUrlParams(
$clientId,
$verifier,
$redirectUri,
$state,
))->setScope([Scopes::EMAIL, Scopes::PHONE]);
$authUrl = $auth->getAuthorizeUrl($authParams);
echo '1. Copy and paste into your browser\'s address bar and press Enter: ' . $auth->getAuthorizeUrl($authParams) . "\n";
echo "2. Login\n";
echo "3. Copy url from the address bar\n";
echo "4. Paste this option below\n\n";
echo 'url > ';
$handle = fopen ('php://stdin','r');
$url = fgets($handle);
fclose($handle);
$query = parse_url($url, PHP_URL_QUERY);
if (empty($query)) {
echo "[err] empty query in url\n";
exit(1);
}
parse_str($query, $q);
if (empty($q['code'])) {
echo "[err] not found parameter code in url\n";
exit(1);
}
if (empty($q['device_id'])) {
echo "[err] not found parameter device_id in url\n";
exit(1);
}
if (empty($q['state'])) {
echo "[err] not found parameter state in url\n";
exit(1);
}
if ($q['state'] !== $state) {
echo "[err] invalid state value in url, expect {$state}: {$q['state']}\n";
exit(1);
}
echo "code: " . $q['code'] . "\n";
echo "device_id: " . $q['device_id'] . "\n\n";
$tokenParams = new TokensParams($clientId, $verifier, $redirectUri, $q['code'], $q['device_id']);
$token = $auth->getTokens($tokenParams);
echo "refresh_token: " . $token['refresh_token'] . "\n";
echo "access_token: " . $token['access_token'] . "\n";
echo "id_token: " . $token['id_token'] . "\n";
echo "token type: " . $token['token_type'] . "\n";