PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/openid/Auth/OpenID/Server.php

https://bitbucket.org/asosso/joomla15
PHP | 1763 lines | 1073 code | 226 blank | 464 comment | 181 complexity | f2f9f4e74762c3f9855e406a614e6dd4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * OpenID server protocol and logic.
  4. *
  5. * Overview
  6. *
  7. * An OpenID server must perform three tasks:
  8. *
  9. * 1. Examine the incoming request to determine its nature and validity.
  10. * 2. Make a decision about how to respond to this request.
  11. * 3. Format the response according to the protocol.
  12. *
  13. * The first and last of these tasks may performed by the {@link
  14. * Auth_OpenID_Server::decodeRequest()} and {@link
  15. * Auth_OpenID_Server::encodeResponse} methods. Who gets to do the
  16. * intermediate task -- deciding how to respond to the request -- will
  17. * depend on what type of request it is.
  18. *
  19. * If it's a request to authenticate a user (a 'checkid_setup' or
  20. * 'checkid_immediate' request), you need to decide if you will assert
  21. * that this user may claim the identity in question. Exactly how you
  22. * do that is a matter of application policy, but it generally
  23. * involves making sure the user has an account with your system and
  24. * is logged in, checking to see if that identity is hers to claim,
  25. * and verifying with the user that she does consent to releasing that
  26. * information to the party making the request.
  27. *
  28. * Examine the properties of the {@link Auth_OpenID_CheckIDRequest}
  29. * object, and if and when you've come to a decision, form a response
  30. * by calling {@link Auth_OpenID_CheckIDRequest::answer()}.
  31. *
  32. * Other types of requests relate to establishing associations between
  33. * client and server and verifing the authenticity of previous
  34. * communications. {@link Auth_OpenID_Server} contains all the logic
  35. * and data necessary to respond to such requests; just pass it to
  36. * {@link Auth_OpenID_Server::handleRequest()}.
  37. *
  38. * OpenID Extensions
  39. *
  40. * Do you want to provide other information for your users in addition
  41. * to authentication? Version 1.2 of the OpenID protocol allows
  42. * consumers to add extensions to their requests. For example, with
  43. * sites using the Simple Registration
  44. * Extension
  45. * (http://www.openidenabled.com/openid/simple-registration-extension/),
  46. * a user can agree to have their nickname and e-mail address sent to
  47. * a site when they sign up.
  48. *
  49. * Since extensions do not change the way OpenID authentication works,
  50. * code to handle extension requests may be completely separate from
  51. * the {@link Auth_OpenID_Request} class here. But you'll likely want
  52. * data sent back by your extension to be signed. {@link
  53. * Auth_OpenID_ServerResponse} provides methods with which you can add
  54. * data to it which can be signed with the other data in the OpenID
  55. * signature.
  56. *
  57. * For example:
  58. *
  59. * <pre> // when request is a checkid_* request
  60. * $response = $request->answer(true);
  61. * // this will a signed 'openid.sreg.timezone' parameter to the response
  62. * response.addField('sreg', 'timezone', 'America/Los_Angeles')</pre>
  63. *
  64. * Stores
  65. *
  66. * The OpenID server needs to maintain state between requests in order
  67. * to function. Its mechanism for doing this is called a store. The
  68. * store interface is defined in Interface.php. Additionally, several
  69. * concrete store implementations are provided, so that most sites
  70. * won't need to implement a custom store. For a store backed by flat
  71. * files on disk, see {@link Auth_OpenID_FileStore}. For stores based
  72. * on MySQL, SQLite, or PostgreSQL, see the {@link
  73. * Auth_OpenID_SQLStore} subclasses.
  74. *
  75. * Upgrading
  76. *
  77. * The keys by which a server looks up associations in its store have
  78. * changed in version 1.2 of this library. If your store has entries
  79. * created from version 1.0 code, you should empty it.
  80. *
  81. * PHP versions 4 and 5
  82. *
  83. * LICENSE: See the COPYING file included in this distribution.
  84. *
  85. * @package OpenID
  86. * @author JanRain, Inc. <openid@janrain.com>
  87. * @copyright 2005-2008 Janrain, Inc.
  88. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  89. */
  90. // Do not allow direct access
  91. defined( '_JEXEC' ) or die( 'Restricted access' );
  92. /**
  93. * Required imports
  94. */
  95. require_once "Auth/OpenID.php";
  96. require_once "Auth/OpenID/Association.php";
  97. require_once "Auth/OpenID/CryptUtil.php";
  98. require_once "Auth/OpenID/BigMath.php";
  99. require_once "Auth/OpenID/DiffieHellman.php";
  100. require_once "Auth/OpenID/KVForm.php";
  101. require_once "Auth/OpenID/TrustRoot.php";
  102. require_once "Auth/OpenID/ServerRequest.php";
  103. require_once "Auth/OpenID/Message.php";
  104. require_once "Auth/OpenID/Nonce.php";
  105. define('AUTH_OPENID_HTTP_OK', 200);
  106. define('AUTH_OPENID_HTTP_REDIRECT', 302);
  107. define('AUTH_OPENID_HTTP_ERROR', 400);
  108. /**
  109. * @access private
  110. */
  111. global $_Auth_OpenID_Request_Modes;
  112. $_Auth_OpenID_Request_Modes = array('checkid_setup',
  113. 'checkid_immediate');
  114. /**
  115. * @access private
  116. */
  117. define('Auth_OpenID_ENCODE_KVFORM', 'kfvorm');
  118. /**
  119. * @access private
  120. */
  121. define('Auth_OpenID_ENCODE_URL', 'URL/redirect');
  122. /**
  123. * @access private
  124. */
  125. define('Auth_OpenID_ENCODE_HTML_FORM', 'HTML form');
  126. /**
  127. * @access private
  128. */
  129. function Auth_OpenID_isError($obj, $cls = 'Auth_OpenID_ServerError')
  130. {
  131. return is_a($obj, $cls);
  132. }
  133. /**
  134. * An error class which gets instantiated and returned whenever an
  135. * OpenID protocol error occurs. Be prepared to use this in place of
  136. * an ordinary server response.
  137. *
  138. * @package OpenID
  139. */
  140. class Auth_OpenID_ServerError {
  141. /**
  142. * @access private
  143. */
  144. function Auth_OpenID_ServerError($message = null, $text = null,
  145. $reference = null, $contact = null)
  146. {
  147. $this->message = $message;
  148. $this->text = $text;
  149. $this->contact = $contact;
  150. $this->reference = $reference;
  151. }
  152. function getReturnTo()
  153. {
  154. if ($this->message &&
  155. $this->message->hasKey(Auth_OpenID_OPENID_NS, 'return_to')) {
  156. return $this->message->getArg(Auth_OpenID_OPENID_NS,
  157. 'return_to');
  158. } else {
  159. return null;
  160. }
  161. }
  162. /**
  163. * Returns the return_to URL for the request which caused this
  164. * error.
  165. */
  166. function hasReturnTo()
  167. {
  168. return $this->getReturnTo() !== null;
  169. }
  170. /**
  171. * Encodes this error's response as a URL suitable for
  172. * redirection. If the response has no return_to, another
  173. * Auth_OpenID_ServerError is returned.
  174. */
  175. function encodeToURL()
  176. {
  177. if (!$this->message) {
  178. return null;
  179. }
  180. $msg = $this->toMessage();
  181. return $msg->toURL($this->getReturnTo());
  182. }
  183. /**
  184. * Encodes the response to key-value form. This is a
  185. * machine-readable format used to respond to messages which came
  186. * directly from the consumer and not through the user-agent. See
  187. * the OpenID specification.
  188. */
  189. function encodeToKVForm()
  190. {
  191. return Auth_OpenID_KVForm::fromArray(
  192. array('mode' => 'error',
  193. 'error' => $this->toString()));
  194. }
  195. function toFormMarkup($form_tag_attrs=null)
  196. {
  197. $msg = $this->toMessage();
  198. return $msg->toFormMarkup($this->getReturnTo(), $form_tag_attrs);
  199. }
  200. function toHTML($form_tag_attrs=null)
  201. {
  202. return Auth_OpenID::autoSubmitHTML(
  203. $this->toFormMarkup($form_tag_attrs));
  204. }
  205. function toMessage()
  206. {
  207. // Generate a Message object for sending to the relying party,
  208. // after encoding.
  209. $namespace = $this->message->getOpenIDNamespace();
  210. $reply = new Auth_OpenID_Message($namespace);
  211. $reply->setArg(Auth_OpenID_OPENID_NS, 'mode', 'error');
  212. $reply->setArg(Auth_OpenID_OPENID_NS, 'error', $this->toString());
  213. if ($this->contact !== null) {
  214. $reply->setArg(Auth_OpenID_OPENID_NS, 'contact', $this->contact);
  215. }
  216. if ($this->reference !== null) {
  217. $reply->setArg(Auth_OpenID_OPENID_NS, 'reference',
  218. $this->reference);
  219. }
  220. return $reply;
  221. }
  222. /**
  223. * Returns one of Auth_OpenID_ENCODE_URL,
  224. * Auth_OpenID_ENCODE_KVFORM, or null, depending on the type of
  225. * encoding expected for this error's payload.
  226. */
  227. function whichEncoding()
  228. {
  229. global $_Auth_OpenID_Request_Modes;
  230. if ($this->hasReturnTo()) {
  231. if ($this->message->isOpenID2() &&
  232. (strlen($this->encodeToURL()) >
  233. Auth_OpenID_OPENID1_URL_LIMIT)) {
  234. return Auth_OpenID_ENCODE_HTML_FORM;
  235. } else {
  236. return Auth_OpenID_ENCODE_URL;
  237. }
  238. }
  239. if (!$this->message) {
  240. return null;
  241. }
  242. $mode = $this->message->getArg(Auth_OpenID_OPENID_NS,
  243. 'mode');
  244. if ($mode) {
  245. if (!in_array($mode, $_Auth_OpenID_Request_Modes)) {
  246. return Auth_OpenID_ENCODE_KVFORM;
  247. }
  248. }
  249. return null;
  250. }
  251. /**
  252. * Returns this error message.
  253. */
  254. function toString()
  255. {
  256. if ($this->text) {
  257. return $this->text;
  258. } else {
  259. return get_class($this) . " error";
  260. }
  261. }
  262. }
  263. /**
  264. * Error returned by the server code when a return_to is absent from a
  265. * request.
  266. *
  267. * @package OpenID
  268. */
  269. class Auth_OpenID_NoReturnToError extends Auth_OpenID_ServerError {
  270. function Auth_OpenID_NoReturnToError($message = null,
  271. $text = "No return_to URL available")
  272. {
  273. parent::Auth_OpenID_ServerError($message, $text);
  274. }
  275. function toString()
  276. {
  277. return "No return_to available";
  278. }
  279. }
  280. /**
  281. * An error indicating that the return_to URL is malformed.
  282. *
  283. * @package OpenID
  284. */
  285. class Auth_OpenID_MalformedReturnURL extends Auth_OpenID_ServerError {
  286. function Auth_OpenID_MalformedReturnURL($message, $return_to)
  287. {
  288. $this->return_to = $return_to;
  289. parent::Auth_OpenID_ServerError($message, "malformed return_to URL");
  290. }
  291. }
  292. /**
  293. * This error is returned when the trust_root value is malformed.
  294. *
  295. * @package OpenID
  296. */
  297. class Auth_OpenID_MalformedTrustRoot extends Auth_OpenID_ServerError {
  298. function Auth_OpenID_MalformedTrustRoot($message = null,
  299. $text = "Malformed trust root")
  300. {
  301. parent::Auth_OpenID_ServerError($message, $text);
  302. }
  303. function toString()
  304. {
  305. return "Malformed trust root";
  306. }
  307. }
  308. /**
  309. * The base class for all server request classes.
  310. *
  311. * @package OpenID
  312. */
  313. class Auth_OpenID_Request {
  314. var $mode = null;
  315. }
  316. /**
  317. * A request to verify the validity of a previous response.
  318. *
  319. * @package OpenID
  320. */
  321. class Auth_OpenID_CheckAuthRequest extends Auth_OpenID_Request {
  322. var $mode = "check_authentication";
  323. var $invalidate_handle = null;
  324. function Auth_OpenID_CheckAuthRequest($assoc_handle, $signed,
  325. $invalidate_handle = null)
  326. {
  327. $this->assoc_handle = $assoc_handle;
  328. $this->signed = $signed;
  329. if ($invalidate_handle !== null) {
  330. $this->invalidate_handle = $invalidate_handle;
  331. }
  332. $this->namespace = Auth_OpenID_OPENID2_NS;
  333. $this->message = null;
  334. }
  335. function fromMessage($message, $server=null)
  336. {
  337. $required_keys = array('assoc_handle', 'sig', 'signed');
  338. foreach ($required_keys as $k) {
  339. if (!$message->getArg(Auth_OpenID_OPENID_NS, $k)) {
  340. return new Auth_OpenID_ServerError($message,
  341. sprintf("%s request missing required parameter %s from \
  342. query", "check_authentication", $k));
  343. }
  344. }
  345. $assoc_handle = $message->getArg(Auth_OpenID_OPENID_NS, 'assoc_handle');
  346. $sig = $message->getArg(Auth_OpenID_OPENID_NS, 'sig');
  347. $signed_list = $message->getArg(Auth_OpenID_OPENID_NS, 'signed');
  348. $signed_list = explode(",", $signed_list);
  349. $signed = $message;
  350. if ($signed->hasKey(Auth_OpenID_OPENID_NS, 'mode')) {
  351. $signed->setArg(Auth_OpenID_OPENID_NS, 'mode', 'id_res');
  352. }
  353. $result = new Auth_OpenID_CheckAuthRequest($assoc_handle, $signed);
  354. $result->message = $message;
  355. $result->sig = $sig;
  356. $result->invalidate_handle = $message->getArg(Auth_OpenID_OPENID_NS,
  357. 'invalidate_handle');
  358. return $result;
  359. }
  360. function answer(&$signatory)
  361. {
  362. $is_valid = $signatory->verify($this->assoc_handle, $this->signed);
  363. // Now invalidate that assoc_handle so it this checkAuth
  364. // message cannot be replayed.
  365. $signatory->invalidate($this->assoc_handle, true);
  366. $response = new Auth_OpenID_ServerResponse($this);
  367. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  368. 'is_valid',
  369. ($is_valid ? "true" : "false"));
  370. if ($this->invalidate_handle) {
  371. $assoc = $signatory->getAssociation($this->invalidate_handle,
  372. false);
  373. if (!$assoc) {
  374. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  375. 'invalidate_handle',
  376. $this->invalidate_handle);
  377. }
  378. }
  379. return $response;
  380. }
  381. }
  382. /**
  383. * A class implementing plaintext server sessions.
  384. *
  385. * @package OpenID
  386. */
  387. class Auth_OpenID_PlainTextServerSession {
  388. /**
  389. * An object that knows how to handle association requests with no
  390. * session type.
  391. */
  392. var $session_type = 'no-encryption';
  393. var $needs_math = false;
  394. var $allowed_assoc_types = array('HMAC-SHA1', 'HMAC-SHA256');
  395. function fromMessage($unused_request)
  396. {
  397. return new Auth_OpenID_PlainTextServerSession();
  398. }
  399. function answer($secret)
  400. {
  401. return array('mac_key' => base64_encode($secret));
  402. }
  403. }
  404. /**
  405. * A class implementing DH-SHA1 server sessions.
  406. *
  407. * @package OpenID
  408. */
  409. class Auth_OpenID_DiffieHellmanSHA1ServerSession {
  410. /**
  411. * An object that knows how to handle association requests with
  412. * the Diffie-Hellman session type.
  413. */
  414. var $session_type = 'DH-SHA1';
  415. var $needs_math = true;
  416. var $allowed_assoc_types = array('HMAC-SHA1');
  417. var $hash_func = 'Auth_OpenID_SHA1';
  418. function Auth_OpenID_DiffieHellmanSHA1ServerSession($dh, $consumer_pubkey)
  419. {
  420. $this->dh = $dh;
  421. $this->consumer_pubkey = $consumer_pubkey;
  422. }
  423. function getDH($message)
  424. {
  425. $dh_modulus = $message->getArg(Auth_OpenID_OPENID_NS, 'dh_modulus');
  426. $dh_gen = $message->getArg(Auth_OpenID_OPENID_NS, 'dh_gen');
  427. if ((($dh_modulus === null) && ($dh_gen !== null)) ||
  428. (($dh_gen === null) && ($dh_modulus !== null))) {
  429. if ($dh_modulus === null) {
  430. $missing = 'modulus';
  431. } else {
  432. $missing = 'generator';
  433. }
  434. return new Auth_OpenID_ServerError($message,
  435. 'If non-default modulus or generator is '.
  436. 'supplied, both must be supplied. Missing '.
  437. $missing);
  438. }
  439. $lib =& Auth_OpenID_getMathLib();
  440. if ($dh_modulus || $dh_gen) {
  441. $dh_modulus = $lib->base64ToLong($dh_modulus);
  442. $dh_gen = $lib->base64ToLong($dh_gen);
  443. if ($lib->cmp($dh_modulus, 0) == 0 ||
  444. $lib->cmp($dh_gen, 0) == 0) {
  445. return new Auth_OpenID_ServerError(
  446. $message, "Failed to parse dh_mod or dh_gen");
  447. }
  448. $dh = new Auth_OpenID_DiffieHellman($dh_modulus, $dh_gen);
  449. } else {
  450. $dh = new Auth_OpenID_DiffieHellman();
  451. }
  452. $consumer_pubkey = $message->getArg(Auth_OpenID_OPENID_NS,
  453. 'dh_consumer_public');
  454. if ($consumer_pubkey === null) {
  455. return new Auth_OpenID_ServerError($message,
  456. 'Public key for DH-SHA1 session '.
  457. 'not found in query');
  458. }
  459. $consumer_pubkey =
  460. $lib->base64ToLong($consumer_pubkey);
  461. if ($consumer_pubkey === false) {
  462. return new Auth_OpenID_ServerError($message,
  463. "dh_consumer_public is not base64");
  464. }
  465. return array($dh, $consumer_pubkey);
  466. }
  467. function fromMessage($message)
  468. {
  469. $result = Auth_OpenID_DiffieHellmanSHA1ServerSession::getDH($message);
  470. if (is_a($result, 'Auth_OpenID_ServerError')) {
  471. return $result;
  472. } else {
  473. list($dh, $consumer_pubkey) = $result;
  474. return new Auth_OpenID_DiffieHellmanSHA1ServerSession($dh,
  475. $consumer_pubkey);
  476. }
  477. }
  478. function answer($secret)
  479. {
  480. $lib =& Auth_OpenID_getMathLib();
  481. $mac_key = $this->dh->xorSecret($this->consumer_pubkey, $secret,
  482. $this->hash_func);
  483. return array(
  484. 'dh_server_public' =>
  485. $lib->longToBase64($this->dh->public),
  486. 'enc_mac_key' => base64_encode($mac_key));
  487. }
  488. }
  489. /**
  490. * A class implementing DH-SHA256 server sessions.
  491. *
  492. * @package OpenID
  493. */
  494. class Auth_OpenID_DiffieHellmanSHA256ServerSession
  495. extends Auth_OpenID_DiffieHellmanSHA1ServerSession {
  496. var $session_type = 'DH-SHA256';
  497. var $hash_func = 'Auth_OpenID_SHA256';
  498. var $allowed_assoc_types = array('HMAC-SHA256');
  499. function fromMessage($message)
  500. {
  501. $result = Auth_OpenID_DiffieHellmanSHA1ServerSession::getDH($message);
  502. if (is_a($result, 'Auth_OpenID_ServerError')) {
  503. return $result;
  504. } else {
  505. list($dh, $consumer_pubkey) = $result;
  506. return new Auth_OpenID_DiffieHellmanSHA256ServerSession($dh,
  507. $consumer_pubkey);
  508. }
  509. }
  510. }
  511. /**
  512. * A request to associate with the server.
  513. *
  514. * @package OpenID
  515. */
  516. class Auth_OpenID_AssociateRequest extends Auth_OpenID_Request {
  517. var $mode = "associate";
  518. function getSessionClasses()
  519. {
  520. return array(
  521. 'no-encryption' => 'Auth_OpenID_PlainTextServerSession',
  522. 'DH-SHA1' => 'Auth_OpenID_DiffieHellmanSHA1ServerSession',
  523. 'DH-SHA256' => 'Auth_OpenID_DiffieHellmanSHA256ServerSession');
  524. }
  525. function Auth_OpenID_AssociateRequest(&$session, $assoc_type)
  526. {
  527. $this->session =& $session;
  528. $this->namespace = Auth_OpenID_OPENID2_NS;
  529. $this->assoc_type = $assoc_type;
  530. }
  531. function fromMessage($message, $server=null)
  532. {
  533. if ($message->isOpenID1()) {
  534. $session_type = $message->getArg(Auth_OpenID_OPENID_NS,
  535. 'session_type');
  536. if ($session_type == 'no-encryption') {
  537. // oidutil.log('Received OpenID 1 request with a no-encryption '
  538. // 'assocaition session type. Continuing anyway.')
  539. } else if (!$session_type) {
  540. $session_type = 'no-encryption';
  541. }
  542. } else {
  543. $session_type = $message->getArg(Auth_OpenID_OPENID_NS,
  544. 'session_type');
  545. if ($session_type === null) {
  546. return new Auth_OpenID_ServerError($message,
  547. "session_type missing from request");
  548. }
  549. }
  550. $session_class = Auth_OpenID::arrayGet(
  551. Auth_OpenID_AssociateRequest::getSessionClasses(),
  552. $session_type);
  553. if ($session_class === null) {
  554. return new Auth_OpenID_ServerError($message,
  555. "Unknown session type " .
  556. $session_type);
  557. }
  558. $session = call_user_func(array($session_class, 'fromMessage'),
  559. $message);
  560. if (is_a($session, 'Auth_OpenID_ServerError')) {
  561. return $session;
  562. }
  563. $assoc_type = $message->getArg(Auth_OpenID_OPENID_NS,
  564. 'assoc_type', 'HMAC-SHA1');
  565. if (!in_array($assoc_type, $session->allowed_assoc_types)) {
  566. $fmt = "Session type %s does not support association type %s";
  567. return new Auth_OpenID_ServerError($message,
  568. sprintf($fmt, $session_type, $assoc_type));
  569. }
  570. $obj = new Auth_OpenID_AssociateRequest($session, $assoc_type);
  571. $obj->message = $message;
  572. $obj->namespace = $message->getOpenIDNamespace();
  573. return $obj;
  574. }
  575. function answer($assoc)
  576. {
  577. $response = new Auth_OpenID_ServerResponse($this);
  578. $response->fields->updateArgs(Auth_OpenID_OPENID_NS,
  579. array(
  580. 'expires_in' => sprintf('%d', $assoc->getExpiresIn()),
  581. 'assoc_type' => $this->assoc_type,
  582. 'assoc_handle' => $assoc->handle));
  583. $response->fields->updateArgs(Auth_OpenID_OPENID_NS,
  584. $this->session->answer($assoc->secret));
  585. if (! ($this->session->session_type == 'no-encryption'
  586. && $this->message->isOpenID1())) {
  587. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  588. 'session_type',
  589. $this->session->session_type);
  590. }
  591. return $response;
  592. }
  593. function answerUnsupported($text_message,
  594. $preferred_association_type=null,
  595. $preferred_session_type=null)
  596. {
  597. if ($this->message->isOpenID1()) {
  598. return new Auth_OpenID_ServerError($this->message);
  599. }
  600. $response = new Auth_OpenID_ServerResponse($this);
  601. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  602. 'error_code', 'unsupported-type');
  603. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  604. 'error', $text_message);
  605. if ($preferred_association_type) {
  606. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  607. 'assoc_type',
  608. $preferred_association_type);
  609. }
  610. if ($preferred_session_type) {
  611. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  612. 'session_type',
  613. $preferred_session_type);
  614. }
  615. return $response;
  616. }
  617. }
  618. /**
  619. * A request to confirm the identity of a user.
  620. *
  621. * @package OpenID
  622. */
  623. class Auth_OpenID_CheckIDRequest extends Auth_OpenID_Request {
  624. /**
  625. * Return-to verification callback. Default is
  626. * Auth_OpenID_verifyReturnTo from TrustRoot.php.
  627. */
  628. var $verifyReturnTo = 'Auth_OpenID_verifyReturnTo';
  629. /**
  630. * The mode of this request.
  631. */
  632. var $mode = "checkid_setup"; // or "checkid_immediate"
  633. /**
  634. * Whether this request is for immediate mode.
  635. */
  636. var $immediate = false;
  637. /**
  638. * The trust_root value for this request.
  639. */
  640. var $trust_root = null;
  641. /**
  642. * The OpenID namespace for this request.
  643. * deprecated since version 2.0.2
  644. */
  645. var $namespace;
  646. function make(&$message, $identity, $return_to, $trust_root = null,
  647. $immediate = false, $assoc_handle = null, $server = null)
  648. {
  649. if ($server === null) {
  650. return new Auth_OpenID_ServerError($message,
  651. "server must not be null");
  652. }
  653. if ($return_to &&
  654. !Auth_OpenID_TrustRoot::_parse($return_to)) {
  655. return new Auth_OpenID_MalformedReturnURL($message, $return_to);
  656. }
  657. $r = new Auth_OpenID_CheckIDRequest($identity, $return_to,
  658. $trust_root, $immediate,
  659. $assoc_handle, $server);
  660. $r->namespace = $message->getOpenIDNamespace();
  661. $r->message =& $message;
  662. if (!$r->trustRootValid()) {
  663. return new Auth_OpenID_UntrustedReturnURL($message,
  664. $return_to,
  665. $trust_root);
  666. } else {
  667. return $r;
  668. }
  669. }
  670. function Auth_OpenID_CheckIDRequest($identity, $return_to,
  671. $trust_root = null, $immediate = false,
  672. $assoc_handle = null, $server = null,
  673. $claimed_id = null)
  674. {
  675. $this->namespace = Auth_OpenID_OPENID2_NS;
  676. $this->assoc_handle = $assoc_handle;
  677. $this->identity = $identity;
  678. if ($claimed_id === null) {
  679. $this->claimed_id = $identity;
  680. } else {
  681. $this->claimed_id = $claimed_id;
  682. }
  683. $this->return_to = $return_to;
  684. $this->trust_root = $trust_root;
  685. $this->server =& $server;
  686. if ($immediate) {
  687. $this->immediate = true;
  688. $this->mode = "checkid_immediate";
  689. } else {
  690. $this->immediate = false;
  691. $this->mode = "checkid_setup";
  692. }
  693. }
  694. function equals($other)
  695. {
  696. return (
  697. (is_a($other, 'Auth_OpenID_CheckIDRequest')) &&
  698. ($this->namespace == $other->namespace) &&
  699. ($this->assoc_handle == $other->assoc_handle) &&
  700. ($this->identity == $other->identity) &&
  701. ($this->claimed_id == $other->claimed_id) &&
  702. ($this->return_to == $other->return_to) &&
  703. ($this->trust_root == $other->trust_root));
  704. }
  705. /*
  706. * Does the relying party publish the return_to URL for this
  707. * response under the realm? It is up to the provider to set a
  708. * policy for what kinds of realms should be allowed. This
  709. * return_to URL verification reduces vulnerability to data-theft
  710. * attacks based on open proxies, corss-site-scripting, or open
  711. * redirectors.
  712. *
  713. * This check should only be performed after making sure that the
  714. * return_to URL matches the realm.
  715. *
  716. * @return true if the realm publishes a document with the
  717. * return_to URL listed, false if not or if discovery fails
  718. */
  719. function returnToVerified()
  720. {
  721. return call_user_func_array($this->verifyReturnTo,
  722. array($this->trust_root, $this->return_to));
  723. }
  724. function fromMessage(&$message, $server)
  725. {
  726. $mode = $message->getArg(Auth_OpenID_OPENID_NS, 'mode');
  727. $immediate = null;
  728. if ($mode == "checkid_immediate") {
  729. $immediate = true;
  730. $mode = "checkid_immediate";
  731. } else {
  732. $immediate = false;
  733. $mode = "checkid_setup";
  734. }
  735. $return_to = $message->getArg(Auth_OpenID_OPENID_NS,
  736. 'return_to');
  737. if (($message->isOpenID1()) &&
  738. (!$return_to)) {
  739. $fmt = "Missing required field 'return_to' from checkid request";
  740. return new Auth_OpenID_ServerError($message, $fmt);
  741. }
  742. $identity = $message->getArg(Auth_OpenID_OPENID_NS,
  743. 'identity');
  744. $claimed_id = $message->getArg(Auth_OpenID_OPENID_NS, 'claimed_id');
  745. if ($message->isOpenID1()) {
  746. if ($identity === null) {
  747. $s = "OpenID 1 message did not contain openid.identity";
  748. return new Auth_OpenID_ServerError($message, $s);
  749. }
  750. } else {
  751. if ($identity && !$claimed_id) {
  752. $s = "OpenID 2.0 message contained openid.identity but not " .
  753. "claimed_id";
  754. return new Auth_OpenID_ServerError($message, $s);
  755. } else if ($claimed_id && !$identity) {
  756. $s = "OpenID 2.0 message contained openid.claimed_id " .
  757. "but not identity";
  758. return new Auth_OpenID_ServerError($message, $s);
  759. }
  760. }
  761. // There's a case for making self.trust_root be a TrustRoot
  762. // here. But if TrustRoot isn't currently part of the
  763. // "public" API, I'm not sure it's worth doing.
  764. if ($message->isOpenID1()) {
  765. $trust_root_param = 'trust_root';
  766. } else {
  767. $trust_root_param = 'realm';
  768. }
  769. $trust_root = $message->getArg(Auth_OpenID_OPENID_NS,
  770. $trust_root_param);
  771. if (! $trust_root) {
  772. $trust_root = $return_to;
  773. }
  774. if (! $message->isOpenID1() &&
  775. ($return_to === null) &&
  776. ($trust_root === null)) {
  777. return new Auth_OpenID_ServerError($message,
  778. "openid.realm required when openid.return_to absent");
  779. }
  780. $assoc_handle = $message->getArg(Auth_OpenID_OPENID_NS,
  781. 'assoc_handle');
  782. $obj = Auth_OpenID_CheckIDRequest::make($message,
  783. $identity,
  784. $return_to,
  785. $trust_root,
  786. $immediate,
  787. $assoc_handle,
  788. $server);
  789. if (is_a($obj, 'Auth_OpenID_ServerError')) {
  790. return $obj;
  791. }
  792. $obj->claimed_id = $claimed_id;
  793. return $obj;
  794. }
  795. function idSelect()
  796. {
  797. // Is the identifier to be selected by the IDP?
  798. // So IDPs don't have to import the constant
  799. return $this->identity == Auth_OpenID_IDENTIFIER_SELECT;
  800. }
  801. function trustRootValid()
  802. {
  803. if (!$this->trust_root) {
  804. return true;
  805. }
  806. $tr = Auth_OpenID_TrustRoot::_parse($this->trust_root);
  807. if ($tr === false) {
  808. return new Auth_OpenID_MalformedTrustRoot($this->message,
  809. $this->trust_root);
  810. }
  811. if ($this->return_to !== null) {
  812. return Auth_OpenID_TrustRoot::match($this->trust_root,
  813. $this->return_to);
  814. } else {
  815. return true;
  816. }
  817. }
  818. /**
  819. * Respond to this request. Return either an
  820. * {@link Auth_OpenID_ServerResponse} or
  821. * {@link Auth_OpenID_ServerError}.
  822. *
  823. * @param bool $allow Allow this user to claim this identity, and
  824. * allow the consumer to have this information?
  825. *
  826. * @param string $server_url DEPRECATED. Passing $op_endpoint to
  827. * the {@link Auth_OpenID_Server} constructor makes this optional.
  828. *
  829. * When an OpenID 1.x immediate mode request does not succeed, it
  830. * gets back a URL where the request may be carried out in a
  831. * not-so-immediate fashion. Pass my URL in here (the fully
  832. * qualified address of this server's endpoint, i.e.
  833. * http://example.com/server), and I will use it as a base for the
  834. * URL for a new request.
  835. *
  836. * Optional for requests where {@link $immediate} is false or
  837. * $allow is true.
  838. *
  839. * @param string $identity The OP-local identifier to answer with.
  840. * Only for use when the relying party requested identifier
  841. * selection.
  842. *
  843. * @param string $claimed_id The claimed identifier to answer
  844. * with, for use with identifier selection in the case where the
  845. * claimed identifier and the OP-local identifier differ,
  846. * i.e. when the claimed_id uses delegation.
  847. *
  848. * If $identity is provided but this is not, $claimed_id will
  849. * default to the value of $identity. When answering requests
  850. * that did not ask for identifier selection, the response
  851. * $claimed_id will default to that of the request.
  852. *
  853. * This parameter is new in OpenID 2.0.
  854. *
  855. * @return mixed
  856. */
  857. function answer($allow, $server_url = null, $identity = null,
  858. $claimed_id = null)
  859. {
  860. if (!$this->return_to) {
  861. return new Auth_OpenID_NoReturnToError();
  862. }
  863. if (!$server_url) {
  864. if ((!$this->message->isOpenID1()) &&
  865. (!$this->server->op_endpoint)) {
  866. return new Auth_OpenID_ServerError(null,
  867. "server should be constructed with op_endpoint to " .
  868. "respond to OpenID 2.0 messages.");
  869. }
  870. $server_url = $this->server->op_endpoint;
  871. }
  872. if ($allow) {
  873. $mode = 'id_res';
  874. } else if ($this->message->isOpenID1()) {
  875. if ($this->immediate) {
  876. $mode = 'id_res';
  877. } else {
  878. $mode = 'cancel';
  879. }
  880. } else {
  881. if ($this->immediate) {
  882. $mode = 'setup_needed';
  883. } else {
  884. $mode = 'cancel';
  885. }
  886. }
  887. if (!$this->trustRootValid()) {
  888. return new Auth_OpenID_UntrustedReturnURL(null,
  889. $this->return_to,
  890. $this->trust_root);
  891. }
  892. $response = new Auth_OpenID_ServerResponse($this);
  893. if ($claimed_id &&
  894. ($this->message->isOpenID1())) {
  895. return new Auth_OpenID_ServerError(null,
  896. "claimed_id is new in OpenID 2.0 and not " .
  897. "available for ".$this->namespace);
  898. }
  899. if ($identity && !$claimed_id) {
  900. $claimed_id = $identity;
  901. }
  902. if ($allow) {
  903. if ($this->identity == Auth_OpenID_IDENTIFIER_SELECT) {
  904. if (!$identity) {
  905. return new Auth_OpenID_ServerError(null,
  906. "This request uses IdP-driven identifier selection. " .
  907. "You must supply an identifier in the response.");
  908. }
  909. $response_identity = $identity;
  910. $response_claimed_id = $claimed_id;
  911. } else if ($this->identity) {
  912. if ($identity &&
  913. ($this->identity != $identity)) {
  914. $fmt = "Request was for %s, cannot reply with identity %s";
  915. return new Auth_OpenID_ServerError(null,
  916. sprintf($fmt, $this->identity, $identity));
  917. }
  918. $response_identity = $this->identity;
  919. $response_claimed_id = $this->claimed_id;
  920. } else {
  921. if ($identity) {
  922. return new Auth_OpenID_ServerError(null,
  923. "This request specified no identity and " .
  924. "you supplied ".$identity);
  925. }
  926. $response_identity = null;
  927. }
  928. if (($this->message->isOpenID1()) &&
  929. ($response_identity === null)) {
  930. return new Auth_OpenID_ServerError(null,
  931. "Request was an OpenID 1 request, so response must " .
  932. "include an identifier.");
  933. }
  934. $response->fields->updateArgs(Auth_OpenID_OPENID_NS,
  935. array('mode' => $mode,
  936. 'return_to' => $this->return_to,
  937. 'response_nonce' => Auth_OpenID_mkNonce()));
  938. if (!$this->message->isOpenID1()) {
  939. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  940. 'op_endpoint', $server_url);
  941. }
  942. if ($response_identity !== null) {
  943. $response->fields->setArg(
  944. Auth_OpenID_OPENID_NS,
  945. 'identity',
  946. $response_identity);
  947. if ($this->message->isOpenID2()) {
  948. $response->fields->setArg(
  949. Auth_OpenID_OPENID_NS,
  950. 'claimed_id',
  951. $response_claimed_id);
  952. }
  953. }
  954. } else {
  955. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  956. 'mode', $mode);
  957. if ($this->immediate) {
  958. if (($this->message->isOpenID1()) &&
  959. (!$server_url)) {
  960. return new Auth_OpenID_ServerError(null,
  961. 'setup_url is required for $allow=false \
  962. in OpenID 1.x immediate mode.');
  963. }
  964. $setup_request =& new Auth_OpenID_CheckIDRequest(
  965. $this->identity,
  966. $this->return_to,
  967. $this->trust_root,
  968. false,
  969. $this->assoc_handle,
  970. $this->server,
  971. $this->claimed_id);
  972. $setup_request->message = $this->message;
  973. $setup_url = $setup_request->encodeToURL($server_url);
  974. if ($setup_url === null) {
  975. return new Auth_OpenID_NoReturnToError();
  976. }
  977. $response->fields->setArg(Auth_OpenID_OPENID_NS,
  978. 'user_setup_url',
  979. $setup_url);
  980. }
  981. }
  982. return $response;
  983. }
  984. function encodeToURL($server_url)
  985. {
  986. if (!$this->return_to) {
  987. return new Auth_OpenID_NoReturnToError();
  988. }
  989. // Imported from the alternate reality where these classes are
  990. // used in both the client and server code, so Requests are
  991. // Encodable too. That's right, code imported from alternate
  992. // realities all for the love of you, id_res/user_setup_url.
  993. $q = array('mode' => $this->mode,
  994. 'identity' => $this->identity,
  995. 'claimed_id' => $this->claimed_id,
  996. 'return_to' => $this->return_to);
  997. if ($this->trust_root) {
  998. if ($this->message->isOpenID1()) {
  999. $q['trust_root'] = $this->trust_root;
  1000. } else {
  1001. $q['realm'] = $this->trust_root;
  1002. }
  1003. }
  1004. if ($this->assoc_handle) {
  1005. $q['assoc_handle'] = $this->assoc_handle;
  1006. }
  1007. $response = new Auth_OpenID_Message(
  1008. $this->message->getOpenIDNamespace());
  1009. $response->updateArgs(Auth_OpenID_OPENID_NS, $q);
  1010. return $response->toURL($server_url);
  1011. }
  1012. function getCancelURL()
  1013. {
  1014. if (!$this->return_to) {
  1015. return new Auth_OpenID_NoReturnToError();
  1016. }
  1017. if ($this->immediate) {
  1018. return new Auth_OpenID_ServerError(null,
  1019. "Cancel is not an appropriate \
  1020. response to immediate mode \
  1021. requests.");
  1022. }
  1023. $response = new Auth_OpenID_Message(
  1024. $this->message->getOpenIDNamespace());
  1025. $response->setArg(Auth_OpenID_OPENID_NS, 'mode', 'cancel');
  1026. return $response->toURL($this->return_to);
  1027. }
  1028. }
  1029. /**
  1030. * This class encapsulates the response to an OpenID server request.
  1031. *
  1032. * @package OpenID
  1033. */
  1034. class Auth_OpenID_ServerResponse {
  1035. function Auth_OpenID_ServerResponse(&$request)
  1036. {
  1037. $this->request =& $request;
  1038. $this->fields = new Auth_OpenID_Message($this->request->namespace);
  1039. }
  1040. function whichEncoding()
  1041. {
  1042. global $_Auth_OpenID_Request_Modes;
  1043. if (in_array($this->request->mode, $_Auth_OpenID_Request_Modes)) {
  1044. if ($this->fields->isOpenID2() &&
  1045. (strlen($this->encodeToURL()) >
  1046. Auth_OpenID_OPENID1_URL_LIMIT)) {
  1047. return Auth_OpenID_ENCODE_HTML_FORM;
  1048. } else {
  1049. return Auth_OpenID_ENCODE_URL;
  1050. }
  1051. } else {
  1052. return Auth_OpenID_ENCODE_KVFORM;
  1053. }
  1054. }
  1055. /*
  1056. * Returns the form markup for this response.
  1057. *
  1058. * @return str
  1059. */
  1060. function toFormMarkup($form_tag_attrs=null)
  1061. {
  1062. return $this->fields->toFormMarkup($this->request->return_to,
  1063. $form_tag_attrs);
  1064. }
  1065. /*
  1066. * Returns an HTML document containing the form markup for this
  1067. * response that autosubmits with javascript.
  1068. */
  1069. function toHTML()
  1070. {
  1071. return Auth_OpenID::autoSubmitHTML($this->toFormMarkup());
  1072. }
  1073. /*
  1074. * Returns True if this response's encoding is ENCODE_HTML_FORM.
  1075. * Convenience method for server authors.
  1076. *
  1077. * @return bool
  1078. */
  1079. function renderAsForm()
  1080. {
  1081. return $this->whichEncoding() == Auth_OpenID_ENCODE_HTML_FORM;
  1082. }
  1083. function encodeToURL()
  1084. {
  1085. return $this->fields->toURL($this->request->return_to);
  1086. }
  1087. function addExtension($extension_response)
  1088. {
  1089. $extension_response->toMessage($this->fields);
  1090. }
  1091. function needsSigning()
  1092. {
  1093. return $this->fields->getArg(Auth_OpenID_OPENID_NS,
  1094. 'mode') == 'id_res';
  1095. }
  1096. function encodeToKVForm()
  1097. {
  1098. return $this->fields->toKVForm();
  1099. }
  1100. }
  1101. /**
  1102. * A web-capable response object which you can use to generate a
  1103. * user-agent response.
  1104. *
  1105. * @package OpenID
  1106. */
  1107. class Auth_OpenID_WebResponse {
  1108. var $code = AUTH_OPENID_HTTP_OK;
  1109. var $body = "";
  1110. function Auth_OpenID_WebResponse($code = null, $headers = null,
  1111. $body = null)
  1112. {
  1113. if ($code) {
  1114. $this->code = $code;
  1115. }
  1116. if ($headers !== null) {
  1117. $this->headers = $headers;
  1118. } else {
  1119. $this->headers = array();
  1120. }
  1121. if ($body !== null) {
  1122. $this->body = $body;
  1123. }
  1124. }
  1125. }
  1126. /**
  1127. * Responsible for the signature of query data and the verification of
  1128. * OpenID signature values.
  1129. *
  1130. * @package OpenID
  1131. */
  1132. class Auth_OpenID_Signatory {
  1133. // = 14 * 24 * 60 * 60; # 14 days, in seconds
  1134. var $SECRET_LIFETIME = 1209600;
  1135. // keys have a bogus server URL in them because the filestore
  1136. // really does expect that key to be a URL. This seems a little
  1137. // silly for the server store, since I expect there to be only one
  1138. // server URL.
  1139. var $normal_key = 'http://localhost/|normal';
  1140. var $dumb_key = 'http://localhost/|dumb';
  1141. /**
  1142. * Create a new signatory using a given store.
  1143. */
  1144. function Auth_OpenID_Signatory(&$store)
  1145. {
  1146. // assert store is not None
  1147. $this->store =& $store;
  1148. }
  1149. /**
  1150. * Verify, using a given association handle, a signature with
  1151. * signed key-value pairs from an HTTP request.
  1152. */
  1153. function verify($assoc_handle, $message)
  1154. {
  1155. $assoc = $this->getAssociation($assoc_handle, true);
  1156. if (!$assoc) {
  1157. // oidutil.log("failed to get assoc with handle %r to verify sig %r"
  1158. // % (assoc_handle, sig))
  1159. return false;
  1160. }
  1161. return $assoc->checkMessageSignature($message);
  1162. }
  1163. /**
  1164. * Given a response, sign the fields in the response's 'signed'
  1165. * list, and insert the signature into the response.
  1166. */
  1167. function sign($response)
  1168. {
  1169. $signed_response = $response;
  1170. $assoc_handle = $response->request->assoc_handle;
  1171. if ($assoc_handle) {
  1172. // normal mode
  1173. $assoc = $this->getAssociation($assoc_handle, false, false);
  1174. if (!$assoc || ($assoc->getExpiresIn() <= 0)) {
  1175. // fall back to dumb mode
  1176. $signed_response->fields->setArg(Auth_OpenID_OPENID_NS,
  1177. 'invalidate_handle', $assoc_handle);
  1178. $assoc_type = ($assoc ? $assoc->assoc_type : 'HMAC-SHA1');
  1179. if ($assoc && ($assoc->getExpiresIn() <= 0)) {
  1180. $this->invalidate($assoc_handle, false);
  1181. }
  1182. $assoc = $this->createAssociation(true, $assoc_type);
  1183. }
  1184. } else {
  1185. // dumb mode.
  1186. $assoc = $this->createAssociation(true);
  1187. }
  1188. $signed_response->fields = $assoc->signMessage(
  1189. $signed_response->fields);
  1190. return $signed_response;
  1191. }
  1192. /**
  1193. * Make a new association.
  1194. */
  1195. function createAssociation($dumb = true, $assoc_type = 'HMAC-SHA1')
  1196. {
  1197. $secret = Auth_OpenID_CryptUtil::getBytes(
  1198. Auth_OpenID_getSecretSize($assoc_type));
  1199. $uniq = base64_encode(Auth_OpenID_CryptUtil::getBytes(4));
  1200. $handle = sprintf('{%s}{%x}{%s}', $assoc_type, intval(time()), $uniq);
  1201. $assoc = Auth_OpenID_Association::fromExpiresIn(
  1202. $this->SECRET_LIFETIME, $handle, $secret, $assoc_type);
  1203. if ($dumb) {
  1204. $key = $this->dumb_key;
  1205. } else {
  1206. $key = $this->normal_key;
  1207. }
  1208. $this->store->storeAssociation($key, $assoc);
  1209. return $assoc;
  1210. }
  1211. /**
  1212. * Given an association handle, get the association from the
  1213. * store, or return a ServerError or null if something goes wrong.
  1214. */
  1215. function getAssociation($assoc_handle, $dumb, $check_expiration=true)
  1216. {
  1217. if ($assoc_handle === null) {
  1218. return new Auth_OpenID_ServerError(null,
  1219. "assoc_handle must not be null");
  1220. }
  1221. if ($dumb) {
  1222. $key = $this->dumb_key;
  1223. } else {
  1224. $key = $this->normal_key;
  1225. }
  1226. $assoc = $this->store->getAssociation($key, $assoc_handle);
  1227. if (($assoc !== null) && ($assoc->getExpiresIn() <= 0)) {
  1228. if ($check_expiration) {
  1229. $this->store->removeAssociation($key, $assoc_handle);
  1230. $assoc = null;
  1231. }
  1232. }
  1233. return $assoc;
  1234. }
  1235. /**
  1236. * Invalidate a given association handle.
  1237. */
  1238. function invalidate($assoc_handle, $dumb)
  1239. {
  1240. if ($dumb) {
  1241. $key = $this->dumb_key;
  1242. } else {
  1243. $key = $this->normal_key;
  1244. }
  1245. $this->store->removeAssociation($key, $assoc_handle);
  1246. }
  1247. }
  1248. /**
  1249. * Encode an {@link Auth_OpenID_ServerResponse} to an
  1250. * {@link Auth_OpenID_WebResponse}.
  1251. *
  1252. * @package OpenID
  1253. */
  1254. class Auth_OpenID_Encoder {
  1255. var $responseFactory = 'Auth_OpenID_WebResponse';
  1256. /**
  1257. * Encode an {@link Auth_OpenID_ServerResponse} and return an
  1258. * {@link Auth_OpenID_WebResponse}.
  1259. */
  1260. function encode(&$response)
  1261. {
  1262. $cls = $this->responseFactory;
  1263. $encode_as = $response->whichEncoding();
  1264. if ($encode_as == Auth_OpenID_ENCODE_KVFORM) {
  1265. $wr = new $cls(null, null, $response->encodeToKVForm());
  1266. if (is_a($response, 'Auth_OpenID_ServerError')) {
  1267. $wr->code = AUTH_OPENID_HTTP_ERROR;
  1268. }
  1269. } else if ($encode_as == Auth_OpenID_ENCODE_URL) {
  1270. $location = $response->encodeToURL();
  1271. $wr = new $cls(AUTH_OPENID_HTTP_REDIRECT,
  1272. array('location' => $location));
  1273. } else if ($encode_as == Auth_OpenID_ENCODE_HTML_FORM) {
  1274. $wr = new $cls(AUTH_OPENID_HTTP_OK, array(),
  1275. $response->toFormMarkup());
  1276. } else {
  1277. return new Auth_OpenID_EncodingError($response);
  1278. }
  1279. return $wr;
  1280. }
  1281. }
  1282. /**
  1283. * An encoder which also takes care of signing fields when required.
  1284. *
  1285. * @package OpenID
  1286. */
  1287. class Auth_OpenID_SigningEncoder extends Auth_OpenID_Encoder {
  1288. function Auth_OpenID_SigningEncoder(&$signatory)
  1289. {
  1290. $this->signatory =& $signatory;
  1291. }
  1292. /**
  1293. * Sign an {@link Auth_OpenID_ServerResponse} and return an
  1294. * {@link Auth_OpenID_WebResponse}.
  1295. */
  1296. function encode(&$response)
  1297. {
  1298. // the isinstance is a bit of a kludge... it means there isn't
  1299. // really an adapter to make the interfaces quite match.
  1300. if (!is_a($response, 'Auth_OpenID_ServerError') &&
  1301. $response->needsSigning()) {
  1302. if (!$this->signatory) {
  1303. return new Auth_OpenID_ServerError(null,
  1304. "Must have a store to sign request");
  1305. }
  1306. if ($response->fields->hasKey(Auth_OpenID_OPENID_NS, 'sig')) {
  1307. return new Auth_OpenID_AlreadySigned($response);
  1308. }
  1309. $response = $this->signatory->sign($response);
  1310. }
  1311. return parent::encode($response);
  1312. }
  1313. }
  1314. /**
  1315. * Decode an incoming query into an Auth_OpenID_Request.
  1316. *
  1317. * @package OpenID
  1318. */
  1319. class Auth_OpenID_Decoder {
  1320. function Auth_OpenID_Decoder(&$server)
  1321. {
  1322. $this->server =& $server;
  1323. $this->handlers = array(
  1324. 'checkid_setup' => 'Auth_OpenID_CheckIDRequest',
  1325. 'checkid_immediate' => 'Auth_OpenID_CheckIDRequest',
  1326. 'check_authentication' => 'Auth_OpenID_CheckAuthRequest',
  1327. 'associate' => 'Auth_OpenID_AssociateRequest'
  1328. );
  1329. }
  1330. /**
  1331. * Given an HTTP query in an array (key-value pairs), decode it
  1332. * into an Auth_OpenID_Request object.
  1333. */
  1334. function decode($query)
  1335. {
  1336. if (!$query) {
  1337. return null;
  1338. }
  1339. $message = Auth_OpenID_Message::fromPostArgs($query);
  1340. if ($mess

Large files files are truncated, but you can click here to view the full file