-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.php
More file actions
52 lines (44 loc) · 1.55 KB
/
Copy pathcallback.php
File metadata and controls
52 lines (44 loc) · 1.55 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
<?php
session_start();
if (!isset($_GET['code'])) {
echo "<h2>Login Failed</h2>";
echo "<p>No authorization code received.</p>";
exit;
}
// Validate the state parameter to protect against CSRF attacks
if (!isset($_SESSION['oauth_state'])) {
echo "<h2>Session State Not Set</h2>";
exit;
}
$authCode = $_GET['code'];
$clientId = 'b8aaf9d8-5190-4df1-a674-c6552907559b';
$clientSecret = 'my_client_secret';
$tokenUrl = 'http://local.oauth.com:5001/oauth/token';
$redirectUri = 'http://local.test.com/callback.php';
// Exchange the authorization code for an access token
$data = http_build_query([
'grant_type' => 'authorization_code',
'code' => $authCode,
'client_id' => $clientId,
'client_secret' => $clientSecret,
'redirect_uri' => $redirectUri,
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$tokenData = json_decode($response, true);
print_r($tokenData);exit;
if (isset($tokenData['access_token'])) {
echo "<h2>Access Token Received</h2>";
echo "<p>Your Access Token: <strong>" . htmlspecialchars($tokenData['access_token']) . "</strong></p>";
// Optionally store the access token in the session or localStorage
$_SESSION['access_token'] = $tokenData['access_token'];
} else {
echo "<h2>Token Exchange Failed</h2>";
echo "<p>Error: " . htmlspecialchars($tokenData['error'] ?? 'Unknown error') . "</p>";
}
?>