From e4218aa2cc2271f6397aa89168db83670d6307b5 Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sat, 21 Nov 2026 12:49:36 -0500 Subject: [PATCH 1/5] Add a dormant inbound LTI 1.3 registration, and drop the CodeRabbit PHPStan setting it no longer accepts. Co-authored-by: Cursor --- .coderabbit.yaml | 1 - lib/src/Services/Lti/database.php | 121 ++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/.coderabbit.yaml b/.coderabbit.yaml index afc8b2182..46ccd2687 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -91,7 +91,6 @@ reviews: tools: phpstan: enabled: true - config_file: "phpstan.neon" phpcs: enabled: false phpmd: diff --git a/lib/src/Services/Lti/database.php b/lib/src/Services/Lti/database.php index ff5959ad1..26095f9d6 100644 --- a/lib/src/Services/Lti/database.php +++ b/lib/src/Services/Lti/database.php @@ -40,6 +40,8 @@ "drop table if exists {$CFG->dbprefix}lti_issuer", "drop table if exists {$CFG->dbprefix}lti_keyset", "drop table if exists {$CFG->dbprefix}lti_key", +// After lti_key. A later foreign key on lti_key will require that order. +"drop table if exists {$CFG->dbprefix}lti_registration", "drop table if exists {$CFG->dbprefix}lti_nonce", "drop table if exists {$CFG->dbprefix}lti_message", "drop table if exists {$CFG->dbprefix}lti_domain", @@ -70,6 +72,79 @@ CONSTRAINT `{$CFG->dbprefix}lti_keyset_const_pk` PRIMARY KEY (keyset_id) ) ENGINE = InnoDB DEFAULT CHARSET=utf8"), +// Inbound LTI 1.3 registration. Tsugi is the tool. +// lti_tool_registration is the other direction, where Tsugi is the platform. +// One row is one OAuth client (issuer + client_id). Deployments stay on lti_key. +// Created before lti_key so a later foreign key can live in the lti_key CREATE. +// Nothing reads this table yet. Launch and dynamic registration still use lti_key. +array( "{$CFG->dbprefix}lti_registration", +"create table {$CFG->dbprefix}lti_registration ( + lti_registration_id INTEGER NOT NULL AUTO_INCREMENT, + + -- Row guid. An omitted value gets an unswapped MySQL UUID() (version 1). + -- Same bytes as UUID_TO_BIN(UUID()). Inserts pass Guid::uuid7(). + -- UNHEX, REPLACE, and UUID are allowed in a default on MySQL 8.0.13+ and + -- MariaDB 10.5. UUID_TO_BIN is not allowed in a MariaDB 10.5 default. + -- Explicit NULL stays NULL. The unique key allows more than one NULL. + -- This is not tool_platform.guid. That value is platform_guid. + guid BINARY(16) NULL DEFAULT (UNHEX(REPLACE(UUID(), '-', ''))), + + -- Human label. Not part of the OAuth identity. Copied from key_title + -- when a registration is created from a key. + title TEXT NULL, + notes TEXT NULL, + + -- Issuer is required. client_id stays NULL until the platform assigns one. + issuer VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, + client_id VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NULL, + + -- The first completed (issuer, client_id) owns these four values. + -- Later deployments of that client reuse this row. They do not get another copy. + -- Canvas cloud shares the issuer and these URLs, so the client id separates schools. + -- Blackboard SaaS shares https://blackboard.com. The authorize, token, and + -- keyset URLs are on the school host, and the token audience is often a + -- different gateway value. The first row to store that client freezes them. + -- A hostname change, or another key entered with different URLs, has nowhere + -- else to go. Handle this when dynamic registration writes this table. + -- Do not copy the first lti_key lms_* values and assume the other keys agree. + oidc_auth_url TEXT NULL, + keyset_url TEXT NULL, + token_url TEXT NULL, + -- OpenID authorization_server, stored verbatim. Often not the token URL. + token_audience TEXT NULL, + + -- One-time dynamic registration code. Same column as lti_key.unlock_code. + -- Dynamic registration still checks the code on the key. + unlock_code MEDIUMTEXT NULL, + -- NULL until a later dynamic registration flow records the unlock. Nothing writes this yet. + dynamic_registration_unlocked_at TIMESTAMP NULL, + + product_family_code VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, + -- tool_platform.guid from a launch. Registration usually does not have it yet. + platform_guid VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NULL, + + -- Future values: manual, dynamic. Nothing reads these yet. + registration_method VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'manual', + -- Future values: pending, active, disabled. + registration_status VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'pending', + -- Future values: automatic, approval, reject. Default is approval. + new_deployment_policy VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT 'approval', + + -- Provenance only. Not a foreign key and not deployment ownership. + created_by_user_id INTEGER NULL, + + json MEDIUMTEXT NULL, + + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NULL, + + -- NULLs are distinct, so pending rows can share an issuer while client_id + -- is still NULL. A completed (issuer, client_id) pair cannot be repeated. + CONSTRAINT `{$CFG->dbprefix}lti_registration_const_1` UNIQUE (issuer, client_id), + CONSTRAINT `{$CFG->dbprefix}lti_registration_const_guid` UNIQUE (`guid`), + CONSTRAINT `{$CFG->dbprefix}lti_registration_const_pk` PRIMARY KEY (lti_registration_id) + ) ENGINE = InnoDB DEFAULT CHARSET=utf8"), + // https://stackoverflow.com/questions/28418360/jwt-json-web-token-audience-aud-versus-client-id-whats-the-difference // Key is in effect "tenant" (like a billing endpoint) @@ -111,6 +186,17 @@ lms_token_url TEXT NULL, lms_token_audience TEXT NULL, + -- Inbound registration. NULL means this key is not in that model yet. + -- ON DELETE SET NULL detaches the key when the registration is removed. + -- deployment_status NULL means the approval workflow does not apply. + -- Future values: pending, active, blocked. + -- platform_guid is tool_platform.guid from a launch, not this row's guid. + lti_registration_id INTEGER NULL, + deployment_status VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin NULL, + -- NULL until a later approval workflow sets it. Existing keys stay NULL. + deployment_approved_at TIMESTAMP NULL, + platform_guid VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NULL, + -- Default IANA zone copied onto a new Plus context. Existing courses keep their own. timezone VARCHAR(64) NULL, @@ -123,6 +209,8 @@ lms_cache_kid TEXT NULL, unlock_code MEDIUMTEXT NULL, + -- NULL until a later dynamic registration flow records the unlock. Nothing writes this yet. + dynamic_registration_unlocked_at TIMESTAMP NULL, json MEDIUMTEXT NULL, user_json MEDIUMTEXT NULL, @@ -148,6 +236,10 @@ -- allows multiple rows per key_sha256 in typical MySQL UNIQUE-with-NULL semantics. CONSTRAINT `{$CFG->dbprefix}lti_key_const_1` UNIQUE(key_sha256, deploy_sha256), CONSTRAINT `{$CFG->dbprefix}lti_key_const_guid` UNIQUE (`guid`), + CONSTRAINT `{$CFG->dbprefix}lti_key_ibfk_1` + FOREIGN KEY (`lti_registration_id`) + REFERENCES `{$CFG->dbprefix}lti_registration` (`lti_registration_id`) + ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `{$CFG->dbprefix}lti_key_const_pk` PRIMARY KEY (key_id) ) ENGINE = InnoDB DEFAULT CHARSET=utf8"), @@ -1717,6 +1809,17 @@ // 2026-10-27 Last Classroom refresh error, and the last successful check. array('lti_user', 'gc_status', 'VARCHAR(512) NULL'), array('lti_user', 'gc_checked_at', 'TIMESTAMP NULL'), + + // 2026-11-21 Inbound LTI 1.3 registration. Existing keys stay NULL. + // The foreign key is added below. platform_guid is tool_platform.guid, not lti_key.guid. + // lti_registration itself is created from DATABASE_INSTALL. + array('lti_key', 'lti_registration_id', 'INTEGER NULL'), + array('lti_key', 'deployment_status', 'VARCHAR(20) CHARACTER SET ascii COLLATE ascii_bin NULL'), + array('lti_key', 'deployment_approved_at', 'TIMESTAMP NULL'), + array('lti_key', 'platform_guid', 'VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NULL'), + array('lti_key', 'dynamic_registration_unlocked_at', 'TIMESTAMP NULL'), + array('lti_registration', 'title', 'TEXT NULL'), + array('lti_registration', 'notes', 'TEXT NULL'), ); foreach ( $add_some_fields as $add_field ) { @@ -2444,6 +2547,24 @@ return is_array($row); }; + // ON DELETE SET NULL: removing a registration detaches its keys. + $key_table = "{$p}lti_key"; + $inbound_table = "{$p}lti_registration"; + $key_registration_fk = "{$p}lti_key_ibfk_1"; + if ( $PDOX->metadata($key_table) !== false + && $PDOX->metadata($inbound_table) !== false + && $PDOX->columnExists('lti_registration_id', $key_table) + && ! $constraint_exists($key_table, $key_registration_fk) ) { + $sql = "ALTER TABLE {$key_table} ADD CONSTRAINT `{$key_registration_fk}` + FOREIGN KEY (`lti_registration_id`) + REFERENCES `{$inbound_table}` (`lti_registration_id`) + ON DELETE SET NULL ON UPDATE CASCADE"; + echo("Upgrading: ".$sql."
\n"); + error_log("Upgrading: ".$sql); + $q = $PDOX->queryReturnError($sql); + if ( ! $q->success ) die("Unable to add {$key_registration_fk}: ".$q->errorImplode."
\n"); + } + if ( $PDOX->metadata($org_table) !== false && $PDOX->columnExists('org_id', $context_table) ) { $org_index = "{$p}lti_context_indx_1"; if ( ! $PDOX->indexExists($org_index, $context_table) ) { From 57f9e0db651290a99a2e282f63e93d18b1dbd3b3 Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Sat, 21 Nov 2026 16:44:07 -0500 Subject: [PATCH 2/5] Use a complete inbound registration for admin, launch, login, and Plus tokens. A launch matches the registration only when its issuer, client id, and platform URLs are all set. A keyset cache is reused when it was fetched from that registration URL, and the key row keeps its own platform fields in the database. Co-authored-by: Cursor --- config-dist.php | 4 + lib/src/Controllers/Admin/Key.php | 24 + .../templates/Admin/key/blackboard-detail.php | 7 +- .../templates/Admin/key/canvas-detail.php | 12 +- .../templates/Admin/key/client-id-check.php | 2 +- .../Controllers/templates/Admin/key/index.php | 22 +- .../templates/Admin/key/key-add.php | 38 +- .../templates/Admin/key/key-detail.php | 222 +++++++- .../templates/Admin/key/registration-add.php | 46 ++ .../Admin/key/registration-detail.php | 129 +++++ .../Admin/key/registration-fields.php | 37 ++ .../Admin/key/registration-from-key.php | 14 + .../templates/Admin/key/registrations.php | 55 ++ .../templates/Admin/key/requests.php | 2 +- lib/src/Core/LTIX.php | 194 ++++++- lib/src/Services/Admin/AdminService.php | 528 +++++++++++++++++- lib/src/Services/Plus/GradeSync.php | 38 +- lib/src/Services/Plus/RosterSync.php | 19 +- lib/src/UI/CrudForm.php | 21 +- lib/tests/Core/InboundRegistrationTest.php | 342 ++++++++++++ .../Admin/AdminServiceKeyTypeTest.php | 12 + lti/oidc_login.php | 19 +- qa/tests/AdminTest.php | 135 ++++- qa/tests/CourseToolLoopbackTest.php | 10 +- qa/tests/Support/TsugiPantherTestCase.php | 9 + 25 files changed, 1837 insertions(+), 104 deletions(-) create mode 100644 lib/src/Controllers/templates/Admin/key/registration-add.php create mode 100644 lib/src/Controllers/templates/Admin/key/registration-detail.php create mode 100644 lib/src/Controllers/templates/Admin/key/registration-fields.php create mode 100644 lib/src/Controllers/templates/Admin/key/registration-from-key.php create mode 100644 lib/src/Controllers/templates/Admin/key/registrations.php create mode 100644 lib/tests/Core/InboundRegistrationTest.php diff --git a/config-dist.php b/config-dist.php index fddd1383b..1d341341d 100644 --- a/config-dist.php +++ b/config-dist.php @@ -174,6 +174,10 @@ function __the_end(){ // $CFG->home_path = 'https://example.com/'; // Legacy: $CFG->setExtension('home_path', $CFG->apphome); +// Temporary log when a launch or Plus token takes LTI config from lti_registration +// instead of lti_key. On unless set false. Remove by February 2027. +// $CFG->setExtension('log_registration_keys', false); + // Temporary ReqScope dump at /reqscope and tool/reqscope. Remove around December 2026. // /admin lists this with the QA settings when it is on. // $CFG->setExtension('reqscope_debug', true); diff --git a/lib/src/Controllers/Admin/Key.php b/lib/src/Controllers/Admin/Key.php index 12a6a08f4..f867a2c5c 100644 --- a/lib/src/Controllers/Admin/Key.php +++ b/lib/src/Controllers/Admin/Key.php @@ -18,6 +18,10 @@ public static function routes(Application $app) { self::map($app, self::class, 'clientCheck', self::paths('key/client-id-check')); self::map($app, self::class, 'add', self::paths('key/key-add')); self::map($app, self::class, 'detail', self::paths('key/key-detail')); + self::map($app, self::class, 'registrations', self::paths('key/registrations')); + self::map($app, self::class, 'registrationAdd', self::paths('key/registration-add')); + self::map($app, self::class, 'registrationDetail', self::paths('key/registration-detail')); + self::map($app, self::class, 'registrationFromKey', self::paths('key/registration-from-key')); self::map($app, self::class, 'settings', self::paths('key/key-settings')); self::map($app, self::class, 'requests', self::paths('key/requests')); self::map($app, self::class, 'request', self::paths('key/request-detail')); @@ -46,6 +50,26 @@ public function detail() { $this->view('key/key-detail.php'); } + public function registrations() { + if ( $r = $this->gate('keys.view') ) return $r; + $this->view('key/registrations.php'); + } + + public function registrationAdd() { + if ( $r = $this->gate('keys.add') ) return $r; + $this->view('key/registration-add.php'); + } + + public function registrationDetail() { + if ( $r = $this->gate('keys.detail') ) return $r; + $this->view('key/registration-detail.php'); + } + + public function registrationFromKey() { + if ( $r = $this->gate('keys.add') ) return $r; + $this->view('key/registration-from-key.php'); + } + public function settings() { if ( $r = $this->gate('keys.settings') ) return $r; $this->view('key/key-settings.php'); diff --git a/lib/src/Controllers/templates/Admin/key/blackboard-detail.php b/lib/src/Controllers/templates/Admin/key/blackboard-detail.php index 622f4917e..0bcbe6564 100644 --- a/lib/src/Controllers/templates/Admin/key/blackboard-detail.php +++ b/lib/src/Controllers/templates/Admin/key/blackboard-detail.php @@ -1,10 +1,9 @@

