-
Notifications
You must be signed in to change notification settings - Fork 28
/
oeauth.php
183 lines (143 loc) · 4.09 KB
/
oeauth.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
<?php
require_once "vendor/autoload.php";
require_once "auth.php";
/**
* OpenERP Authentication Provider
*/
class OEAuthProvider extends AuthProvider {
function __construct($url, $db) {
$this->oe = new PhpOeJson\OpenERP($url, $db);
}
/**
* Login with session tokens to resume an existing session
*
*/
public function login_with_tokens($tokens) {
return $this->oe->loginWithSessionId($tokens["oe_session_id"],$tokens["oe_cookie"]);
}
/**
* Returns tokens from the current opened session.
*
* Note that this method will be called only after login
*
*/
public function get_tokens() {
return array("oe_session_id" => $this->oe->session_id,
"oe_cookie" => $this->oe->cookie);
}
/**
* Returns True/False whether credentials are valid and session created.
*
* Note that some sort of a session must be created as we will ask for
* tokens of this session with ``get_tokens()``.
*
*/
public function login($credentials) {
if (!(isset($credentials["login"]) && isset($credentials["password"])))
return False;
return $this->oe->login($credentials["login"], $credentials["password"]);
}
/**
* Closes the current session and returns boolean upon success.
*
*/
public function logout() {
return $this->oe->logout(); // doesn't seem to work how it should
}
}
/**
* Uses $_SESSION variable to store authentication tokens
*/
class SessionAuthTokenStore extends AuthTokenStore {
private $key = "auth_tokens";
function __construct() {
session_start();
}
public function exists() {
return isset($_SESSION[$this->key]);
}
public function set($tokens) {
$_SESSION[$this->key] = $tokens;
}
public function get() {
return isset($_SESSION[$this->key])?$_SESSION[$this->key]:null;
}
}
/**
* Silent JS ajax call to propagate tokens.
*/
class JsAuthWebTransmitter extends AuthWebTransmitter {
function __construct($urls) {
$this->urls = $urls;
}
public function read_tokens_from_request() {
return array("oe_session_id" => $_REQUEST["oe_session_id"],
"oe_cookie" => $_REQUEST["oe_cookie"]);
}
public function js_propagation_code($tokens) {
$url_js_code = array();
foreach($this->urls as $url) {
$url_js_code[] = "'$url'";
};
$url_js_code = implode(", ", $url_js_code);
return "<script type='text/javascript'>
urls = [$url_js_code];
function propagate(url, session_ids, origin) {
var data = {'origin': origin}
$.extend(data, session_ids);
var ajax = {
'type': 'POST',
'dataType': 'text',
'cache': false,
'data': data,
'url': url + '/auth-with-session-id.php',
'xhrFields': {
'withCredentials': true
}
};
return $.ajax(ajax);
}
function propagate_authentication_status(origin) {
var res = $.Deferred();
get_session_ids().then(function(session_ids) {
var deferreds = [];
for(var i=0; i < urls.length; i++) {
url = urls[i];
deferreds.push(propagate(url, session_ids, origin));
}
return $.when(deferreds).done(function() {
res.resolve();
});
});
return res;
}
function get_session_ids() {
var res = $.Deferred();
res.resolve(" . json_encode($tokens, true) . ");
return res;
}
var propagate = propagate_authentication_status(
'http://" . $_SERVER["HTTP_HOST"] . "');
</script>
";
}
};
/**
* Manages an oe connection and it's relation with php session,
* provides also facilities to send authentication to other domains.
*/
class OEAuth extends Auth {
function __construct($url, $db) {
global $config;
$this->authProvider = new OEAuthProvider($url, $db);
$this->authTokenStore = new SessionAuthTokenStore();
$this->authWebTransmitter = new JsAuthWebTransmitter($config["urls"]);
}
/**
* call delegation Delegation to $this->authProvider->oe
*/
function __call($method, $params) {
return $this->authProvider->oe->__call($method, $params);
}
}
?>