Skip to content

Commit

Permalink
Merge branch 'MDL-67072-oidc-additional-login-parameters' of https://…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Mar 26, 2020
2 parents 70598a9 + 9b0b472 commit 9c7c2d8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
63 changes: 41 additions & 22 deletions mod/lti/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3278,7 +3278,44 @@ function lti_post_launch_html($newparms, $endpoint, $debug=false) {
*/
function lti_initiate_login($courseid, $id, $instance, $config, $messagetype = 'basic-lti-launch-request', $title = '',
$text = '') {
global $SESSION, $USER, $CFG;
global $SESSION;

$params = lti_build_login_request($courseid, $id, $instance, $config, $messagetype);
$SESSION->lti_message_hint = "{$courseid},{$config->typeid},{$id}," . base64_encode($title) . ',' .
base64_encode($text);

$r = "<form action=\"" . $config->lti_initiatelogin .
"\" name=\"ltiInitiateLoginForm\" id=\"ltiInitiateLoginForm\" method=\"post\" " .
"encType=\"application/x-www-form-urlencoded\">\n";

foreach ($params as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
$r .= " <input type=\"hidden\" name=\"{$key}\" value=\"{$value}\"/>\n";
}
$r .= "</form>\n";

$r .= "<script type=\"text/javascript\">\n" .
"//<![CDATA[\n" .
"document.ltiInitiateLoginForm.submit();\n" .
"//]]>\n" .
"</script>\n";

return $r;
}

/**
* Prepares an LTI 1.3 login request
*
* @param int $courseid Course ID
* @param int $id LTI instance ID
* @param stdClass|null $instance LTI instance
* @param stdClass $config Tool type configuration
* @param string $messagetype LTI message type
* @return array Login request parameters
*/
function lti_build_login_request($courseid, $id, $instance, $config, $messagetype) {
global $USER, $CFG;

if (!empty($instance)) {
$endpoint = !empty($instance->toolurl) ? $instance->toolurl : $config->lti_toolurl;
Expand All @@ -3302,27 +3339,9 @@ function lti_initiate_login($courseid, $id, $instance, $config, $messagetype = '
$params['target_link_uri'] = $endpoint;
$params['login_hint'] = $USER->id;
$params['lti_message_hint'] = $id;
$SESSION->lti_message_hint = "{$courseid},{$config->typeid},{$id}," . base64_encode($title) . ',' .
base64_encode($text);

$r = "<form action=\"" . $config->lti_initiatelogin .
"\" name=\"ltiInitiateLoginForm\" id=\"ltiInitiateLoginForm\" method=\"post\" " .
"encType=\"application/x-www-form-urlencoded\">\n";

foreach ($params as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
$r .= " <input type=\"hidden\" name=\"{$key}\" value=\"{$value}\"/>\n";
}
$r .= "</form>\n";

$r .= "<script type=\"text/javascript\">\n" .
"//<![CDATA[\n" .
"document.ltiInitiateLoginForm.submit();\n" .
"//]]>\n" .
"</script>\n";

return $r;
$params['client_id'] = $config->lti_clientid;
$params['lti_deployment_id'] = $config->typeid;
return $params;
}

function lti_get_type($typeid) {
Expand Down
32 changes: 32 additions & 0 deletions mod/lti/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1383,4 +1383,36 @@ public function test_lti_new_access_token() {
$this->assertEquals($token->timecreated + LTI_ACCESS_TOKEN_LIFE, $token->validuntil);
$this->assertNull($token->lastaccess);
}

/**
* Test lti_build_login_request().
*/
public function test_lti_build_login_request() {
global $USER, $CFG;

$this->resetAfterTest();

$USER->id = 123456789;

$course = $this->getDataGenerator()->create_course();
$instance = $this->getDataGenerator()->create_module('lti',
[
'course' => $course->id,
]
);

$config = new stdClass();
$config->lti_clientid = 'some-client-id';
$config->typeid = 'some-type-id';
$config->lti_toolurl = 'some-lti-tool-url';

$request = lti_build_login_request($course->id, $instance->id, $instance, $config, 'basic-lti-launch-request');

$this->assertEquals($CFG->wwwroot, $request['iss']);
$this->assertEquals('http://some-lti-tool-url', $request['target_link_uri']);
$this->assertEquals(123456789, $request['login_hint']);
$this->assertEquals($instance->id, $request['lti_message_hint']);
$this->assertEquals('some-client-id', $request['client_id']);
$this->assertEquals('some-type-id', $request['lti_deployment_id']);
}
}

0 comments on commit 9c7c2d8

Please sign in to comment.