3 if (!defined('BASEPATH'))
4 exit('No direct script access allowed');
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/
16 var $storePath = 'tmp';
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;
30 function __construct()
32 $CI = & get_instance();
33 $CI->config->load('openid');
34 $this->storePath = $CI->config->item('openid_storepath');
37 $this->_do_includes();
39 log_message('debug', "OpenID Class Initialized");
42 function _do_includes()
44 set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
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";
53 function set_sreg($enable, $required = NULL, $optional = NULL, $policy = NULL)
55 $this->sreg_enable = $enable;
56 $this->sreg_required = $required;
57 $this->sreg_optional = $optional;
58 $this->sreg_policy = $policy;
61 function set_ax($enable, $ax_attributes = NULL)
63 $this->ax_enable = $enable;
64 $this->ax_attributes = $ax_attributes;
67 function set_pape($enable, $policy_uris = NULL)
69 $this->pape_enable = $enable;
70 $this->pape_policy_uris = $policy_uris;
73 function set_request_to($uri)
75 $this->request_to = $uri;
78 function set_trust_root($trust_root)
80 $this->trust_root = $trust_root;
83 function set_args($args)
85 $this->ext_args = $args;
88 function _set_message($error, $msg, $val = '', $sub = '%s')
90 $CI = & get_instance();
91 $CI->lang->load('openid', 'english');
92 echo str_replace($sub, $val, $CI->lang->line($msg));
100 function authenticate($openId)
102 $consumer = $this->_get_consumer();
103 $authRequest = $consumer->begin($openId);
105 // No auth request means we can't begin OpenID.
108 $this->_set_message(TRUE, 'openid_auth_error');
111 if ($this->sreg_enable)
113 $sreg_request = Auth_OpenID_SRegRequest::build(
114 $this->sreg_required, $this->sreg_optional,
119 $authRequest->addExtension($sreg_request);
123 $this->_set_message(TRUE, 'openid_sreg_failed');
127 if ($this->ax_enable)
129 $ax_request = new Auth_OpenID_AX_FetchRequest();
133 foreach ($this->ax_attributes as $attr)
134 $ax_request->add($attr);
135 $authRequest->addExtension($ax_request);
139 $this->_set_message(TRUE, 'openid_ax_failed');
143 if ($this->pape_enable)
145 $pape_request = new Auth_OpenID_PAPE_Request($this->pape_policy_uris);
149 $authRequest->addExtension($pape_request);
153 $this->_set_message(TRUE, 'openid_pape_failed');
157 if ($this->ext_args != NULL)
159 foreach ($this->ext_args as $extensionArgument)
161 if (count($extensionArgument) == 3)
163 $authRequest->addExtensionArg($extensionArgument[0],
164 $extensionArgument[1],
165 $extensionArgument[2]);
170 // Redirect the user to the OpenID server for authentication.
171 // Store the token for this authentication so we can verify the
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())
177 $redirect_url = $authRequest->redirectURL($this->trust_root,
180 // If the redirect URL can't be built, display an error
182 if (Auth_OpenID::isFailure($redirect_url))
184 $this->_set_message(TRUE, 'openid_redirect_failed', $redirect_url->message);
189 header("Location: " . $redirect_url);
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));
199 // Display an error if the form markup couldn't be generated;
200 // otherwise, render the HTML.
201 if (Auth_OpenID::isFailure($form_html))
203 $this->_set_message(TRUE, 'openid_redirect_failed', $form_html->message);
212 function get_response()
214 $consumer = $this->_get_consumer();
215 $response = $consumer->complete($this->request_to);
220 function _get_consumer()
222 if (!file_exists($this->storePath) && !mkdir($this->storePath))
224 $this->_set_message(TRUE, 'openid_storepath_failed', $this->storePath);
227 $store = new Auth_OpenID_FileStore($this->storePath);
228 $consumer = new Auth_OpenID_Consumer($store);