コード例 #1
0
ファイル: class.Service.php プロジェクト: nagyist/tao-core
 /**
  * Adds a signature to the request
  *
  * @access public
  * @author Joel Bout, <joel@taotesting.com>
  * @param $authorizationHeader Move the signature parameters into the Authorization header of the request
  */
 public function sign(common_http_Request $request, common_http_Credentials $credentials, $authorizationHeader = false)
 {
     if (!$credentials instanceof tao_models_classes_oauth_Credentials) {
         throw new tao_models_classes_oauth_Exception('Invalid credentals: ' . gettype($credentials));
     }
     $oauthRequest = $this->getOauthRequest($request);
     $dataStore = new tao_models_classes_oauth_DataStore();
     $consumer = $dataStore->getOauthConsumer($credentials);
     $token = $dataStore->new_request_token($consumer);
     $allInitialParameters = array();
     $allInitialParameters = array_merge($allInitialParameters, $request->getParams());
     $allInitialParameters = array_merge($allInitialParameters, $request->getHeaders());
     //oauth_body_hash is used for the signing computation
     if ($authorizationHeader) {
         $oauth_body_hash = base64_encode(sha1($request->getBody(), true));
         //the signature should be ciomputed from encoded versions
         $allInitialParameters = array_merge($allInitialParameters, array("oauth_body_hash" => $oauth_body_hash));
     }
     //$authorizationHeader = self::buildAuthorizationHeader($signatureParameters);
     $signedRequest = OAuthRequest::from_consumer_and_token($consumer, $token, $oauthRequest->get_normalized_http_method(), $oauthRequest->getUrl(), $allInitialParameters);
     $signature_method = new OAuthSignatureMethod_HMAC_SHA1();
     //common_logger::d('Base string: '.$signedRequest->get_signature_base_string());
     $signedRequest->sign_request($signature_method, $consumer, $token);
     common_logger::d('Base string from TAO/Joel: ' . $signedRequest->get_signature_base_string());
     if ($authorizationHeader) {
         $combinedParameters = $signedRequest->get_parameters();
         $signatureParameters = array_diff_assoc($combinedParameters, $allInitialParameters);
         $signatureParameters["oauth_body_hash"] = base64_encode(sha1($request->getBody(), true));
         $signatureHeaders = array("Authorization" => self::buildAuthorizationHeader($signatureParameters));
         $signedRequest = new common_http_Request($signedRequest->getUrl(), $signedRequest->get_normalized_http_method(), $request->getParams(), array_merge($signatureHeaders, $request->getHeaders()), $request->getBody());
     } else {
         $signedRequest = new common_http_Request($signedRequest->getUrl(), $signedRequest->get_normalized_http_method(), $signedRequest->get_parameters(), $request->getHeaders(), $request->getBody());
     }
     return $signedRequest;
 }
コード例 #2
0
 /**
  * Get common user uri associated to Lti user id
  *
  * @param $id string Identifier of LTI user
  * @param $key string Oauth LTI consumer key
  * @return array|null
  * @throws \common_Exception
  * @throws \tao_models_classes_oauth_Exception
  */
 public function getUserId($id, $key)
 {
     $class = new \core_kernel_classes_Class(CLASS_LTI_USER);
     $dataStore = new \tao_models_classes_oauth_DataStore();
     try {
         /** @var \core_kernel_classes_Resource $consumerResource */
         $consumerResource = $dataStore->findOauthConsumerResource($key);
     } catch (\tao_models_classes_oauth_Exception $e) {
         throw new \common_exception_NotFound($e->getMessage());
     }
     $instances = $class->searchInstances(array(PROPERTY_USER_LTIKEY => $id, PROPERTY_USER_LTICONSUMER => $consumerResource), array('like' => false));
     if (count($instances) > 1) {
         throw new \common_Exception('Multiple user accounts found for user key: ' . $id);
     }
     /** @var \core_kernel_classes_Resource $ltiUser */
     $ltiUser = count($instances) == 1 ? current($instances) : null;
     if (!$ltiUser) {
         return null;
     }
     return array('id' => $ltiUser->getUri());
 }