LTI 1.3

-Blackboard uses a single issuer for all tenants, but +Blackboard uses one issuer for every school, but since the Blackboard developer portal assigns a Well-Known/KeySet URL that is unique -to each Client ID, it is best to skip creating a reusable Issuer in Tsugi, and instead create a Key for each Tenant, -select "No Global Issuer Selected" and set all the issuer values here -to create a Tenant key. +to each Client ID, skip a reusable issuer in Tsugi and create a key for each client id. +Leave the LTI 1.3 registration unset and set all the issuer values on that key. The Deployment ID is provided when you place Tsugi into a Blackboard instance using the Client ID. For Blackboard, the following are the typical values for the Tsugi items: diff --git a/lib/src/Controllers/templates/Admin/key/canvas-detail.php b/lib/src/Controllers/templates/Admin/key/canvas-detail.php index e12ccda61..06924c214 100644 --- a/lib/src/Controllers/templates/Admin/key/canvas-detail.php +++ b/lib/src/Controllers/templates/Admin/key/canvas-detail.php @@ -1,10 +1,10 @@

Canvas LTI 1.3

-To use LTI 1.3 in Canvas, first create a Tsugi Tenant using a temporary +To use LTI 1.3 in Canvas, first create a Tsugi key using a temporary Client ID such as temporary-42-oertufgh. Make sure it is random enough to be unique across issuers as Tsugi prohibits duplicate (issuer, client_id) records system wide. -Save the Tenant in Tsugi and use the +Save the key in Tsugi and use the LTI values like JWKs URL shown by Tsugi to create a Developer Key in Canvas.

