-
Notifications
You must be signed in to change notification settings - Fork 335
/
auth.php
112 lines (98 loc) · 4.29 KB
/
auth.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
<?php
/**
* Demonstration of the various OAuth flows. You would typically do this
* when an unknown user is first using your application. Instead of storing
* the token and secret in the session you would probably store them in a
* secure database with their logon details for your website.
*
* When the user next visits the site, or you wish to act on their behalf,
* you would use those tokens and skip this entire process.
*
* The Sign in with Twitter flow directs users to the oauth/authenticate
* endpoint which does not support the direct message permission. To obtain
* direct message permissions you must use the "Authorize Application" flows.
*
* Instructions:
* 1) If you don't have one already, create a Twitter application on
* https://dev.twitter.com/apps
* 2) From the application details page copy the consumer key and consumer
* secret into the place in this code marked with (YOUR_CONSUMER_KEY
* and YOUR_CONSUMER_SECRET)
* 3) Visit this page using your web browser.
*
* @author themattharris
*/
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
));
$here = tmhUtilities::php_self();
session_start();
function outputError($tmhOAuth) {
echo 'Error: ' . $tmhOAuth->response['response'] . PHP_EOL;
tmhUtilities::pr($tmhOAuth);
}
// reset request?
if ( isset($_REQUEST['wipe'])) {
session_destroy();
header("Location: {$here}");
// already got some credentials stored?
} elseif ( isset($_SESSION['access_token']) ) {
$tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
if ($code == 200) {
$resp = json_decode($tmhOAuth->response['response']);
echo $resp->screen_name;
} else {
outputError($tmhOAuth);
}
// we're being called back by Twitter
} elseif (isset($_REQUEST['oauth_verifier'])) {
$tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
$tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
));
if ($code == 200) {
$_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
unset($_SESSION['oauth']);
header("Location: {$here}");
} else {
outputError($tmhOAuth);
}
// start the OAuth dance
} elseif ( isset($_REQUEST['authenticate']) || isset($_REQUEST['authorize']) ) {
$callback = isset($_REQUEST['oob']) ? 'oob' : $here;
$params = array(
'oauth_callback' => $callback
);
if (isset($_REQUEST['force_write'])) :
$params['x_auth_access_type'] = 'write';
elseif (isset($_REQUEST['force_read'])) :
$params['x_auth_access_type'] = 'read';
endif;
$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
if ($code == 200) {
$_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
$method = isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
$force = isset($_REQUEST['force']) ? '&force_login=1' : '';
$authurl = $tmhOAuth->url("oauth/{$method}", '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
echo '<p>To complete the OAuth flow follow this URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
} else {
outputError($tmhOAuth);
}
}
?>
<ul>
<li><a href="?authenticate=1">Sign in with Twitter</a></li>
<li><a href="?authenticate=1&force=1">Sign in with Twitter (force login)</a></li>
<li><a href="?authorize=1">Authorize Application (with callback)</a></li>
<li><a href="?authorize=1&oob=1">Authorize Application (oob - pincode flow)</a></li>
<li><a href="?authorize=1&force_read=1">Authorize Application (with callback) (force read-only permissions)</a></li>
<li><a href="?authorize=1&force_write=1">Authorize Application (with callback) (force read-write permissions)</a></li>
<li><a href="?authorize=1&force=1">Authorize Application (with callback) (force login)</a></li>
<li><a href="?wipe=1">Start Over and delete stored tokens</a></li>
</ul>