bf20e8565710e1348f1dec5f555adf57d908e04b
[living-lab-site.git] / application / libraries / Openid.php
1 <?php
2
3 if (!defined('BASEPATH'))
4         exit('No direct script access allowed');
5
6 /**
7  * OpenID Library
8  *
9  * @package    CodeIgniter
10  * @author     bardelot, Călin-Andrei Burloiu
11  * @see        http://cakebaker.42dh.com/2007/01/11/cakephp-and-openid/
12  *             & http://openidenabled.com/php-openid/
13  */
14 class Openid {
15
16         var $storePath = 'tmp';
17         
18         var $sreg_enable = FALSE;
19         var $sreg_required = NULL;
20         var $sreg_optional = NULL;
21         var $sreg_policy = NULL;
22         var $ax_enable  = FALSE;
23         var $ax_attributes = NULL;
24         var $pape_enable = FALSE;
25         var $pape_policy_uris = NULL;
26         var $ext_args = NULL;
27         var $request_to;
28         var $trust_root;
29
30         function __construct()
31         {
32                 $CI = & get_instance();
33                 $CI->config->load('openid');
34                 $this->storePath = $CI->config->item('openid_storepath');
35
36                 session_start();
37                 $this->_do_includes();
38
39                 log_message('debug', "OpenID Class Initialized");
40         }
41
42         function _do_includes()
43         {
44                 set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
45
46                 require_once "Auth/OpenID/Consumer.php";
47                 require_once "Auth/OpenID/FileStore.php";
48                 require_once "Auth/OpenID/SReg.php";
49                 require_once "Auth/OpenID/AX.php";
50                 require_once "Auth/OpenID/PAPE.php";
51         }
52
53         function set_sreg($enable, $required = NULL, $optional = NULL, $policy = NULL)
54         {
55                 $this->sreg_enable = $enable;
56                 $this->sreg_required = $required;
57                 $this->sreg_optional = $optional;
58                 $this->sreg_policy = $policy;
59         }
60         
61         function set_ax($enable, $ax_attributes = NULL)
62         {
63                 $this->ax_enable = $enable;
64                 $this->ax_attributes = $ax_attributes;
65         }
66
67         function set_pape($enable, $policy_uris = NULL)
68         {
69                 $this->pape_enable = $enable;
70                 $this->pape_policy_uris = $policy_uris;
71         }
72
73         function set_request_to($uri)
74         {
75                 $this->request_to = $uri;
76         }
77
78         function set_trust_root($trust_root)
79         {
80                 $this->trust_root = $trust_root;
81         }
82
83         function set_args($args)
84         {
85                 $this->ext_args = $args;
86         }
87
88         function _set_message($error, $msg, $val = '', $sub = '%s')
89         {
90                 $CI = & get_instance();
91                 $CI->lang->load('openid', 'english');
92                 echo str_replace($sub, $val, $CI->lang->line($msg));
93
94                 if ($error)
95                 {
96                         exit;
97                 }
98         }
99
100         function authenticate($openId)
101         {
102                 $consumer = $this->_get_consumer();
103                 $authRequest = $consumer->begin($openId);
104
105                 // No auth request means we can't begin OpenID.
106                 if (!$authRequest)
107                 {
108                         $this->_set_message(TRUE, 'openid_auth_error');
109                 }
110                 
111                 if ($this->sreg_enable)
112                 {
113                         $sreg_request = Auth_OpenID_SRegRequest::build(
114                                         $this->sreg_required, $this->sreg_optional, 
115                                         $this->sreg_policy);
116
117                         if ($sreg_request)
118                         {
119                                 $authRequest->addExtension($sreg_request);
120                         }
121                         else
122                         {
123                                 $this->_set_message(TRUE, 'openid_sreg_failed');
124                         }
125                 }
126                 
127                 if ($this->ax_enable)
128                 {
129                         $ax_request = new Auth_OpenID_AX_FetchRequest();
130                         
131                         if ($ax_request)
132                         {
133                                 foreach ($this->ax_attributes as $attr)
134                                         $ax_request->add($attr);
135                                 $authRequest->addExtension($ax_request);
136                         }
137                         else
138                         {
139                                 $this->_set_message(TRUE, 'openid_ax_failed');
140                         }
141                 }
142                 
143                 if ($this->pape_enable)
144                 {
145                         $pape_request = new Auth_OpenID_PAPE_Request($this->pape_policy_uris);
146
147                         if ($pape_request)
148                         {
149                                 $authRequest->addExtension($pape_request);
150                         }
151                         else
152                         {
153                                 $this->_set_message(TRUE, 'openid_pape_failed');
154                         }
155                 }
156
157                 if ($this->ext_args != NULL)
158                 {
159                         foreach ($this->ext_args as $extensionArgument)
160                         {
161                                 if (count($extensionArgument) == 3)
162                                 {
163                                         $authRequest->addExtensionArg($extensionArgument[0],
164                                                         $extensionArgument[1],
165                                                         $extensionArgument[2]);
166                                 }
167                         }
168                 }
169
170                 // Redirect the user to the OpenID server for authentication.
171                 // Store the token for this authentication so we can verify the
172                 // response.
173                 // For OpenID 1, send a redirect.  For OpenID 2, use a Javascript
174                 // form to send a POST request to the server.
175                 if ($authRequest->shouldSendRedirect())
176                 {
177                         $redirect_url = $authRequest->redirectURL($this->trust_root,
178                                         $this->request_to);
179
180                         // If the redirect URL can't be built, display an error
181                         // message.
182                         if (Auth_OpenID::isFailure($redirect_url))
183                         {
184                                 $this->_set_message(TRUE, 'openid_redirect_failed', $redirect_url->message);
185                         }
186                         else
187                         {
188                                 // Send redirect.
189                                 header("Location: " . $redirect_url);
190                         }
191                 }
192                 else
193                 {
194                         // Generate form markup and render it.
195                         $form_id = 'openid_message';
196                         $form_html = $authRequest->htmlMarkup($this->trust_root,
197                                         $this->request_to, FALSE, array('id' => $form_id));
198
199                         // Display an error if the form markup couldn't be generated;
200                         // otherwise, render the HTML.
201                         if (Auth_OpenID::isFailure($form_html))
202                         {
203                                 $this->_set_message(TRUE, 'openid_redirect_failed', $form_html->message);
204                         }
205                         else
206                         {
207                                 print $form_html;
208                         }
209                 }
210         }
211
212         function get_response()
213         {
214                 $consumer = $this->_get_consumer();
215                 $response = $consumer->complete($this->request_to);
216
217                 return $response;
218         }
219
220         function _get_consumer()
221         {
222                 if (!file_exists($this->storePath) && !mkdir($this->storePath))
223                 {
224                         $this->_set_message(TRUE, 'openid_storepath_failed', $this->storePath);
225                 }
226
227                 $store = new Auth_OpenID_FileStore($this->storePath);
228                 $consumer = new Auth_OpenID_Consumer($store);
229
230                 return $consumer;
231         }
232
233 }