-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.php
More file actions
106 lines (82 loc) · 3.4 KB
/
authenticate.php
File metadata and controls
106 lines (82 loc) · 3.4 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
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
<?php
declare(strict_types=1);
error_reporting(E_ALL); // & ~E_NOTICE
ini_set('display_errors', 'On');
require_once __DIR__.'/vendor/autoload.php';
use OkapiAuth\Okapi;
// Start session
session_start();
#header('Content-Type: text/plain');
// Prepare config
$configs = require __DIR__.'/config.php';
$config = false;
$server = false;
if (isset($_GET['oc_server']) && isset($configs[$_GET['oc_server']])) {
$_SESSION['oc_server'] = $_GET['oc_server'];
$config = $configs[$_GET['oc_server']];
}
elseif (isset($_SESSION['oc_server'])) {
$config = $configs[$_SESSION['oc_server']];
}
if ($config) {
$config['callback_uri'] = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
// Create server
$server = new Okapi($config);
}
// Step 4
if (isset($_GET['user']) && $server) {
// Check somebody hasn't manually entered this URL in,
// by checking that we have the token credentials in
// the session.
if ( ! isset($_SESSION['token_credentials'])) {
echo 'No token credentials.';
exit(1);
}
// Retrieve our token credentials. From here, it's play time!
$tokenCredentials = unserialize($_SESSION['token_credentials']);
// // Below is an example of retrieving the identifier & secret
// // (formally known as access token key & secret in earlier
// // OAuth 1.0 specs).
// $identifier = $tokenCredentials->getIdentifier();
// $secret = $tokenCredentials->getSecret();
// Authenticated! Welcome the user
echo '<p>Welcome '.$server->getUserScreenName($tokenCredentials).' @ '.$_SESSION['oc_server'].'</p>';
// This does a second call to the OKAPI! Do only this or the above line.
$user = $server->getUserDetails($tokenCredentials);
echo "\n<p>Details:</p><pre>";
var_dump($user);
echo "</pre>\n";
// Step 3
} elseif (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']) && $server) {
// Retrieve the temporary credentials from step 2
$temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
// Third and final part to OAuth 1.0 authentication is to retrieve token
// credentials (formally known as access tokens in earlier OAuth 1.0
// specs).
$tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']);
// Now, we'll store the token credentials and discard the temporary
// ones - they're irrelevant at this stage.
unset($_SESSION['temporary_credentials']);
$_SESSION['token_credentials'] = serialize($tokenCredentials);
session_write_close();
// Redirect to the user page
header("Location: ${config['callback_uri']}/?user=user");
exit;
// Step 2
} elseif (isset($_GET['go']) && $server) {
// First part of OAuth 1.0 authentication is retrieving temporary credentials.
// These identify you as a client to the server.
$temporaryCredentials = $server->getTemporaryCredentials();
// Store the credentials in the session.
$_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
session_write_close();
// Second part of OAuth 1.0 authentication is to redirect the
// resource owner to the login screen on the server.
$server->authorize($temporaryCredentials);
// Step 1
} else {
// Display links to start process
foreach ($configs as $server => $config) {
echo "<p><a href=\"?go=go&oc_server=$server\">Login with $server</a></p>";
}
}