-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAuthenticatedActiveResourceBase.php
executable file
·93 lines (78 loc) · 2.63 KB
/
AuthenticatedActiveResourceBase.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
<?php
require_once ('ActiveResourceBase.php');
/**
* Extended version of the ActiveResource REST client to automatically sign the request.
* The RoR backend server must use authlogic_api to authenticate the requests.
*
* Usage:
* @code
*
* <?php
*
* require_once ('AuthenticatedActiveResource.php');
*
* class Song extends ActiveResource {
* var $site = 'http://localhost:3000/';
* var $element_name = 'songs';
* var $app_key = 'demo';
* var $app_secret = 'secret';
* }
*
* @endcode
*
* @copyright Copyright 2010 Pascal Hurni
* @license http://www.opensource.org/licenses/mit-license.php Licensed under the MIT License
* @author Pascal Hurni <phi@ruby-reactive.org> http://github.com/phurni
*/
class AuthenticatedActiveResourceBase extends ActiveResourceBase {
/// The application key used to identify all requests
var $app_key = false;
/// The application secret which is used to sign all requests
var $app_secret = false;
/// The parameter name for the application key
var $app_key_param = 'api_key';
/// The parameter name for the signature
var $signature_param = 'signature';
/// The parameter name for the request id
var $request_id_param = 'reqid';
protected function _get ($options) {
return parent::_get($this->_sign_get($options));
}
protected function _post ($options, $data) {
return parent::_post($this->_sign_post($options, $data));
}
protected function _put ($options, $data) {
return parent::_put($this->_sign_post($options, $data));
}
protected function _delete ($options) {
return parent::_delete($this->_sign_get($options));
}
protected function _sign_get($options) {
$reqid = microtime(true);
$sig = $this->_collect_authentic_keys($options, $reqid) . $this->app_secret;
$options[$this->app_key_param] = $this->app_key;
$options[$this->request_id_param] = $reqid;
$options[$this->signature_param] = md5($sig);
return $options;
}
protected function _sign_post($options, $data) {
$reqid = microtime(true);
$sig = $this->_collect_authentic_keys($options, $reqid) . $data . $this->app_secret;
$options[$this->app_key_param] = $this->app_key;
$options[$this->request_id_param] = $reqid;
$options[$this->signature_param] = md5($sig);
return $options;
}
protected function _collect_authentic_keys($options, $reqid) {
$args = $this->_remove_path_keys($options);
$args[$this->app_key_param] = $this->app_key;
$args[$this->request_id_param] = $reqid;
ksort($args);
$request_str = '';
foreach ($args as $key => $value) {
$request_str .= $key . $value;
}
return $request_str;
}
}
?>