/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
- <?php
-
- /*
- * This file is part of Xerxes.
- *
- * (c) California State University <library@calstate.edu>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Xerxes\Lti;
-
- use Xerxes\Utility\Parser;
-
- require_once __DIR__ . '/../OAuth/OAuth.php'; // @todo: make this not hardwired
-
- /**
- * Convenience class for verifying and accessing properties of basic lti launch request
- *
- * @author David Walker <dwalker@calstate.edu>
- */
-
- class Basic
- {
- /**
- * @var OAuthRequest
- */
-
- protected $request;
-
- /**
- * Create new Basic LTI access object
- *
- * @param string $key
- * @param string $secret
- *
- * @throws \Exception
- */
-
- public function __construct( $key, $secret )
- {
- $request = \OAuthRequest::from_request();
-
- $oauth_consumer_key = $request->get_parameter("oauth_consumer_key");
-
- // ensure the key in the request matches the locally supplied one
-
- if ( $oauth_consumer_key == null)
- {
- throw new \Exception("Missing oauth_consumer_key in request");
- }
-
- if ( $oauth_consumer_key != $key )
- {
- throw new \Exception("oauth_consumer_key doesn't match supplied key");
- }
-
- // verify the message signature
-
- $store = new TrivialOAuthDataStore( $oauth_consumer_key, $secret );
- $server = new \OAuthServer( $store );
-
- $method = new \OAuthSignatureMethod_HMAC_SHA1();
- $server->add_signature_method( $method );
-
- $server->verify_request( $request );
-
- $this->request = $request;
- }
-
- /**
- * Get request parameter
- *
- * @param string $name
- */
-
- public function getParam($name)
- {
- return $this->request->get_parameter($name);
- }
-
- /**
- * Create a unique identifier for this LTI context
- *
- * oauth_consumer_key + context_id + resource_link_id
- *
- * @return string
- */
-
- public function getID()
- {
- return $this->getParam("oauth_consumer_key") . "-"
- . $this->getParam('context_id') . "-"
- . $this->getParam('resource_link_id');
- }
-
- /**
- * User ID
- *
- * @return string
- */
-
- public function getUserID()
- {
- return $this->getParam('user_id');
- }
-
- /**
- * Whether the user is an Instructor
- *
- * @return bool
- */
-
- public function isInstructor()
- {
- $roles = explode(',', $this->getParam('roles'));
-
- if ( in_array('Instructor', $roles) )
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- /**
- * Serialize the objet to XML
- */
-
- public function toXML()
- {
- $xml = Parser::convertToDOMDocument("<lti />");
-
- $this->appendElement($xml, "id", $this->getID());
- $this->appendElement($xml, "instructor", $this->isInstructor());
-
- foreach ( $this->request->get_parameters() as $id => $value )
- {
- $this->appendElement($xml, $id, $value);
- }
-
- return $xml;
- }
-
- /**
- * Append an item to the xml
- *
- * @param DOMDocument $xml
- * @param string $id
- * @param mixed $value
- */
-
- private function appendElement(&$xml, $id, $value)
- {
- $new = $xml->createElement($id, Parser::escapeXml($value));
- $xml->documentElement->appendChild($new);
- }
- }
-
- /**
- * A Trivial memory-based store - no support for tokens
- */
-
- class TrivialOAuthDataStore extends \OAuthDataStore
- {
- private $consumers = array();
-
- public function __construct($consumer_key, $consumer_secret)
- {
- $this->consumers[$consumer_key] = $consumer_secret;
- }
-
- public function lookup_consumer($consumer_key)
- {
- if ( strpos($consumer_key, "http://" ) === 0 )
- {
- $consumer = new \OAuthConsumer($consumer_key,"secret", NULL);
- return $consumer;
- }
-
- if ( $this->consumers[$consumer_key] )
- {
- $consumer = new \OAuthConsumer($consumer_key,$this->consumers[$consumer_key], NULL);
- return $consumer;
- }
- return NULL;
- }
-
- public function lookup_token($consumer, $token_type, $token)
- {
- return new \OAuthToken($consumer, "");
- }
-
- public function lookup_nonce($consumer, $token, $nonce, $timestamp)
- {
- // Should add some clever logic to keep nonces from
- // being reused - for no we are really trusting
- // that the timestamp will save us
- return NULL;
- }
-
- public function new_request_token($consumer)
- {
- return NULL;
- }
-
- public function new_access_token($token, $consumer)
- {
- return NULL;
- }
- }