19declare(strict_types=1);
56 $auth_arr = explode(
'_', $a_auth_key);
57 if (count($auth_arr) > 1) {
58 return 'lti_' . $auth_arr[1];
70 $auth_arr = explode(
'_', $a_auth_mode);
71 if (count($auth_arr) > 1) {
86 $query =
'SELECT consumer_pk from lti2_consumer where enabled = ' .
$ilDB->quote(1,
'integer');
91 $sids[] = $row->consumer_pk;
104 $query =
'SELECT distinct(consumer_pk) consumer_pk from lti2_consumer';
109 $sids[] = $row->consumer_pk;
124 $object_ref = $consumer->getRefId();
126 return $consumer->getTitle() .
" / " . $object_title;
136 if (self::isAuthModeLTI($a_auth_mode)) {
137 $auth_arr = explode(
'_', $a_auth_mode);
138 return (
int) $auth_arr[1];
154 $auth_arr = explode(
'_', $a_auth_mode);
167 $query =
'SELECT consumer_pk from lti2_consumer where consumer_key = ' .
$ilDB->quote(
168 $a_oauth_consumer_key,
177 $lti_id = $row->consumer_pk;
180 $this->
getLogger()->debug(
'External consumer key is: ' . (
int) $lti_id);
193 $query =
'SELECT prefix from lti_ext_consumer where id = ' .
$ilDB->quote($a_lti_id,
'integer');
200 $prefix = $row->prefix;
202 $this->
getLogger()->debug(
'LTI prefix: ' . $prefix);
215 $query =
'SELECT role from lti_ext_consumer where id = ' .
$ilDB->quote($a_lti_id,
'integer');
221 $role = (
int) $row->role;
223 $this->
getLogger()->debug(
'LTI role: ' . $role);
241 if (
$DIC->http()->wrapper()->post()->has(
'launch_presentation_return_url')) {
242 $this->launchReturnUrl =
$DIC->http()->wrapper()->post()->retrieve(
'launch_presentation_return_url',
$DIC->refinery()->kindlyTo()->string());
243 setcookie(
"launch_presentation_return_url", $this->launchReturnUrl, time() + 86400,
"/",
"",
true,
true);
244 $this->
logger->info(
"Setting launch_presentation_return_url in cookie storage " . $this->launchReturnUrl);
246 $lti_provider->handleRequest();
247 $this->provider = $lti_provider;
248 $this->messageParameters = $this->provider->getMessageParameters();
250 if (!
$DIC->http()->wrapper()->post()->has(
'launch_presentation_return_url')) {
251 $this->launchReturnUrl =
$_COOKIE[
'launch_presentation_return_url'] ??
"";
252 $this->
logger->info(
"Catching launch_presentation_return_url from cookies" . $this->launchReturnUrl);
256 if (!$lti_provider->ok) {
257 $this->
getLogger()->info(
'LTI authentication failed with message: ' . $lti_provider->reason);
258 $status->
setReason($lti_provider->reason);
262 $this->
getLogger()->debug(
'LTI authentication success');
265 if (empty($this->messageParameters)) {
266 $status->
setReason(
'empty_lti_message_parameters');
273 $this->ref_id = $platform->getRefId();
277 if (isset($lti_context_ids) && is_array($lti_context_ids)) {
278 if (!in_array($this->ref_id, $lti_context_ids)) {
279 $this->
getLogger()->debug(
"push new lti ref_id: " . $this->ref_id);
285 $this->
getLogger()->debug(
"lti_context_ids is not set. Create new array...");
290 if (!empty($this->messageParameters[
'launch_presentation_return_url'])) {
291 $post[
'launch_presentation_return_url'] = $this->messageParameters[
'launch_presentation_return_url'];
293 if (!empty($this->messageParameters[
'launch_presentation_css_url'])) {
294 $post[
'launch_presentation_css_url'] = $this->messageParameters[
'launch_presentation_css_url'];
296 if (!empty($this->messageParameters[
'resource_link_title'])) {
297 $post[
'resource_link_title'] = $this->messageParameters[
'resource_link_title'];
303 $obj_definition =
$DIC[
"objDefinition"];
307 if (!$platform->enabled) {
308 $this->
getLogger()->warning(
'Consumer is not enabled');
309 $status->
setReason(
'lti_consumer_inactive');
314 if (!$platform->getActive()) {
315 $this->
getLogger()->warning(
'Consumer is not active');
316 $status->
setReason(
'lti_consumer_inactive');
321 $lti_id = $platform->getExtConsumerId();
323 $status->
setReason(
'lti_auth_failed_invalid_key');
328 $this->
getLogger()->debug(
'Using prefix:' . $platform->getPrefix());
330 $this->
getCredentials()->setUsername($this->messageParameters[
'user_id']);
335 $platform->getPrefix()
338 if ($internal_account) {
339 $this->
updateUser($internal_account, $platform);
341 $internal_account = $this->
createUser($platform);
359 protected function findUserId(
string $a_oauth_user,
string $a_oauth_id,
string $a_user_prefix):
int
362 self::AUTH_MODE_PREFIX .
'_' . $a_oauth_id,
369 $this->
getLogger()->debug(
'Found user with auth mode lti_' . $a_oauth_id .
' with user_id: ' .
$user_id);
382 global $ilClientIniFile,
$DIC;
388 $user_obj =
new ilObjUser($a_local_user_id);
389 if (isset($this->messageParameters[
'lis_person_name_given'])) {
390 $user_obj->setFirstname($this->messageParameters[
'lis_person_name_given']);
392 $user_obj->setFirstname(
'-');
394 if (isset($this->messageParameters[
'lis_person_name_family'])) {
395 $user_obj->setLastname($this->messageParameters[
'lis_person_name_family']);
397 $user_obj->setLastname(
'-');
399 $user_obj->setEmail($this->messageParameters[
'lis_person_contact_email_primary']);
401 $user_obj->setActive(
true);
403 $until = $user_obj->getTimeLimitUntil();
405 if ($until < (time() + (
int) $ilClientIniFile->readVariable(
'session',
'expire'))) {
406 $user_obj->setTimeLimitFrom(time() - 60);
407 $user_obj->setTimeLimitUntil(time() + (
int) $ilClientIniFile->readVariable(
"session",
"expire"));
409 $user_obj->refreshLogin();
412 $GLOBALS[
'DIC']->rbac()->admin()->assignUser($consumer->
getRole(), $user_obj->getId());
415 $this->
getLogger()->info(
'Update of lti user with uid: ' . $user_obj->getId() .
' and login: ' . $user_obj->getLogin());
416 return $user_obj->getId();
429 global $ilClientIniFile,
$DIC;
438 $newUser[
"login"] = $local_user;
439 if (isset($this->messageParameters[
'lis_person_name_given'])) {
440 $newUser[
"firstname"] = $this->messageParameters[
'lis_person_name_given'];
442 $newUser[
"firstname"] =
'-';
444 if (isset($this->messageParameters[
'lis_person_name_family'])) {
445 $newUser[
"lastname"] = $this->messageParameters[
'lis_person_name_family'];
447 $newUser[
"lastname"] =
'-';
449 $newUser[
'email'] = $this->messageParameters[
'lis_person_contact_email_primary'];
457 $newUser[
"profile_incomplete"] = 0;
461 $newUser[
"gender"] =
'n';
462 $newUser[
"title"] =
null;
463 $newUser[
"birthday"] =
null;
464 $newUser[
"institution"] =
null;
465 $newUser[
"department"] =
null;
466 $newUser[
"street"] =
null;
467 $newUser[
"city"] =
null;
468 $newUser[
"zipcode"] =
null;
469 $newUser[
"country"] =
null;
470 $newUser[
"sel_country"] =
null;
471 $newUser[
"phone_office"] =
null;
472 $newUser[
"phone_home"] =
null;
473 $newUser[
"phone_mobile"] =
null;
474 $newUser[
"fax"] =
null;
475 $newUser[
"matriculation"] =
null;
476 $newUser[
"second_email"] =
null;
477 $newUser[
"hobby"] =
null;
478 $newUser[
"client_ip"] =
null;
479 $newUser[
"passwd_salt"] =
null;
480 $newUser[
"latitude"] =
null;
481 $newUser[
"longitude"] =
null;
482 $newUser[
"loc_zoom"] =
null;
483 $newUser[
"last_login"] =
null;
484 $newUser[
"first_login"] =
null;
485 $newUser[
"last_profile_prompt"] =
null;
488 $newUser[
"referral_comment"] =
null;
489 $newUser[
"approve_date"] =
null;
490 $newUser[
"agree_date"] =
null;
491 $newUser[
"inactivation_date"] =
null;
492 $newUser[
"time_limit_from"] =
null;
493 $newUser[
"time_limit_until"] =
null;
494 $newUser[
"is_self_registered"] =
null;
497 $newUser[
"passwd_enc_type"] =
"";
498 $newUser[
"active"] =
true;
499 $newUser[
"time_limit_owner"] = 7;
500 $newUser[
"time_limit_unlimited"] = 0;
501 $newUser[
"time_limit_message"] = 0;
502 $newUser[
"passwd"] =
" ";
506 $userObj->assignData($newUser);
507 $userObj->setTitle($userObj->getFullname());
508 $userObj->setDescription($userObj->getEmail());
514 $userObj->setTimeLimitOwner(7);
515 $userObj->setTimeLimitUnlimited(
false);
516 $userObj->setTimeLimitFrom(time() - 5);
518 $userObj->setTimeLimitUntil(time() + (
int) $ilClientIniFile->readVariable(
"session",
"expire"));
521 $userObj->setOwner(6);
523 $userObj->setActive(
true);
525 $userObj->setLastPasswordChangeTS(time());
526 $userObj->saveAsNew();
527 $userObj->writePrefs();
529 $GLOBALS[
'DIC']->rbac()->admin()->assignUser($consumer->
getRole(), $userObj->getId());
531 $this->
getLogger()->info(
'Created new lti user with uid: ' . $userObj->getId() .
' and login: ' . $userObj->getLogin());
532 return $userObj->getId();
538 $this->
getLogger()->info(
'$target_ref_id: ' . $target_ref_id);
539 if (!$target_ref_id) {
540 $this->
getLogger()->warning(
'No target id given');
546 $roles = $this->messageParameters[
'roles'] ??
'';
548 if (!is_string($roles) || empty($roles)) {
549 $this->
getLogger()->warning(
'No role information given or invalid role format.');
553 $this->
getLogger()->info(
"Deassigning all roles for user: " . $user_id);
554 $DIC->rbac()->admin()->deassignUser($obj_settings->getTutorRole(),
$user_id);
555 $DIC->rbac()->admin()->deassignUser($obj_settings->getMemberRole(),
$user_id);
556 $DIC->rbac()->admin()->deassignUser($obj_settings->getAdminRole(),
$user_id);
558 $role_arr = is_array($roles) ? $roles : explode(
',', $roles);
560 $this->
getLogger()->info(
'Recieved roles: ' . implode(
', ', $role_arr));
562 $tree =
$DIC->repositoryTree();
563 $parent = $tree->getParentId($target_ref_id);
567 foreach ($role_arr as $role) {
570 if (isset($local_role_id)) {
571 $this->
getLogger()->info(
'Assigning local role ID: ' . $local_role_id .
' for LTI role: ' . $role .
' to user ID: ' .
$user_id);
572 $DIC->rbac()->admin()->assignUser($local_role_id,
$user_id);
574 $this->
getLogger()->info(
'No local role mapping found for LTI role: ' . $role);
593 'http://purl.imsglobal.org/vocab/lti/system/person#TestUser' =>
null,
594 'http://purl.imsglobal.org/vocab/lis/v2/system/person#Administrator' => $settings->
getAdminRole(),
595 'http://purl.imsglobal.org/vocab/lis/v2/system/person#None' =>
null,
596 'http://purl.imsglobal.org/vocab/lis/v2/system/person#AccountAdmin' =>
null,
597 'http://purl.imsglobal.org/vocab/lis/v2/system/person#Creator' =>
null,
598 'http://purl.imsglobal.org/vocab/lis/v2/system/person#SysAdmin' =>
null,
599 'http://purl.imsglobal.org/vocab/lis/v2/system/person#SysSupport' =>
null,
600 'http://purl.imsglobal.org/vocab/lis/v2/system/person#User' =>
null,
603 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Administrator' => $settings->
getAdminRole(),
604 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Faculty' => $settings->
getTutorRole(),
605 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Guest' =>
null,
606 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#None' =>
null,
607 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Other' =>
null,
608 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Staff' =>
null,
609 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Student' => $settings->
getMemberRole(),
610 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Alumni' =>
null,
611 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Instructor' => $settings->
getTutorRole(),
612 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Learner' => $settings->
getMemberRole(),
613 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Member' => $settings->
getMemberRole(),
614 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Mentor' =>
null,
615 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#Observer' =>
null,
616 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#ProspectiveStudent' =>
null,
619 'http://purl.imsglobal.org/vocab/lis/v2/membership#Administrator' => $settings->
getAdminRole(),
620 'http://purl.imsglobal.org/vocab/lis/v2/membership#ContentDeveloper' =>
null,
621 'http://purl.imsglobal.org/vocab/lis/v2/membership#Instructor' => $settings->
getTutorRole(),
622 'http://purl.imsglobal.org/vocab/lis/v2/membership#Learner' => $settings->
getMemberRole(),
623 'http://purl.imsglobal.org/vocab/lis/v2/membership#Mentor' =>
null,
624 'http://purl.imsglobal.org/vocab/lis/v2/membership#Manager' => $settings->
getAdminRole(),
625 'http://purl.imsglobal.org/vocab/lis/v2/membership#Member' => $settings->
getMemberRole(),
626 'http://purl.imsglobal.org/vocab/lis/v2/membership#Officer' =>
null,
629 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistant' => $settings->
getTutorRole(),
630 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistantGroup' => $settings->
getTutorRole(),
631 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistantOffering' => $settings->
getTutorRole(),
632 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistantSection' => $settings->
getTutorRole(),
633 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistantSectionAssociation' => $settings->
getTutorRole(),
634 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#TeachingAssistantTemplate' => $settings->
getTutorRole(),
636 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#Grader' => $settings->
getTutorRole(),
638 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#GuestInstructor' => $settings->
getTutorRole(),
639 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#Lecturer' => $settings->
getTutorRole(),
640 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#PrimaryInstructor' => $settings->
getTutorRole(),
641 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#SecondaryInstructor' => $settings->
getTutorRole(),
643 'http://purl.imsglobal.org/vocab/lis/v2/membership/Instructor#ExternalInstructor' => $settings->
getTutorRole(),
646 'http://purl.imsglobal.org/vocab/lis/v2/membership/Learner#ExternalLearner' => $settings->
getMemberRole(),
647 'http://purl.imsglobal.org/vocab/lis/v2/membership/Learner#GuestLearner' => $settings->
getMemberRole(),
648 'http://purl.imsglobal.org/vocab/lis/v2/membership/Learner#Learner' => $settings->
getMemberRole(),
649 'http://purl.imsglobal.org/vocab/lis/v2/membership/Learner#NonCreditLearner' => $settings->
getMemberRole(),
652 'http://purl.imsglobal.org/vocab/lis/v2/membership/Manager#AreaManager' => $settings->
getAdminRole(),
653 'http://purl.imsglobal.org/vocab/lis/v2/membership/Manager#CourseCoordinator' =>
null,
654 'http://purl.imsglobal.org/vocab/lis/v2/membership/Manager#ExternalObserver' =>
null,
655 'http://purl.imsglobal.org/vocab/lis/v2/membership/Manager#Manager' => $settings->
getAdminRole(),
656 'http://purl.imsglobal.org/vocab/lis/v2/membership/Manager#Observer' =>
null,
659 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#Advisor' =>
null,
660 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#Auditor' =>
null,
661 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalAdvisor' =>
null,
662 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalAuditor' =>
null,
663 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalLearningFacilitator' =>
null,
664 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalMentor' =>
null,
665 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalReviewer' =>
null,
666 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#ExternalTutor' =>
null,
667 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#LearningFacilitator' =>
null,
668 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#Mentor' =>
null,
669 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#Reviewer' =>
null,
670 'http://purl.imsglobal.org/vocab/lis/v2/membership/Mentor#Tutor' => $settings->
getTutorRole(),
673 'http://purl.imsglobal.org/vocab/lis/v2/membership/Officer#Chair' =>
null,
674 'http://purl.imsglobal.org/vocab/lis/v2/membership/Officer#Communications' =>
null,
675 'http://purl.imsglobal.org/vocab/lis/v2/membership/Officer#Secretary' =>
null,
676 'http://purl.imsglobal.org/vocab/lis/v2/membership/Officer#Treasurer' =>
null,
677 'http://purl.imsglobal.org/vocab/lis/v2/membership/Officer#Vice-Chair' =>
null,
680 'http://purl.imsglobal.org/vocab/lis/v2/membership/ContentDeveloper#ContentDeveloper' =>
null,
681 'http://purl.imsglobal.org/vocab/lis/v2/membership/ContentDeveloper#ContentExpert' =>
null,
682 'http://purl.imsglobal.org/vocab/lis/v2/membership/ContentDeveloper#ExternalContentExpert' =>
null,
683 'http://purl.imsglobal.org/vocab/lis/v2/membership/ContentDeveloper#Librarian' =>
null,
686 'http://purl.imsglobal.org/vocab/lis/v2/membership/Member#Member' => $settings->
getMemberRole(),
689 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#Administrator' => $settings->
getAdminRole(),
690 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#Developer' =>
null,
691 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#ExternalDeveloper' =>
null,
692 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#ExternalSupport' =>
null,
693 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#ExternalSystemAdministrator' =>
null,
694 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#Support' =>
null,
695 'http://purl.imsglobal.org/vocab/lis/v2/membership/Administrator#SystemAdministrator' =>
null,
702 'ContentDeveloper' =>
null,
711 if (isset($role_map[$lti_role])) {
712 return $role_map[$lti_role];
713 } elseif (isset($simple_name_map[$lti_role])) {
715 return $simple_name_map[$lti_role];
OAuth based lti authentication.
static getServerIdByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
static getActiveAuthModes()
get all active authmode server ids
findAuthKeyId(string $a_oauth_consumer_key)
find consumer key id
static getKeyByAuthMode(string $a_auth_mode)
Get auth id by auth mode.
__construct(ilAuthCredentials $credentials)
Constructor.
static lookupConsumer(int $a_sid)
Lookup consumer title.
static getAuthModeByKey(string $a_auth_key)
Get auth mode by key.
createUser(ilLTIPlatform $consumer)
create new user @access protected
findAuthPrefix(int $a_lti_id)
find lti id
ilLTIDataConnector $dataConnector
mapLTIRoleToLocalRole(string $lti_role, ilLTIProviderObjectSetting $settings)
Maps an LTI role (URI or simple name) to a local ILIAS role ID.
findUserId(string $a_oauth_user, string $a_oauth_id, string $a_user_prefix)
Find user by auth mode and lti id.
handleLocalRoleAssignments(int $user_id, ilLTIPlatform $consumer, int $target_ref_id, ?int $default_rol_id=null)
updateUser(int $a_local_user_id, ilLTIPlatform $consumer)
update existing user @access protected
static isAuthModeLTI(string $a_auth_mode)
Check if user auth mode is LTI.
findGlobalRole(int $a_lti_id)
find global role of consumer
ilAuthCredentials $credentials
const int STATUS_AUTHENTICATION_FAILED
setReason(string $a_reason)
Set reason.
setAuthenticatedUserId(int $a_id)
setStatus(int $a_status)
Set auth status.
const int STATUS_AUTHENTICATED
static _generateLogin(string $a_login)
generate free login by starting with a default string and adding postfix numbers
const int AUTH_PROVIDER_LTI
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
static _lookupId(string|array $a_user_str)
static _checkExternalAuthAccount(string $a_auth, string $a_account, bool $tryFallback=true)
check whether external account and authentication method matches with a user
static _lookupObjectId(int $ref_id)
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
static get(string $a_var)
static clear(string $a_var)
static set(string $a_var, $a_val)
Set a value.
static now()
Return current timestamp in Y-m-d H:i:s format.
doAuthentication(ilAuthStatus $status)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc