forked from ULCC/moodle-block_echo360_echocenter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_lib.php
519 lines (462 loc) · 15.6 KB
/
oauth_lib.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?php
// This file is part of the Echo360 Moodle Plugin - http://moodle.org/
//
// The Echo360 Moodle Plugin is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Echo360 Moodle Plugin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the Echo360 Moodle Plugin. If not, see <http://www.gnu.org/licenses/>.
//
// This file is originally from http://oauth.googlecode.com/svn/code/php/
// It has been modified to work with the EchoSystem Rest API
//
// The original license is listed below:
//
// The MIT License
//
// Copyright (c) 2007 Andy Smith
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/**
* This file contains functions for calling OAuth protected resources
*
* @package block
* @subpackage echo360_echocenter
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/*
* This is a generic OAuth consumer class
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_oauth_consumer {
public $key;
public $secret;
/**
* Store the key and secret and optionally the callbackurl (not used by Echo360)
*
* @param string - oauth consumer key
* @param string - oauth consumer secret
* @param string - callback url only used for 3 legged oauth
*/
function __construct($key, $secret, $callback_url=NULL) {
$this->key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}
}
/*
* This is a generic OAuth Token class
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_oauth_token {
// access tokens and request tokens
public $key;
public $secret;
/**
* Store the key and secret
*
* @param string - oauth token key
* @param string - oauth token secret
*/
function __construct($key, $secret) {
$this->key = $key;
$this->secret = $secret;
}
/**
* Generates the basic string serialization of a token that a server
* would respond to request_token and access_token calls with
*
* @return string
*/
function to_string() {
return "oauth_token=" . echo360_util::urlencodeRFC3986($this->key) .
"&oauth_token_secret=" . echo360_util::urlencodeRFC3986($this->secret);
}
/**
* Generates the basic string serialization of a token that a server
* would respond to request_token and access_token calls with
*
* @return string
*/
function __toString() {
return $this->to_string();
}
}
/*
* This is a generic class for OAuth signature methods (we use HMACSHA1)
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_oauth_signature_method {
/**
* Generates the basic string serialization of a token that a server
* would respond to request_token and access_token calls with
*
* @param echo360_oauth_request - The request
* @param echo360_oauth_consumer - Contains the secret and key
* @param echo360_oauth_token - Can be null
* @param string - The signature to check
* @return string
*/
public function check_signature(&$request, $consumer, $token, $signature) {
$built = $this->build_signature($request, $consumer, $token);
return $built == $signature;
}
}
/*
* This class implements HMACSHA1 auth
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_oauth_signature_method_hmacsha1 extends echo360_oauth_signature_method {/*{{{*/
/**
* Return the name of this signature method. This is included as a paramenter
* in OAuth requests so it has to be exact.
*
* @return string
*/
function get_name() {
return "HMAC-SHA1";
}
/**
* Return the request signature encrypted with hash_hmac.
*
* @param echo360_oauth_request - The OAuth request we are creating
* @param echo360_oauth_consumer - The OAuth consumer contains the token and secrets
* @param OAuthToken - The OAuth token - can be empty for 2 legged OAuth
* @return string
*/
public function build_signature($request, $consumer, $token) {
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;
$key_parts = array(
$consumer->secret,
($token) ? $token->secret : ""
);
$key_parts = array_map(array('echo360_util','urlencodeRFC3986'), $key_parts);
$key = implode('&', $key_parts);
return base64_encode( hash_hmac('sha1', $base_string, $key, true));
}
}
/*
* This class is used to construct an OAuth request
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_oauth_request {
private $parameters;
private $http_method;
private $http_url;
public $base_string;
public static $version = '1.0';
/**
* Constructor for this class.
*
* @param string - "GET|POST|DELETE|PUT" (for REST)
* @param string - The URL of the request
* @param array - Array of parameters for the request
*/
function __construct($http_method, $http_url, $parameters=NULL) {
@$parameters or $parameters = array();
$this->parameters = $parameters;
$this->http_method = $http_method;
$this->http_url = $http_url;
}
/**
* Pretty much a helper function to set up the request
*
* @param echo360_oauth_consumer - Contains the OAuth secret and key
* @param echo360_oauth_token - The token for the request (can be null)
* @param string - "GET|POST|DELETE|PUT" (for REST)
* @param string - The URL of the request
* @param array - Array of parameters for the request
* @return echo360_oauth_request
*/
public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL) {
@$parameters or $parameters = array();
$defaults = array("oauth_version" => echo360_oauth_request::$version,
"oauth_nonce" => echo360_oauth_request::generate_nonce(),
"oauth_timestamp" => echo360_oauth_request::generate_timestamp(),
"oauth_consumer_key" => $consumer->key);
$parameters = array_merge($defaults, $parameters);
if ($token) {
$parameters['oauth_token'] = $token->key;
}
return new echo360_oauth_request($http_method, $http_url, $parameters);
}
/**
* Add a parameter to the request
*
* @param string - Param name
* @param string - Param value
*/
public function set_parameter($name, $value) {
$this->parameters[$name] = $value;
}
/**
* Get a parameter from the request
*
* @param string - Param name
* @return string
*/
public function get_parameter($name) {
return $this->parameters[$name];
}
/**
* Get a list of request parameters
*
* @return array
*/
public function get_parameters() {
return $this->parameters;
}
/**
* Returns the normalized parameters of the request
*
* This will be all (except oauth_signature) parameters,
* sorted first by key, and if duplicate keys, then by
* value.
*
* The returned string will be all the key=value pairs
* concated by &.
*
* @return string
*/
public function get_signable_parameters() {
// Grab all parameters
$params = $this->parameters;
// Remove oauth_signature if present
if (isset($params['oauth_signature'])) {
unset($params['oauth_signature']);
}
// Urlencode both keys and values
$keys = array_map(array('echo360_util', 'urlencodeRFC3986'), array_keys($params));
$values = array_map(array('echo360_util', 'urlencodeRFC3986'), array_values($params));
$params = array_combine($keys, $values);
// Sort by keys (natsort)
uksort($params, 'strnatcmp');
// Generate key=value pairs
$pairs = array();
foreach ($params as $key=>$value ) {
if (is_array($value)) {
// If the value is an array, it's because there are multiple
// with the same key, sort them, then add all the pairs
natsort($value);
foreach ($value as $v2) {
$pairs[] = $key . '=' . $v2;
}
} else {
$pairs[] = $key . '=' . $value;
}
}
// Return the pairs, concated with &
return implode('&', $pairs);
}
/**
* Returns the base string of this request
*
* The base string defined as the method, the url
* and the parameters (normalized), each urlencoded
* and the concated with &.
*/
public function get_signature_base_string() {
$parts = array(
$this->get_normalized_http_method(),
$this->get_normalized_http_url(),
$this->get_signable_parameters()
);
$parts = array_map(array('echo360_util', 'urlencodeRFC3986'), $parts);
return implode('&', $parts);
}
/**
* Just uppercases the http method
*
* @return string
*/
public function get_normalized_http_method() {
return strtoupper($this->http_method);
}
/**
* Parses the url and rebuilds it to be
* scheme://host/path
*
* @return string
*/
public function get_normalized_http_url() {
$parts = parse_url($this->http_url);
$port = @$parts['port'];
$scheme = $parts['scheme'];
$host = $parts['host'];
$path = @$parts['path'];
$port or $port = ($scheme == 'https') ? '443' : '80';
if (($scheme == 'https' && $port != '443')
|| ($scheme == 'http' && $port != '80')) {
$host = "$host:$port";
}
return "$scheme://$host$path";
}
/**
* Builds a url usable for a GET request.
* ie puts the params on the query string
*
* @return string
*/
public function to_url() {
$out = $this->get_normalized_http_url() . "?";
$out .= $this->to_postdata();
return $out;
}
/**
* Builds the data one would send in a POST request
*
* @return string
*/
public function to_postdata() {
$total = array();
uksort($this->parameters, array('echo360_util', 'echo360cmp'));
foreach ($this->parameters as $k => $v) {
$total[] = echo360_util::urlencodeRFC3986($k) . "=" . echo360_util::urlencodeRFC3986($v);
}
$out = implode("&", $total);
return $out;
}
/**
* Builds the Authorization: header
*
* @return string
*/
public function to_header() {
$out ='Authorization: OAuth ';
$first = true;
$total = array();
foreach ($this->parameters as $k => $v) {
if (substr($k, 0, 5) != "oauth") {
continue;
}
if (!$first) {
$out .= ',';
}
$out .= echo360_util::urlencodeRFC3986($k) . '="' . echo360_util::urlencodeRFC3986($v) . '"';
$first = false;
}
return $out;
}
/**
* Returns this url as a string
*
* @return string
*/
public function __toString() {
return $this->to_url();
}
/**
* Appends the OAuth signature parameter based on the values of all the other variables
*
* @param string - HTTP method
* @param echo360_oauth_consumer - Contains the consumer key and secret
* @param echo360_oauth_token - Optional - for 3 legged oauth
*/
public function sign_request($signature_method, $consumer, $token) {
$this->set_parameter("oauth_signature_method", $signature_method->get_name());
$signature = $this->build_signature($signature_method, $consumer, $token);
$this->set_parameter("oauth_signature", $signature);
}
/**
* Build the string that is encrypted to sign a request
*
* @param string - HTTP method
* @param echo360_oauth_consumer - Contains the consumer key and secret
* @param echo360_oauth_token - Optional - for 3 legged oauth
* @return string
*/
public function build_signature($signature_method, $consumer, $token) {
$signature = $signature_method->build_signature($this, $consumer, $token);
return $signature;
}
/**
* Util function: current timestamp
*
* @return int
*/
private static function generate_timestamp() {
return time();
}
/**
* Util function: current nonce
*
* @return string
*/
private static function generate_nonce() {
$mt = microtime();
$rand = mt_rand();
return md5($mt . $rand); // md5s look nicer than numbers
}
}
/*
* This class contains some util functions for oauth
*
* @copyright 2011 Echo360 Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class echo360_util {
/**
* Special URL encoding for OAuth - includes + and ~ escaping
*
* @param string
* @return string
*/
public static function urlencodeRFC3986($string) {
return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode($string)));
}
/**
* EchoSystem expects the first parameter to always be redirecturl (for seamless login)
* So if you sort the parameters alphabetically, you need to make a special case for
* redirecturl.
*
* @param string
* @param string
* @return int
*/
public static function echo360cmp($a, $b) {
if ($a === 'redirecturl' && $a !== $b) {
return -1;
}
if ($b === 'redirecturl' && $a !== $b) {
return 1;
}
return strnatcmp($a, $b);
}
}
?>