@@ -12,8 +12,8 @@

When the Developer Key is created, Canvas will assign a real Client ID (for example -38288000000000436). Edit the Tsugi Tenant and replace the -temporary Client ID with the value assigned by Canvas and save the Tenant. +38288000000000436). Edit the Tsugi key and replace the +temporary Client ID with the value assigned by Canvas and save the key.

@@ -26,7 +26,7 @@

The Deployment ID is optional in Tsugi and may be left blank to accept launches from any Canvas deployment using that issuer/client pair. -If you enter a Deployment ID on the Tenant, launches will be restricted +If you enter a Deployment ID on the key, launches will be restricted to that specific Canvas deployment (i.e. it might just be one course).

To use LTI 1.1 in Canvas, create your key with a title, key, and secret and come back here to view the XML-based Canvas auto-installation URL. Auto-installation of LTI 1.1 tools -in Canvas is on a per-Tenant basis. +in Canvas is on a per-key basis.

diff --git a/lib/src/Controllers/templates/Admin/key/client-id-check.php b/lib/src/Controllers/templates/Admin/key/client-id-check.php index eed0c204b..b63ce07c4 100644 --- a/lib/src/Controllers/templates/Admin/key/client-id-check.php +++ b/lib/src/Controllers/templates/Admin/key/client-id-check.php @@ -41,7 +41,7 @@ echo \Tsugi\Services\Admin\AdminService::issuerClientDuplicateNotice(); ?>

