Skip to content

Commit 48c347f

Browse files
committed
chg: dev: refactor to separate concerns.
1 parent e4e7794 commit 48c347f

File tree

4 files changed

+230
-65
lines changed

4 files changed

+230
-65
lines changed

auth-with-session-id.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
header('Access-Control-Allow-Headers: origin, content-type, accept');
1010

1111
$oe = new OEAuth($config["oe"]["url"], $config["oe"]["dbname"]);
12-
$oe->setSessionInformation($_REQUEST["oe_session_id"], $_REQUEST["oe_cookie"]);
12+
$oe->authTokenStore->set($oe->authWebTransmitter->read_tokens_from_request());
1313

1414
if ($oe->is_auth()) {
1515
echo "AUTH";

auth.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
4+
/**
5+
* Stores tokens
6+
*/
7+
8+
abstract class AuthTokenStore {
9+
10+
abstract public function exists();
11+
abstract public function set($tokens);
12+
abstract public function get();
13+
14+
};
15+
16+
17+
/**
18+
* Authentication Provider
19+
*/
20+
abstract class AuthProvider {
21+
22+
/**
23+
* Login with session tokens to resume an existing session
24+
*
25+
*/
26+
abstract public function login_with_tokens($tokens);
27+
28+
/**
29+
* Returns tokens from the current opened session.
30+
*
31+
* Note that this method will be called only after login
32+
*
33+
*/
34+
abstract public function get_tokens();
35+
36+
/**
37+
* Returns True/False whether credentials are valid and session created.
38+
*
39+
* Note that some sort of a session must be created as we will ask for
40+
* tokens of this session with ``get_tokens()``.
41+
*
42+
*/
43+
abstract public function login($credentials);
44+
45+
46+
/**
47+
* Closes the current session and returns boolean upon success.
48+
*
49+
*/
50+
abstract public function logout();
51+
52+
};
53+
54+
/**
55+
* Propagate tokens
56+
*/
57+
abstract class AuthWebTransmitter {
58+
59+
abstract public function read_tokens_from_request();
60+
abstract public function js_propagation_code($tokens);
61+
62+
};
63+
64+
65+
/**
66+
* Manages an oe connection and it's relation with php session,
67+
* provides also facilities to send authentication to other domains.
68+
*/
69+
abstract class Auth {
70+
71+
private $auth_cache = NULL;
72+
private $auth_cache_dirty = True;
73+
private $enable_propagation = False;
74+
75+
/**
76+
* returns True if login is accepted
77+
*/
78+
79+
public function is_auth() {
80+
if ($this->auth_cache !== NULL &&
81+
$this->auth_cache_dirty === False)
82+
return $this->auth_cache;
83+
84+
$this->auth_cache = $this->authTokenStore->exists() &&
85+
$this->authProvider->login_with_tokens($this->authTokenStore->get());
86+
$this->auth_cache_dirty = False;
87+
return $this->auth_cache;
88+
}
89+
90+
91+
/** authenticating by credential
92+
*
93+
* This function must validate authentication of given credentials
94+
*/
95+
public function authenticate($credentials) {
96+
97+
$login_success = $this->authProvider->login($credentials);
98+
if ($login_success) {
99+
$tokens = $this->authProvider->get_tokens();
100+
$this->authTokenStore->set($tokens);
101+
$this->auth_cache = True;
102+
$this->auth_cache_dirty = False;
103+
$this->enable_propagation = True;
104+
};
105+
return $login_success;
106+
}
107+
108+
/** deauthenticating
109+
*
110+
* This function must unlog current session
111+
*/
112+
public function deauthenticate() {
113+
114+
$this->authProvider->logout();
115+
116+
$this->authTokenStore->set(null); // deleting tokens
117+
$this->auth_cache = False;
118+
$this->auth_cache_dirty = False;
119+
120+
$this->enable_propagation = True;
121+
return True; // logout succeeded
122+
}
123+
124+
/** returns javascript code to define the ``get_session_ids()``
125+
* function and urls variable
126+
*
127+
*/
128+
public function js_code_for_propagate() {
129+
if (!$this->enable_propagation) return "";
130+
131+
$tokens = $this->authTokenStore->get(); // returns NULL if no tokens
132+
return $this->authWebTransmitter->js_propagation_code($tokens);
133+
}
134+
135+
}
136+
137+
?>

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
</style>
5252

53-
<?php echo $oe->js_code; ?>
53+
<?php echo $oe->js_code_for_propagate(); ?>
5454

5555
</head>
5656
<body>

oeauth.php

Lines changed: 91 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,109 @@
11
<?php
22

33
require_once "php-oe-json/openerp.php";
4+
require_once "auth.php";
5+
46

57
/**
6-
* Manages an oe connection and it's relation with php session,
7-
* provides also facilities to send authentication to other domains.
8+
* OpenERP Authentication Provider
89
*/
9-
class OEAuth {
10-
11-
public $js_code = "";
10+
class OEAuthProvider extends AuthProvider {
1211

1312
function __construct($url, $db) {
1413
$this->oe = new OpenERP($url, $db);
15-
$this->_auth_cache = NULL;
16-
$this->_auth_cache_dirty = True;
17-
18-
session_start();
1914
}
2015

21-
public function is_auth() {
22-
if ($this->_auth_cache !== NULL &&
23-
$this->_auth_cache_dirty === False)
24-
return $this->_auth_cache;
25-
26-
$auth = False;
27-
if (isset($_SESSION["oe_session_id"])) {
28-
if ($this->oe->loginWithSessionId($_SESSION["oe_session_id"],$_SESSION["oe_cookie"]))
29-
$auth = True;
30-
}
31-
32-
$this->_auth_cache = $auth;
33-
$this->_auth_cache_dirty = False;
34-
return $this->_auth_cache;
16+
/**
17+
* Login with session tokens to resume an existing session
18+
*
19+
*/
20+
public function login_with_tokens($tokens) {
21+
return $this->oe->loginWithSessionId($tokens["oe_session_id"],$tokens["oe_cookie"]);
3522
}
3623

37-
public function setSessionInformation($oe_session_id, $oe_cookie) {
38-
$_SESSION["oe_session_id"] = $oe_session_id;
39-
$_SESSION["oe_cookie"] = $oe_cookie;
24+
/**
25+
* Returns tokens from the current opened session.
26+
*
27+
* Note that this method will be called only after login
28+
*
29+
*/
30+
public function get_tokens() {
31+
return array("oe_session_id" => $this->oe->session_id,
32+
"oe_cookie" => $this->oe->cookie);
4033
}
4134

42-
/** authenticating by credential
35+
/**
36+
* Returns True/False whether credentials are valid and session created.
37+
*
38+
* Note that some sort of a session must be created as we will ask for
39+
* tokens of this session with ``get_tokens()``.
4340
*
44-
* This function must validate authentication of given credentials
4541
*/
46-
public function authenticate($credentials) {
47-
42+
public function login($credentials) {
4843
if (!(isset($credentials["login"]) && isset($credentials["password"])))
4944
return False;
5045

51-
$this->oe->login($credentials["login"], $credentials["password"]);
52-
53-
if ($this->oe->authenticated) {
54-
$this->setSessionInformation($this->oe->session_id, $this->oe->cookie);
55-
$this->_auth_cache = True;
56-
$this->_auth_cache_dirty = False;
57-
};
58-
$this->js_code = $this->js_code_for_propagate();
59-
return $this->oe->authenticated;
46+
return $this->oe->login($credentials["login"], $credentials["password"]);
6047
}
6148

62-
/** deauthenticating
49+
/**
50+
* Closes the current session and returns boolean upon success.
6351
*
64-
* This function must unlog current session
6552
*/
66-
public function deauthenticate() {
53+
public function logout() {
54+
return $this->oe->logout(); // doesn't seem to work how it should
55+
}
6756

68-
$this->oe->logout(); // doesn't seem to work how it should
57+
}
6958

70-
unset($_SESSION["oe_session_id"]);
71-
unset($_SESSION["oe_cookie"]);
72-
$this->_auth_cache = False;
73-
$this->_auth_cache_dirty = False;
59+
/**
60+
* Uses $_SESSION variable to store authentication tokens
61+
*/
62+
class SessionAuthTokenStore extends AuthTokenStore {
7463

75-
$this->js_code = $this->js_code_for_propagate();
76-
return True; // logout succeeded
64+
private $key = "auth_tokens";
65+
66+
function __construct() {
67+
session_start();
7768
}
7869

79-
/** returns javascript code to define the ``get_session_ids()``
80-
* function and urls variable
81-
*
82-
*/
83-
public function js_code_for_propagate() {
70+
public function exists() {
71+
return isset($_SESSION[$this->key]);
72+
}
8473

85-
global $config;
74+
public function set($tokens) {
75+
$_SESSION[$this->key] = $tokens;
76+
}
77+
78+
public function get() {
79+
return isset($_SESSION[$this->key])?$_SESSION[$this->key]:null;
80+
}
81+
82+
}
83+
84+
/**
85+
* Silent JS ajax call to propagate tokens.
86+
*/
87+
class JsAuthWebTransmitter extends AuthWebTransmitter {
88+
89+
function __construct($urls) {
90+
$this->urls = $urls;
91+
}
92+
93+
public function read_tokens_from_request() {
94+
return array("oe_session_id" => $_REQUEST["oe_session_id"],
95+
"oe_cookie" => $_REQUEST["oe_cookie"]);
96+
}
8697

87-
$oe_session_id = isset($_SESSION["oe_session_id"])?$_SESSION["oe_session_id"]:"";
88-
$oe_cookie = isset($_SESSION["oe_cookie"])?$_SESSION["oe_cookie"]:"";
98+
public function js_propagation_code($tokens) {
8999

90100
$url_js_code = array();
91-
foreach($config["urls"] as $url) {
101+
foreach($this->urls as $url) {
92102
$url_js_code[] = "'$url'";
93103
};
94104
$url_js_code = implode(", ", $url_js_code);
95105

96-
return "<script type='text/javascript'>\n
106+
return "<script type='text/javascript'>
97107
98108
urls = [$url_js_code];
99109
@@ -131,8 +141,7 @@ function propagate_authentication_status(origin) {
131141
132142
function get_session_ids() {
133143
var res = $.Deferred();
134-
res.resolve({oe_session_id: '" . $oe_session_id . "',
135-
oe_cookie: '" . $oe_cookie . "'});
144+
res.resolve(" . json_encode($tokens, true) . ");
136145
return res;
137146
}
138147
@@ -144,11 +153,30 @@ function get_session_ids() {
144153

145154
}
146155

156+
};
157+
158+
159+
160+
161+
/**
162+
* Manages an oe connection and it's relation with php session,
163+
* provides also facilities to send authentication to other domains.
164+
*/
165+
class OEAuth extends Auth {
166+
167+
function __construct($url, $db) {
168+
global $config;
169+
$this->authProvider = new OEAuthProvider($url, $db);
170+
$this->authTokenStore = new SessionAuthTokenStore();
171+
$this->authWebTransmitter = new JsAuthWebTransmitter($config["urls"]);
172+
}
173+
174+
147175
/**
148-
* call delegation Delegation to $this->oe
176+
* call delegation Delegation to $this->authProvider->oe
149177
*/
150178
function __call($method, $params) {
151-
return $this->oe->__call($method, $params);
179+
return $this->authProvider->oe->__call($method, $params);
152180
}
153181
}
154182

0 commit comments

Comments
 (0)