PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/Common/PortalOpenId.php

http://pradoportal.googlecode.com/
PHP | 315 lines | 215 code | 46 blank | 54 comment | 30 complexity | d8fe1103b6bc996e37ec5d178c2b4fca MD5 | raw file
  1. <?php
  2. /**
  3. * Prado Portal.
  4. *
  5. * @author Steen Rabol <steen.rabol@gmail.com>
  6. * @link http://www.pradoportal.dk/
  7. * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
  8. * @license http://www.pradoportal.dk
  9. * @version $Id: PortalOpenId.php 431 2011-01-05 11:58:18Z steen.rabol $
  10. * @package Pradoportal.Common
  11. *
  12. */
  13. /**
  14. *
  15. * @package Pradoportal.Common
  16. */
  17. Prado::using('System.Util.TVarDumper');
  18. // We need to set the include path
  19. $path_extra = Prado::getPathOfNamespace('Application.Common.3rdParty.OpenId') .'/';
  20. $incpath = ini_get('include_path');
  21. $incpath = $path_extra . PATH_SEPARATOR . $incpath;
  22. ini_set('include_path', $incpath);
  23. // If we are running on Windows there is no random device
  24. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
  25. {
  26. define("Auth_OpenID_RAND_SOURCE",null);
  27. }
  28. require_once "Auth/OpenID/Consumer.php";
  29. // Require the "file store" module, which we'll need to store OpenID information.
  30. require_once "Auth/OpenID/FileStore.php";
  31. // Require the Simple Registration extension API.
  32. require_once "Auth/OpenID/SReg.php";
  33. //Require the PAPE extension module.
  34. require_once "Auth/OpenID/PAPE.php";
  35. class PortalOpenId extends TComponent
  36. {
  37. private $_store = null;
  38. private $_consumer = null;
  39. private $_app = null;
  40. public function __construct($store = null, $consumer = null)
  41. {
  42. $this->Application = Prado::getApplication();
  43. }
  44. public function getStore()
  45. {
  46. if($this->_store === null)
  47. {
  48. $store_path = $this->Application->PortalBasePath . '/storage/OpenId';
  49. PortalUtil::CreateDirStructure($store_path);
  50. $this->_store = new Auth_OpenID_FileStore($store_path);
  51. }
  52. return $this->_store;
  53. }
  54. public function setStore($value)
  55. {
  56. $this->_store = $value;
  57. }
  58. public function getConsumer()
  59. {
  60. if($this->_consumer === null)
  61. {
  62. $this->_consumer = new Auth_OpenID_Consumer($this->Store);
  63. }
  64. return $this->_consumer;
  65. }
  66. public function setConsumer($value)
  67. {
  68. $this->_consumer = $value;
  69. }
  70. public function getApplication()
  71. {
  72. return $this->_app;
  73. }
  74. public function setApplication($value)
  75. {
  76. $this->_app =$value;
  77. }
  78. public function VerifyOpenId($openid_url,$trusturl, $returntourl)
  79. {
  80. // Begin the OpenID authentication process.
  81. $consumer = $this->getConsumer();
  82. $auth_request = $consumer->begin($openid_url);
  83. // No auth request means we can't begin OpenID.
  84. if (!$auth_request)
  85. {
  86. $msg= "Authentication error; not a valid OpenID.";
  87. $this->reportError(0,$msg);
  88. }
  89. if ($auth_request->shouldSendRedirect())
  90. {
  91. $redirect_url = $auth_request->redirectURL($trusturl,$returntourl);
  92. // If the redirect URL can't be built, display an error message.
  93. if (Auth_OpenID::isFailure($redirect_url))
  94. {
  95. $msg = "Could not redirect to server: " . $redirect_url->message;
  96. $this->reportError(0, $msg);
  97. }
  98. else
  99. {
  100. $app = $this->getApplication();
  101. $r = $app->Response;
  102. // Send redirect.
  103. $r->redirect($redirect_url);
  104. }
  105. }
  106. else
  107. {
  108. // Generate form markup and render it.
  109. $form_id = 'openid_message';
  110. $form_html = $auth_request->htmlMarkup($trusturl, $returntourl,false, array('id' => $form_id));
  111. // Display an error if the form markup couldn't be generated;
  112. // otherwise, render the HTML.
  113. if (Auth_OpenID::isFailure($form_html))
  114. {
  115. $msg = "Could not redirect to server: " . $form_html->message;
  116. $this->reportError(1, $msg);
  117. }
  118. else
  119. {
  120. print $form_html;
  121. }
  122. }
  123. }
  124. public function BeginSimpleRegistration($openid_url,$trusturl, $returntourl)
  125. {
  126. // Begin the OpenID authentication process.
  127. $consumer = $this->getConsumer();
  128. $auth_request = $consumer->begin($openid_url);
  129. // No auth request means we can't begin OpenID.
  130. if (!$auth_request)
  131. {
  132. $msg= "Authentication error; not a valid OpenID.";
  133. $this->reportError(1,$msg);
  134. }
  135. try
  136. {
  137. $sreg_request = Auth_OpenID_SRegRequest::build(
  138. // Required
  139. array('nickname','email'),
  140. // Optional
  141. array('fullname', 'language'));
  142. }
  143. catch(Exception $e)
  144. {
  145. $this->reportError(1, $e->getMessage());
  146. }
  147. if ($sreg_request)
  148. {
  149. $auth_request->addExtension($sreg_request);
  150. }
  151. // Redirect the user to the OpenID server for authentication.
  152. // Store the token for this authentication so we can verify the
  153. // response.
  154. // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
  155. // form to send a POST request to the server.
  156. if ($auth_request->shouldSendRedirect())
  157. {
  158. $redirect_url = $auth_request->redirectURL($trusturl,$returntourl);
  159. // If the redirect URL can't be built, display an error message.
  160. if (Auth_OpenID::isFailure($redirect_url))
  161. {
  162. $msg = "Could not redirect to server: " . $redirect_url->message;
  163. $this->reportError(1, $msg);
  164. }
  165. else
  166. {
  167. $app = $this->getApplication();
  168. $r = $app->Response;
  169. // Send redirect.
  170. $r->redirect($redirect_url);
  171. }
  172. }
  173. else
  174. {
  175. // Generate form markup and render it.
  176. $form_id = 'openid_message';
  177. $form_html = $auth_request->htmlMarkup($trusturl, $returntourl,false, array('id' => $form_id));
  178. // Display an error if the form markup couldn't be generated;
  179. // otherwise, render the HTML.
  180. if (Auth_OpenID::isFailure($form_html))
  181. {
  182. $msg = "Could not redirect to server: " . $form_html->message;
  183. $this->reportError(1, $msg);
  184. }
  185. else
  186. {
  187. print $form_html;
  188. }
  189. }
  190. }
  191. public function CompleteSimpleRegistration($returntourl)
  192. {
  193. $sreg = array();
  194. $consumer = $this->getConsumer();
  195. // Complete the authentication process using the server's
  196. // response.
  197. $response = $consumer->complete($returntourl);
  198. // Check the response status.
  199. if ($response->status == Auth_OpenID_CANCEL)
  200. {
  201. // This means the authentication was cancelled.
  202. $msg = 'Verification cancelled.';
  203. $this->reportError(0,$msg);
  204. }
  205. else if ($response->status == Auth_OpenID_FAILURE)
  206. {
  207. // Authentication failed; display the error message.
  208. if($response->message === 'Server denied check_authentication')
  209. {
  210. $msg = "Did you remember to fill in the complete OpenId URL?";
  211. }
  212. else
  213. {
  214. $msg = "OpenID authentication failed: " . $response->message;
  215. }
  216. $this->reportError(0,$msg);
  217. }
  218. else if ($response->status == Auth_OpenID_SUCCESS)
  219. {
  220. // This means the authentication succeeded; extract the
  221. // identity URL and Simple Registration data (if it was
  222. // returned).
  223. $openid = $response->getDisplayIdentifier();
  224. $esc_identity = htmlentities($openid);
  225. $sreg['openid_url'] = $esc_identity;
  226. if ($response->endpoint->canonicalID)
  227. {
  228. $escaped_canonicalID = htmlentities($response->endpoint->canonicalID);
  229. $success .= ' (XRI CanonicalID: '.$escaped_canonicalID.') ';
  230. }
  231. $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
  232. $sreg = array_merge($sreg,$sreg_resp->contents());
  233. }
  234. return $sreg;
  235. }
  236. public function gotoPage($pagePath,$getParameters = null)
  237. {
  238. $this->Application->Response->redirect($this->Application->Service->constructUrl($pagePath,$getParameters));
  239. }
  240. public function reportError($errorCode, $errorMsg)
  241. {
  242. $this->gotoPage('System.ErrorReport',array('id'=>$errorCode,'msg' => urldecode($this->getApplication()->getSecurityManager()->hashData($errorMsg))));
  243. }
  244. public function CompleteVerifyOpenId($returntourl)
  245. {
  246. $consumer = $this->getConsumer();
  247. // Complete the authentication process using the server's
  248. // response.
  249. $response = $consumer->complete($returntourl);
  250. // Check the response status.
  251. if ($response->status == Auth_OpenID_CANCEL)
  252. {
  253. // This means the authentication was cancelled.
  254. $this->reportError(0,'Verification cancelled.');
  255. }
  256. else if ($response->status == Auth_OpenID_FAILURE)
  257. {
  258. // Authentication failed; display the error message.
  259. die($response->message);
  260. $this->reportError(0,"OpenID authentication failed: " . $response->message);
  261. }
  262. else if ($response->status == Auth_OpenID_SUCCESS)
  263. {
  264. return true;
  265. }
  266. return false;
  267. }
  268. }
  269. ?>