コード例 #3
0
 public function deliveriesByProctor()
 {
     $deliveryData = array();
     if (\common_ext_ExtensionsManager::singleton()->isInstalled('ltiDeliveryProvider')) {
         /** @var DeliveryService $service */
         $service = $this->getServiceManager()->get(DeliveryService::CONFIG_ID);
         $deliveries = $service->getAccessibleDeliveries();
         if (!empty($deliveries)) {
             try {
                 $dataStore = new \tao_models_classes_oauth_DataStore();
                 $test_consumer = $dataStore->lookup_consumer('proctoring_key');
             } catch (\tao_models_classes_oauth_Exception $e) {
                 $secret = uniqid('proctoring_');
                 \taoLti_models_classes_ConsumerService::singleton()->getRootClass()->createInstanceWithProperties(array(RDFS_LABEL => 'proctoring', PROPERTY_OAUTH_KEY => 'proctoring_key', PROPERTY_OAUTH_SECRET => $secret));
                 $test_consumer = new \OAuthConsumer('proctoring_key', $secret);
             }
             $session = \common_session_SessionManager::getSession();
             $ltiData = array('lti_message_type' => 'basic-lti-launch-request', 'lti_version' => 'LTI-1p0', 'resource_link_id' => rand(0, 9999999), 'resource_link_title' => 'Launch Title', 'resource_link_label' => 'Launch label', 'context_title' => 'Launch Title', 'context_label' => 'Launch label', 'user_id' => $session->getUserUri(), 'roles' => 'Learner', 'lis_person_name_full' => $session->getUserLabel(), 'tool_consumer_info_product_family_code' => PRODUCT_NAME, 'tool_consumer_info_version' => TAO_VERSION, 'custom_skip_thankyou' => 'true', 'launch_presentation_return_url' => _url('logout', 'Main', 'tao'));
             $hmac_method = new \OAuthSignatureMethod_HMAC_SHA1();
             $test_token = new \OAuthToken($test_consumer, '');
             foreach ($deliveries as $delivery) {
                 $launchUrl = LTIDeliveryTool::singleton()->getLaunchUrl(array('delivery' => $delivery->getUri()));
                 $acc_req = \OAuthRequest::from_consumer_and_token($test_consumer, $test_token, 'GET', $launchUrl, $ltiData);
                 $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
                 $deliveryData[] = array('id' => $delivery->getUri(), 'label' => $delivery->getLabel(), 'url' => $acc_req->to_url(), 'text' => __('Test'));
             }
         }
     }
     $this->setData('title', __('Available Deliveries'));
     if (\tao_helpers_Request::isAjax()) {
         $this->returnJson(array('list' => $deliveryData));
     } else {
         try {
             $testCenter = $this->getCurrentTestCenter();
             $this->composeView('diagnostic-deliveries', array('list' => $deliveryData), array(BreadcrumbsHelper::testCenters(), BreadcrumbsHelper::testCenter($testCenter, TestCenterHelper::getTestCenters()), BreadcrumbsHelper::diagnostics($testCenter, array(BreadcrumbsHelper::deliveries($testCenter))), BreadcrumbsHelper::deliveriesByProctor($testCenter)));
         } catch (\common_Exception $e) {
             $this->composeView('diagnostic-deliveries', array('list' => $deliveryData), array(BreadcrumbsHelper::testCenters()));
         }
     }
 }
コード例 #4
0
 /**
  * Returns the LTI Consumer resource associated to this lti session
  *
  * @access public
  * @author Joel Bout, <joel@taotesting.com>
  * @return core_kernel_classes_Resource resource of LtiConsumer
  * @throws tao_models_classes_oauth_Exception thrown if no Consumer found for key
  */
 public function getLtiConsumerResource($launchData)
 {
     $dataStore = new tao_models_classes_oauth_DataStore();
     return $dataStore->findOauthConsumerResource($launchData->getOauthKey());
 }