PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/lib/Auth/OpenID/Server.php

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

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