-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.php
147 lines (108 loc) · 4.28 KB
/
oauth.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<pre>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$conf = include 'conf.php';
$appToken = $conf['appToken'];
$appSecret = $conf['appSecret'];
//first step
//let's get an unauthorized request token
if (count($_REQUEST) == 0) {
$url = 'https://11870.com/services/manage-api/request-token';
$params = array(
'oauth_consumer_key' => $appToken,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_nonce' => uniqid(),
'oauth_version' => '1.0',
'oauth_callback' => sprintf('%s://%s%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']),
);
//creating signing key with appSecret and nothing else since there is no token at this point
$signingKey = rawurlencode($appSecret) . '&';
$output = getOutput($url, $params, $signingKey);
parse_str($output, $values);
if (isset($values['oauth_token']) && isset($values['oauth_token_secret'])) {
$authToken = $values['oauth_token'];
$authTokenSecret = $values['oauth_token_secret'];
//token secret is stored to be used later
$_SESSION['oauth_token_secret'] = $values['oauth_token_secret'];
$url = 'https://11870.com/services/manage-api/authorize';
$authorizeUrl = $url . '?oauth_token=' . $values['oauth_token'];
header('Location: ' . $authorizeUrl);
} else {
var_dump($values);
}
} else if (isset($_REQUEST['oauth_token']) && isset($_REQUEST['oauth_verifier'])) {
$oauthToken = $_REQUEST['oauth_token'];
$oauthVerifier = $_REQUEST['oauth_verifier'];
$url = 'https://11870.com/services/manage-api/access-token';
$params = array(
'oauth_consumer_key' => rawurlencode($appToken),
'oauth_token' => $oauthToken,
'oauth_signature_method' => rawurlencode('HMAC-SHA1'),
'oauth_timestamp' => rawurlencode(time()),
'oauth_nonce' => rawurlencode(uniqid()),
'oauth_version' => rawurlencode('1.0'),
'oauth_verifier' => $oauthVerifier,
);
$signingKey = $appSecret . '&' . $_SESSION['oauth_token_secret'];
$output = getOutput($url, $params, $signingKey);
parse_str($output, $values);
if (isset($values['oauth_token']) && isset($values['oauth_token_secret'])) {
$oauthToken = $values['oauth_token'];
$oauthTokenSecret = $values['oauth_token_secret'];
echo sprintf('Auth success : token : %s', $oauthToken);
} else {
echo 'Auth failed';
}
}
function buildSignatureBaseString($method, $url, $params)
{
$urlElements = parse_url($url);
$signatureParams = array(
rawurlencode(strtoupper($method)),
rawurlencode(sprintf('%s://%s%s', $urlElements['scheme'], $urlElements['host'], $urlElements['path'])),
rawurlencode(getOauthQueryString($params)),
);
return implode('&', $signatureParams);
}
function buildSignature($signingKey, $signatureBaseString)
{
$hash = hash_hmac('sha1', $signatureBaseString, $signingKey, true);
return base64_encode($hash);
}
function getOauthQueryString($params, $separator = '&')
{
$pairs = array();
foreach ($params as $kParams => $vParams) {
$k = rawurlencode(utf8_encode($kParams));
$v = rawurlencode(utf8_encode($vParams));
$pairs[$k] = $k . '=' . $v;
}
return implode('&', $pairs);
}
function getOutput($url, $params, $signingKey)
{
$urlElems = parse_url($url);
ksort($params);
//creating signature params
$signatureBaseString = buildSignatureBaseString('GET', $url, $params);
$signature = buildSignature($signingKey, $signatureBaseString);
var_dump($url, $params, $signingKey, $signatureBaseString, $signature);
//and adding it to params
$params['oauth_signature'] = $signature;
$fullUrl = $url . '?' . getOauthQueryString($params);
$headers = 'Authorization: OAuth realm=""';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'OAuth realm=""',
));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}