-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
184 lines (172 loc) · 4.78 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
require __DIR__ . '/../autoload.php.dist';
use PhoAuth\Utils, PhoAuth\Signer;
/**
* A quick abstraction for storing data as JSON. Note: will not scale :)
*/
class DataFile
{
/**
* The name of the data file.
*
* @var string
*/
const DATA_FILE = 'data.json';
/**
* Get the path to the file.
*
* @static
* @return string
*/
public static function getPath()
{
return __DIR__ . DIRECTORY_SEPARATOR . self::DATA_FILE;
}
/**
* Read the file, decode the JSON, and return the results.
*
* @static
* @return mixed
*/
public static function read()
{
return json_decode(file_get_contents(self::getPath()));
}
/**
* Encode the given data as JSON and write it to the file.
*
* @static
*
* @param $data
*
* @return void
*/
public static function write($data)
{
file_put_contents(self::getPath(), json_encode($data));
}
}
$signer = new Signer(
Utils::getServerScheme(),
Utils::getServerMethod(),
Utils::getServerHost(),
Utils::getServerPort(),
Utils::getServerRequestUri(),
Utils::getServerHeaders(),
Utils::getServerInputBody()
);
$signer->setTimestampAndNonceValidator(function($timestamp, $nonce)
{
if ($timestamp < time() - 500 || $timestamp > time() + 500) {
return false;
}
$data = DataFile::read();
foreach ($data->nonces as $combo) {
if ($combo->value == $nonce && $timestamp == $combo->timestamp) {
return false;
}
}
$data->nonces[] = array(
'value' => $nonce,
'timestamp' => (int)$timestamp,
);
DataFile::write($data);
return true;
});
$signer->setConsumerSecretFinder(function($consumer)
{
$data = DataFile::read();
foreach ($data->consumers as $potential) {
if ($potential->key == $consumer) {
return $potential->secret;
}
}
throw new \Exception(
'Could not find consumer for key: ' . var_export($consumer, true)
);
});
$normalizedPath = preg_replace('#/+$#', '', $signer->getPath());
if ($normalizedPath == '/request-token') {
$signer->setTokenSecretFinder(function($consumer, $token)
{
return '';
});
} else {
$signer->setTokenSecretFinder(function($consumer, $token)
{
$data = DataFile::read();
foreach ($data->tokens as $potential) {
if ($potential->consumer == $consumer &&
$potential->key == $token
) {
return $potential->secret;
}
}
throw new \Exception(
'Could not find token for consumer: ' .
var_export($consumer, true) .
' and key: ' .
var_export($token, true)
);
});
}
if ($signer->getSignature() != $signer->getParam('oauth_signature')) {
echo '<h1>OAuth signatures do not match</h1>';
echo '<b>Expected: </b>' . $signer->getSignature(false);
echo '<br/>';
echo '<b>Actual: </b>' . $signer->getParam('oauth_signature');
exit;
}
if ($normalizedPath == '/request-token') {
$newToken = sha1(uniqid(microtime()));
$newTokenSecret = sha1(uniqid(microtime()));
$data = DataFile::read();
$data->tokens[] = array(
'type' => 'request',
'key' => $newToken,
'secret' => $newTokenSecret,
'consumer' => $signer->getParam('oauth_consumer_key'),
);
DataFile::write($data);
echo http_build_query(
array(
'oauth_token' => $newToken,
'oauth_token_secret' => $newTokenSecret,
'oauth_callback_confirmed' => 1,
)
);
} elseif ($normalizedPath == '/direct-access-token') {
$data = DataFile::read();
$validUserFound = false;
foreach ($data->users as $user) {
if ($user->username == $signer->getParam('username') &&
$user->password == $signer->getParam('password')
) {
$validUserFound = true;
break;
}
}
if (!$validUserFound) {
echo '<h1>Invalid username or password</h1>';
exit;
}
$newToken = sha1(uniqid(microtime()));
$newTokenSecret = sha1(uniqid(microtime()));
$data = DataFile::read();
$data->tokens[] = array(
'type' => 'access',
'key' => $newToken,
'secret' => $newTokenSecret,
'consumer' => $signer->getParam('oauth_consumer_key'),
);
DataFile::write($data);
echo http_build_query(
array(
'oauth_token' => $newToken,
'oauth_token_secret' => $newTokenSecret,
)
);
} else {
echo '<h1>Success! You are seeing this because you made it through ' .
'the OAuth gauntlet.</h1>';
}