demo.php (3153B)
1 <?php 2 3 session_start(); 4 5 $client_id = "697aca40-17cd-46fc-afb4-74acddff8b01"; 6 $client_secret = "EEM9cycs4fmSwXKqd5PQdKdhDF69wdouh"; 7 $token_endpoint = "http://localhost/oauth2/token"; 8 $authorize_endpoint = "http://localhost/oauth2/authorize"; 9 10 $code = $_GET["code"] ?? null; 11 $state = $_GET["state"] ?? null; 12 $process = []; 13 $info = null; 14 15 if (!empty($code)) { 16 assert($_GET["state"] ?? '' === $_SESSION["state"] ?? ''); 17 unset($_SESSION["state"]); 18 $data = compact('client_id', 'client_secret', 'code'); 19 $options = [ 20 'ssl' => [ 21 "verify_peer"=>false, 22 "verify_peer_name"=>false, 23 ], 24 'http' => [ 25 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 26 'method' => 'POST', 27 'content' => http_build_query($data), 28 ], 29 ]; 30 $context = stream_context_create($options); 31 $result = @file_get_contents("http://localhost/oauth2/token", false, $context); 32 if ($result !== false) { 33 $parsed = json_decode($result, true); 34 $options = [ 35 'ssl' => [ 36 "verify_peer"=>false, 37 "verify_peer_name"=>false, 38 ], 39 'http' => [ 40 'header' => "Authorization: Bearer {$parsed["access_token"]}\r\n", 41 'method' => 'GET' 42 ], 43 ]; 44 $context = stream_context_create($options); 45 $result = @file_get_contents("http://localhost/oauth2/userinfo", false, $context); 46 if ($result !== false) { 47 $info = json_decode($result, true); 48 } else { 49 $info = $parsed; 50 } 51 } 52 } else { 53 $state = bin2hex(random_bytes(24)); 54 $_SESSION["state"] = $state; 55 $process = [ 56 "phone" => "http://localhost/oauth2/authorize?client_id={$client_id}&state={$state}&scope=email+phone-number", 57 "id-document" => "http://localhost/oauth2/authorize?client_id={$client_id}&state={$state}&scope=email+id-document", 58 "both" => "http://localhost/oauth2/authorize?client_id={$client_id}&state={$state}&scope=email+id-document+phone-number", 59 ]; 60 } 61 62 ?><!DOCTYPE html> 63 <html lang="en"> 64 <head> 65 <meta charset="UTF-8"> 66 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 67 <title>Document</title> 68 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"/> 69 </head> 70 <body class="container"> 71 <header></header> 72 <div style="max-width: 24em; margin-left: auto; margin-right: auto;"> 73 <article> 74 <header> 75 <h1>Demo</h1> 76 </header> 77 <?php if(empty($process)): ?> 78 <pre id="data"><code><?= json_encode($info, JSON_PRETTY_PRINT) ?></code></pre> 79 <div role="group"> 80 <a href="/" role="button">Reset</a> 81 </div> 82 <?php else: ?> 83 <?php foreach ($process as $demo => $url): ?> 84 <a role="button" href="<?= $url ?>"> 85 <?= ucfirst($demo) ?> 86 </a> 87 <?php endforeach; ?> 88 <?php endif; ?> 89 </article> 90 </div> 91 </body> 92 </html>