- Tenant Keys + Keys Client Id Check Admin

diff --git a/lib/src/Controllers/templates/Admin/key/index.php b/lib/src/Controllers/templates/Admin/key/index.php index 77ea72a93..82e64a792 100644 --- a/lib/src/Controllers/templates/Admin/key/index.php +++ b/lib/src/Controllers/templates/Admin/key/index.php @@ -16,12 +16,15 @@ $rows = $PDOX->queryDie($patch_sql); $query_parms = array(); -$searchfields = array("K.key_id", "key_title", "key_key", "deploy_key", "K.login_at", "K.updated_at", "K.user_id", "K.lms_client"); +$searchfields = array("K.key_id", "key_title", "key_key", "deploy_key", "K.login_at", "K.updated_at", "K.user_id", "K.lms_client", "R.issuer", "R.client_id"); $sql = "SELECT K.key_id AS key_id, key_title, key_key, secret, lms_issuer, K.lms_client AS lms_client, lms_oidc_auth, lms_keyset_url, lms_token_url, deploy_key, K.login_at AS login_at, K.updated_at as updated_at, - K.user_id AS user_id + K.user_id AS user_id, K.lti_registration_id AS lti_registration_id, + R.issuer AS registration_issuer, R.client_id AS registration_client FROM {$CFG->dbprefix}lti_key AS K + LEFT JOIN {$CFG->dbprefix}lti_registration AS R + ON R.lti_registration_id = K.lti_registration_id "; $newsql = Table::pagedQuery($sql, $query_parms, $searchfields); @@ -34,8 +37,13 @@ $newrow['key'] = $row['key_key']; if ( !empty($row['key_title']) ) $newrow['key'] = $row['key_title']; $newrow['key_type'] = \Tsugi\Services\Admin\AdminService::keyTypeLabel($row); + $newrow['_html_key_type'] = \Tsugi\Services\Admin\AdminService::keyTypeHtml($row); $issuer_key = $row['lms_issuer']; $client_id = $row['lms_client']; + if ( empty($issuer_key) && empty($client_id) && ! empty($row['registration_issuer']) ) { + $issuer_key = $row['registration_issuer']; + $client_id = $row['registration_client']; + } $issuer_display = ''; if ( !empty($issuer_key) || !empty($client_id) ) { if ( !empty($issuer_key) ) { @@ -64,9 +72,10 @@ $OUTPUT->flashMessages(); echo \Tsugi\Services\Admin\AdminService::clientIdCheckNotices(); ?> -

LTI Tenants (Keys)

+

Keys / Deployments

- Tenant Keys + Registrations + Keys providekeys ) { ?> Key Requests @@ -75,11 +84,12 @@

-You have no Tenant keys for this system. +You have no keys for this system.

"key-add" + "Insert Registration" => "registration-add", + "Insert Key" => "key-add" ); Table::pagedTable($newrows, $searchfields, false, "key-detail", false, $extra_buttons); // echo("
\n");print_r($newrows);echo("
\n"); diff --git a/lib/src/Controllers/templates/Admin/key/key-add.php b/lib/src/Controllers/templates/Admin/key/key-add.php index 2453f21d6..9332426f7 100644 --- a/lib/src/Controllers/templates/Admin/key/key-add.php +++ b/lib/src/Controllers/templates/Admin/key/key-add.php @@ -17,13 +17,12 @@ $from_location = LTIX::curPageUrlFolder(); $tablename = "{$CFG->dbprefix}lti_key"; -$fields = array('key_title', 'plus', 'key_key', 'key_sha256', 'secret', +$fields = array('key_title', 'plus', 'user_id', 'key_key', 'key_sha256', 'secret', 'lms_issuer', 'lms_issuer_sha256', 'lms_client', 'deploy_key', 'deploy_sha256', 'lms_oidc_auth', 'lms_keyset_url', 'lms_token_url', 'lms_token_audience', 'created_at', 'updated_at', - 'user_id', ); $titles = array( @@ -65,6 +64,10 @@ } if ( isset($_POST['doSave']) && count($_POST) > 0 ) { + if ( ! \Tsugi\Services\Admin\AdminService::applyPostedRegistrationToKey() ) { + header("Location: key-add"); + return; + } $lms_issuer = U::get($_POST, 'lms_issuer'); $lms_client = U::get($_POST, 'lms_client'); $identity_message = LTI::ascii255First(array( @@ -99,7 +102,9 @@ if ( isset($_POST['doSave']) ) { $binaryHex['guid'] = \Tsugi\Core\Guid::uuid7(); } -$retval = CrudForm::handleInsert($tablename, $fields, $binaryHex); +$save_fields = $fields; +$save_fields[] = 'lti_registration_id'; +$retval = CrudForm::handleInsert($tablename, $save_fields, $binaryHex); if ( $retval == CrudForm::CRUD_SUCCESS || $retval == CrudForm::CRUD_FAIL ) { header("Location: $from_location"); return; @@ -111,7 +116,7 @@ $OUTPUT->flashMessages(); ?> -

Adding Tsugi Tenant/Key +

Adding Key Exit