postverify) ? $CFG->postverify : false; $postmessage_enabled = isset($CFG->postmessage) ? $CFG->postmessage : false; // target_link_uri and lti_message_hint are not required by Tsugi $login_hint = U::get($_REQUEST, 'login_hint'); $iss = U::get($_REQUEST, 'iss'); $issuer_guid = U::get($_REQUEST, 'guid', $rest_path->action); $lti_storage_target = U::get($_REQUEST, 'web_message_target'); $lti_storage_target = U::get($_REQUEST, 'ims_web_message_target', $lti_storage_target); $lti_storage_target = U::get($_REQUEST, 'lti_storage_target', $lti_storage_target); $put_data_supported = is_string($lti_storage_target) && !empty($lti_storage_target); if ( $lti_storage_target == "_parent" ) $lti_storage_target = null; // Lets mark the browser every chance we get LTIX::getBrowserMark(); // TODO: Try to get rid of this $put_data_supported = true; // For Now // Allow a format where the parameter is the primary key of the lti_key row $key_id = null; if ( is_numeric($issuer_guid) ) $key_id = intval($issuer_guid); // echo("
\n");var_dump($_REQUEST); LTIX::abort_with_error_log();

if ( ! $login_hint ) {
    LTIX::abort_with_error_log('Missing login_hint');
}

if ( ! $iss ) {
    LTIX::abort_with_error_log('Missing iss');
}

if ( ! $key_id ) {
    LTIX::abort_with_error_log('Missing or invalid key_id in login URL');
}

$PDOX = \Tsugi\Core\LTIX::getConnection();

$key_sha256 = LTI13::extract_issuer_key_string($iss);

error_log("key_id=$key_id iss=".$iss." sha256=".$key_sha256);

$registration_sql = LTIX::inboundRegistrationSql($CFG->dbprefix, 'K');
// TODO: Remove this lti_registration existence gate by February 2027.
$sql = "SELECT key_id,
    lms_issuer, lms_client, lms_oidc_auth, lms_keyset_url,
    lms_token_url, lms_token_audience, lms_cache_keyset, lms_cache_pubkey, lms_cache_kid";
if ( $registration_sql['select'] !== '' ) {
    $sql .= ",
    ".$registration_sql['select'];
}
$sql .= "
    FROM {$CFG->dbprefix}lti_key AS K";
if ( $registration_sql['join'] !== '' ) {
    $sql .= "
    ".$registration_sql['join'];
}
$sql .= "
    WHERE K.key_id = :KID AND (K.deleted IS NULL OR K.deleted = 0)";
$row = $PDOX->rowDie($sql, array(":KID" => $key_id));
if ( ! is_array($row) || count($row) < 1 ) {
    LTIX::abort_with_error_log('Login could not find key '.htmlentities($key_id)." for iss=".htmlentities($iss));
}
$row = LTIX::applyInboundRegistration($row);

$row['issuer_client'] = $row['lms_client'];
$row['lti13_oidc_auth'] = $row['lms_oidc_auth'];
$row['issuer_key'] = $row['lms_issuer'];
$row['lti13_kid'] = $row['lms_cache_kid'];
$row['lti13_keyset_url'] = $row['lms_keyset_url'];
$row['lti13_keyset'] = $row['lms_cache_keyset'];
$row['lti13_platform_pubkey'] = $row['lms_cache_pubkey'];

$client_id = trim($row['issuer_client']);
$redirect = trim($row['lti13_oidc_auth']);

$issuer_key = $row['issuer_key'];
$platform_public_key = $row['lti13_platform_pubkey'];
$our_kid = $row['lti13_kid'];
$our_keyset_url = $row['lti13_keyset_url'];
$our_keyset = $row['lti13_keyset'];

$signature = \Tsugi\Core\LTIX::getBrowserSignature();

$nonce = bin2hex(random_bytes(32));
$payload = array();
$payload['signature'] = $signature;
$payload['time'] = time();
// Bind the nonce to this transaction and make concurrent login states distinct.
$payload['nonce'] = $nonce;
// Someday we might do something clever with this...
if ( U::get($_REQUEST,'target_link_uri') ) {
    $payload['target_link_uri'] = $_REQUEST['target_link_uri'];
}

$state = JWT::encode($payload, $CFG->cookiesecret, 'HS256');

// Make a short-lived session
$sid = substr("log-".md5($state), 0, 20);
error_log(" =============== oidc_login ===================== $sid");
session_id($sid);
session_start();
$_SESSION['state'] = $state;
$_SESSION['key_id'] = $row['key_id'];
$_SESSION['issuer_key'] = $issuer_key;
$_SESSION['platform_public_key'] = $platform_public_key;
$_SESSION['lti_storage_target'] = $lti_storage_target;
$_SESSION['our_kid'] = $our_kid;
$_SESSION['our_keyset_url'] = $our_keyset_url;
$_SESSION['our_keyset'] = $our_keyset;
$_SESSION['lti13_oidc_auth'] = trim($row['lti13_oidc_auth']);
$session_password = uniqid();
$_SESSION['password'] = $session_password;

$_SESSION['put_data_supported'] = $put_data_supported;

$redirect = U::add_url_parm($redirect, "scope", "openid");
$redirect = U::add_url_parm($redirect, "response_type", "id_token");
$redirect = U::add_url_parm($redirect, "response_mode", "form_post");
$redirect = U::add_url_parm($redirect, "prompt", "none");
$redirect = U::add_url_parm($redirect, "nonce", $nonce);

// client_id - Required, per OIDC spec, the tool’s client id for this issuer.
$redirect = U::add_url_parm($redirect, "client_id", $client_id);
$redirect = U::add_url_parm($redirect, "login_hint", $login_hint);
if ( U::get($_REQUEST,'lti_message_hint') ) {
    $redirect = U::add_url_parm($redirect, "lti_message_hint", $_REQUEST['lti_message_hint']);
}
$redirect = U::add_url_parm($redirect, "redirect_uri", $CFG->wwwroot . '/lti/oidc_launch');
$redirect = U::add_url_parm($redirect, "state", $state);

error_log("oidc_login redirect: ".$redirect);

// Store it in a session cookie - likely won't work inside iframes on future browsers
setcookie("TSUGI_STATE", $state);

if ( is_string(U::get($_POST, 'canvas_region')) ) {
    error_log("Activating Canvas bypass of cookie handling code");
    header("Location: ".$redirect);
    return;
}

// We use postmessage if we have explicit notification from the LMS or if we always enable it through config
if ( $put_data_supported || $postmessage_enabled ) {
    // Fall through to below
} else {
    // Simple fallback
?>