Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch missing data #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lti/LTI_Assignments_Grades_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function find_or_create_lineitem(LTI_Lineitem $new_line_item) {
'application/vnd.ims.lis.v2.lineitemcontainer+json'
);
foreach ($line_items['body'] as $line_item) {
if (empty($new_line_item->get_resource_id()) || $line_item['resourceId'] == $new_line_item->get_resource_id()) {
if (empty($new_line_item->get_tag()) || $line_item['tag'] == $new_line_item->get_tag()) {
if (empty($new_line_item->get_resource_id()) || (isset($line_item['resourceId']) && $line_item['resourceId'] == $new_line_item->get_resource_id())) {
if (empty($new_line_item->get_tag()) || (isset($line_item['tag']) && $line_item['tag'] == $new_line_item->get_tag())) {
return new LTI_Lineitem($line_item);
}
}
Expand Down Expand Up @@ -88,4 +88,4 @@ public function get_grades(LTI_Lineitem $lineitem) {
return $scores['body'];
}
}
?>
?>
4 changes: 2 additions & 2 deletions src/lti/LTI_Deep_Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function get_response_jwt($resources) {
"https://purl.imsglobal.org/spec/lti/claim/message_type" => "LtiDeepLinkingResponse",
"https://purl.imsglobal.org/spec/lti/claim/version" => "1.3.0",
"https://purl.imsglobal.org/spec/lti-dl/claim/content_items" => array_map(function($resource) { return $resource->to_array(); }, $resources),
"https://purl.imsglobal.org/spec/lti-dl/claim/data" => $this->deep_link_settings['data'],
"https://purl.imsglobal.org/spec/lti-dl/claim/data" => $this->deep_link_settings['data']?? "",
];
return JWT::encode($message_jwt, $this->registration->get_tool_private_key(), 'RS256', $this->registration->get_kid());
}
Expand All @@ -43,4 +43,4 @@ public function output_response_form($resources) {
<?php
}
}
?>
?>
7 changes: 2 additions & 5 deletions src/lti/LTI_Deep_Link_Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LTI_Deep_Link_Resource {
private $target = 'iframe';

public static function new() {
return new LTI_Deep_Link_Resource();
return new static();
}

public function get_type() {
Expand Down Expand Up @@ -79,10 +79,7 @@ public function to_array() {
"custom" => $this->custom_params,
];
if ($this->lineitem !== null) {
$resource["lineItem"] = [
"scoreMaximum" => $this->lineitem->get_score_maximum(),
"label" => $this->lineitem->get_label(),
];
$resource["lineItem"] = $this->lineitem->to_array();
}
return $resource;
}
Expand Down
18 changes: 11 additions & 7 deletions src/lti/LTI_Lineitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function __construct(array $lineitem = null) {
$this->label = $lineitem["label"];
$this->resource_id = $lineitem["resourceId"];
$this->tag = $lineitem["tag"];
$this->start_date_time = $lineitem["startDateTime"];
$this->end_date_time = $lineitem["endDateTime"];
$this->start_date_time = $lineitem["startDateTime"]?? date(\DateTime::ISO8601);
$this->end_date_time = $lineitem["endDateTime"]?? date(\DateTime::ISO8601);
}

/**
* Static function to allow for method chaining without having to assign to a variable first.
*/
public static function new() {
return new LTI_Lineitem();
return new static();
}

public function get_id() {
Expand Down Expand Up @@ -93,16 +93,20 @@ public function set_end_date_time($value) {
return $this;
}

public function __toString() {
return json_encode(array_filter([
public function to_array() {
return [
"id" => $this->id,
"scoreMaximum" => $this->score_maximum,
"label" => $this->label,
"resourceId" => $this->resource_id,
"tag" => $this->tag,
"startDateTime" => $this->start_date_time,
"endDateTime" => $this->end_date_time,
]));
];
}

public function __toString() {
return json_encode(array_filter($this->to_array()));
}
}
?>
?>
9 changes: 4 additions & 5 deletions src/lti/LTI_Message_Launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function __construct(Database $database, Cache $cache = null, Cookie $cookie = n
* Static function to allow for method chaining without having to assign to a variable first.
*/
public static function new(Database $database, Cache $cache = null, Cookie $cookie = null) {
return new LTI_Message_Launch($database, $cache, $cookie);
return new static($database, $cache, $cookie);
}

/**
Expand All @@ -57,7 +57,7 @@ public static function new(Database $database, Cache $cache = null, Cookie $cook
* @return LTI_Message_Launch A populated and validated LTI_Message_Launch.
*/
public static function from_cache($launch_id, Database $database, Cache $cache = null) {
$new = new LTI_Message_Launch($database, $cache, null);
$new = new static($database, $cache, null);
$new->launch_id = $launch_id;
$new->jwt = [ 'body' => $new->cache->get_launch_data($launch_id) ];
return $new->validate_registration();
Expand Down Expand Up @@ -270,7 +270,7 @@ private function validate_jwt_format() {

private function validate_nonce() {
if (!$this->cache->check_nonce($this->jwt['body']['nonce'])) {
//throw new LTI_Exception("Invalid Nonce");
throw new LTI_Exception("Invalid Nonce");
}
return $this;
}
Expand Down Expand Up @@ -301,7 +301,6 @@ private function validate_jwt_signature() {
try {
JWT::decode($this->request['id_token'], $public_key['key'], array('RS256'));
} catch(\Exception $e) {
var_dump($e);
// Error validating signature.
throw new LTI_Exception("Invalid signature on id_token", 1);
}
Expand Down Expand Up @@ -369,4 +368,4 @@ private function validate_message() {

}
}
?>
?>
10 changes: 3 additions & 7 deletions src/lti/LTI_OIDC_Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,12 @@ public static function new(Database $database, Cache $cache = null, Cookie $cook
*
* @return Redirect Returns a redirect object containing the fully formed OIDC login URL.
*/
public function do_oidc_login_redirect($launch_url, array $request = null) {
public function do_oidc_login_redirect(string $launch_url = "", array $request = null) {

if ($request === null) {
$request = $_REQUEST;
}

if (empty($launch_url)) {
throw new OIDC_Exception("No launch URL configured", 1);
}

// Validate Request Data.
$registration = $this->validate_oidc_login($request);

Expand All @@ -75,7 +71,7 @@ public function do_oidc_login_redirect($launch_url, array $request = null) {
'response_mode' => 'form_post', // OIDC response is always a form post.
'prompt' => 'none', // Don't prompt user on redirect.
'client_id' => $registration->get_client_id(), // Registered client id.
'redirect_uri' => $launch_url, // URL to return to after login.
'redirect_uri' => $launch_url?: $request['target_link_uri'], // URL to return to after login.
'state' => $state, // State to identify browser session.
'nonce' => $nonce, // Prevent replay attacks.
'login_hint' => $request['login_hint'] // Login hint to identify platform session.
Expand Down Expand Up @@ -117,4 +113,4 @@ protected function validate_oidc_login($request) {
// Return Registration.
return $registration;
}
}
}