1
1
<?php
2
2
3
3
require_once "php-oe-json/openerp.php " ;
4
+ require_once "auth.php " ;
5
+
4
6
5
7
/**
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
8
9
*/
9
- class OEAuth {
10
-
11
- public $ js_code = "" ;
10
+ class OEAuthProvider extends AuthProvider {
12
11
13
12
function __construct ($ url , $ db ) {
14
13
$ this ->oe = new OpenERP ($ url , $ db );
15
- $ this ->_auth_cache = NULL ;
16
- $ this ->_auth_cache_dirty = True ;
17
-
18
- session_start ();
19
14
}
20
15
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 " ]);
35
22
}
36
23
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 );
40
33
}
41
34
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()``.
43
40
*
44
- * This function must validate authentication of given credentials
45
41
*/
46
- public function authenticate ($ credentials ) {
47
-
42
+ public function login ($ credentials ) {
48
43
if (!(isset ($ credentials ["login " ]) && isset ($ credentials ["password " ])))
49
44
return False ;
50
45
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 " ]);
60
47
}
61
48
62
- /** deauthenticating
49
+ /**
50
+ * Closes the current session and returns boolean upon success.
63
51
*
64
- * This function must unlog current session
65
52
*/
66
- public function deauthenticate () {
53
+ public function logout () {
54
+ return $ this ->oe ->logout (); // doesn't seem to work how it should
55
+ }
67
56
68
- $ this -> oe -> logout (); // doesn't seem to work how it should
57
+ }
69
58
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 {
74
63
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 ();
77
68
}
78
69
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
+ }
84
73
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
+ }
86
97
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 ) {
89
99
90
100
$ url_js_code = array ();
91
- foreach ($ config [ " urls " ] as $ url ) {
101
+ foreach ($ this -> urls as $ url ) {
92
102
$ url_js_code [] = "' $ url' " ;
93
103
};
94
104
$ url_js_code = implode (", " , $ url_js_code );
95
105
96
- return "<script type='text/javascript'> \n
106
+ return "<script type='text/javascript'>
97
107
98
108
urls = [ $ url_js_code];
99
109
@@ -131,8 +141,7 @@ function propagate_authentication_status(origin) {
131
141
132
142
function get_session_ids() {
133
143
var res = $.Deferred();
134
- res.resolve({oe_session_id: ' " . $ oe_session_id . "',
135
- oe_cookie: ' " . $ oe_cookie . "'});
144
+ res.resolve( " . json_encode ($ tokens , true ) . ");
136
145
return res;
137
146
}
138
147
@@ -144,11 +153,30 @@ function get_session_ids() {
144
153
145
154
}
146
155
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
+
147
175
/**
148
- * call delegation Delegation to $this->oe
176
+ * call delegation Delegation to $this->authProvider-> oe
149
177
*/
150
178
function __call ($ method , $ params ) {
151
- return $ this ->oe ->__call ($ method , $ params );
179
+ return $ this ->authProvider -> oe ->__call ($ method , $ params );
152
180
}
153
181
}
154
182
0 commit comments