/application/src/Xerxes/Lti/Basic.php

https://github.com/dswalker/xerxes · PHP · 214 lines · 176 code · 13 blank · 25 comment · 4 complexity · 968d30806070acfb72210d9474f7f19d MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Xerxes.
  4. *
  5. * (c) California State University <library@calstate.edu>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Xerxes\Lti;
  11. use Xerxes\Utility\Parser;
  12. require_once __DIR__ . '/../OAuth/OAuth.php'; // @todo: make this not hardwired
  13. /**
  14. * Convenience class for verifying and accessing properties of basic lti launch request
  15. *
  16. * @author David Walker <dwalker@calstate.edu>
  17. */
  18. class Basic
  19. {
  20. /**
  21. * @var OAuthRequest
  22. */
  23. protected $request;
  24. /**
  25. * Create new Basic LTI access object
  26. *
  27. * @param string $key
  28. * @param string $secret
  29. *
  30. * @throws \Exception
  31. */
  32. public function __construct( $key, $secret )
  33. {
  34. $request = \OAuthRequest::from_request();
  35. $oauth_consumer_key = $request->get_parameter("oauth_consumer_key");
  36. // ensure the key in the request matches the locally supplied one
  37. if ( $oauth_consumer_key == null)
  38. {
  39. throw new \Exception("Missing oauth_consumer_key in request");
  40. }
  41. if ( $oauth_consumer_key != $key )
  42. {
  43. throw new \Exception("oauth_consumer_key doesn't match supplied key");
  44. }
  45. // verify the message signature
  46. $store = new TrivialOAuthDataStore( $oauth_consumer_key, $secret );
  47. $server = new \OAuthServer( $store );
  48. $method = new \OAuthSignatureMethod_HMAC_SHA1();
  49. $server->add_signature_method( $method );
  50. $server->verify_request( $request );
  51. $this->request = $request;
  52. }
  53. /**
  54. * Get request parameter
  55. *
  56. * @param string $name
  57. */
  58. public function getParam($name)
  59. {
  60. return $this->request->get_parameter($name);
  61. }
  62. /**
  63. * Create a unique identifier for this LTI context
  64. *
  65. * oauth_consumer_key + context_id + resource_link_id
  66. *
  67. * @return string
  68. */
  69. public function getID()
  70. {
  71. return $this->getParam("oauth_consumer_key") . "-"
  72. . $this->getParam('context_id') . "-"
  73. . $this->getParam('resource_link_id');
  74. }
  75. /**
  76. * User ID
  77. *
  78. * @return string
  79. */
  80. public function getUserID()
  81. {
  82. return $this->getParam('user_id');
  83. }
  84. /**
  85. * Whether the user is an Instructor
  86. *
  87. * @return bool
  88. */
  89. public function isInstructor()
  90. {
  91. $roles = explode(',', $this->getParam('roles'));
  92. if ( in_array('Instructor', $roles) )
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Serialize the objet to XML
  103. */
  104. public function toXML()
  105. {
  106. $xml = Parser::convertToDOMDocument("<lti />");
  107. $this->appendElement($xml, "id", $this->getID());
  108. $this->appendElement($xml, "instructor", $this->isInstructor());
  109. foreach ( $this->request->get_parameters() as $id => $value )
  110. {
  111. $this->appendElement($xml, $id, $value);
  112. }
  113. return $xml;
  114. }
  115. /**
  116. * Append an item to the xml
  117. *
  118. * @param DOMDocument $xml
  119. * @param string $id
  120. * @param mixed $value
  121. */
  122. private function appendElement(&$xml, $id, $value)
  123. {
  124. $new = $xml->createElement($id, Parser::escapeXml($value));
  125. $xml->documentElement->appendChild($new);
  126. }
  127. }
  128. /**
  129. * A Trivial memory-based store - no support for tokens
  130. */
  131. class TrivialOAuthDataStore extends \OAuthDataStore
  132. {
  133. private $consumers = array();
  134. public function __construct($consumer_key, $consumer_secret)
  135. {
  136. $this->consumers[$consumer_key] = $consumer_secret;
  137. }
  138. public function lookup_consumer($consumer_key)
  139. {
  140. if ( strpos($consumer_key, "http://" ) === 0 )
  141. {
  142. $consumer = new \OAuthConsumer($consumer_key,"secret", NULL);
  143. return $consumer;
  144. }
  145. if ( $this->consumers[$consumer_key] )
  146. {
  147. $consumer = new \OAuthConsumer($consumer_key,$this->consumers[$consumer_key], NULL);
  148. return $consumer;
  149. }
  150. return NULL;
  151. }
  152. public function lookup_token($consumer, $token_type, $token)
  153. {
  154. return new \OAuthToken($consumer, "");
  155. }
  156. public function lookup_nonce($consumer, $token, $nonce, $timestamp)
  157. {
  158. // Should add some clever logic to keep nonces from
  159. // being reused - for no we are really trusting
  160. // that the timestamp will save us
  161. return NULL;
  162. }
  163. public function new_request_token($consumer)
  164. {
  165. return NULL;
  166. }
  167. public function new_access_token($token, $consumer)
  168. {
  169. return NULL;
  170. }
  171. }