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";
51 require_once "Auth/extensions.php";
54 function set_sreg($enable, $required = NULL, $optional = NULL, $policy = NULL)
56 $this->sreg_enable = $enable;
57 $this->sreg_required = $required;
58 $this->sreg_optional = $optional;
59 $this->sreg_policy = $policy;
62 function set_ax($enable, $ax_attributes = NULL)
64 $this->ax_enable = $enable;
65 $this->ax_attributes = $ax_attributes;
68 function set_pape($enable, $policy_uris = NULL)
70 $this->pape_enable = $enable;
71 $this->pape_policy_uris = $policy_uris;
74 function set_request_to($uri)
76 $this->request_to = $uri;
79 function set_trust_root($trust_root)
81 $this->trust_root = $trust_root;
84 function set_args($args)
86 $this->ext_args = $args;
89 function _set_message($error, $msg, $val = '', $sub = '%s')
91 $CI = & get_instance();
92 $CI->lang->load('openid', 'english');
93 echo str_replace($sub, $val, $CI->lang->line($msg));
101 function authenticate($openId)
103 $consumer = $this->_get_consumer();
104 $authRequest = $consumer->begin($openId);
106 // No auth request means we can't begin OpenID.
109 $this->_set_message(TRUE, 'openid_auth_error');
112 if ($this->sreg_enable)
114 $sreg_request = Auth_OpenID_SRegRequest::build(
115 $this->sreg_required, $this->sreg_optional,
120 $authRequest->addExtension($sreg_request);
124 $this->_set_message(TRUE, 'openid_sreg_failed');
128 if ($this->ax_enable)
130 $ax_request = new Auth_OpenID_AX_FetchRequest();
134 foreach ($this->ax_attributes as $attr)
135 $ax_request->add($attr);
136 $authRequest->addExtension($ax_request);
140 $this->_set_message(TRUE, 'openid_ax_failed');
144 if ($this->pape_enable)
146 $pape_request = new Auth_OpenID_PAPE_Request($this->pape_policy_uris);
150 $authRequest->addExtension($pape_request);
154 $this->_set_message(TRUE, 'openid_pape_failed');
158 if ($this->ext_args != NULL)
160 foreach ($this->ext_args as $extensionArgument)
162 if (count($extensionArgument) == 3)
164 $authRequest->addExtensionArg($extensionArgument[0],
165 $extensionArgument[1],
166 $extensionArgument[2]);
171 // Redirect the user to the OpenID server for authentication.
172 // Store the token for this authentication so we can verify the
174 // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
175 // form to send a POST request to the server.
176 if ($authRequest->shouldSendRedirect())
178 $redirect_url = $authRequest->redirectURL($this->trust_root,
181 // If the redirect URL can't be built, display an error
183 if (Auth_OpenID::isFailure($redirect_url))
185 $this->_set_message(TRUE, 'openid_redirect_failed', $redirect_url->message);
190 header("Location: " . $redirect_url);
195 // Generate form markup and render it.
196 $form_id = 'openid_message';
197 $form_html = $authRequest->htmlMarkup($this->trust_root,
198 $this->request_to, FALSE, array('id' => $form_id));
200 // Display an error if the form markup couldn't be generated;
201 // otherwise, render the HTML.
202 if (Auth_OpenID::isFailure($form_html))
204 $this->_set_message(TRUE, 'openid_redirect_failed', $form_html->message);
213 function get_response()
215 $consumer = $this->_get_consumer();
216 $response = $consumer->complete($this->request_to);
221 function _get_consumer()
223 if (!file_exists($this->storePath) && !mkdir($this->storePath))
225 $this->_set_message(TRUE, 'openid_storepath_failed', $this->storePath);
228 $store = new Auth_OpenID_FileStore($this->storePath);
229 $consumer = new Auth_OpenID_Consumer($store,
230 new Auth_Yadis_CISession());