ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
AuthFrontendCredentialsApache.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ilUtil;
27use ilSetting;
28use ilContext;
29use ilLogLevel;
34
36{
37 private readonly ilSetting $settings;
38
39 public function __construct(
40 private readonly GlobalHttpState $http,
41 private readonly Factory $refinery,
42 private readonly ilCtrlInterface $ctrl
43 ) {
44 $this->settings = new ilSetting('apache_auth');
46 }
47
52 public function tryAuthenticationOnLoginPage(): void
53 {
54 if (!$this->getSettings()->get('apache_enable_auth', '0')) {
55 return;
56 }
57
58 if (!$this->getSettings()->get('apache_auth_authenticate_on_login_page', '0')) {
59 return;
60 }
61
62 if ((\defined('IL_CERT_SSO') && \IL_CERT_SSO === true) ||
64 $this->http->wrapper()->query()->has('passed_sso')) {
65 return;
66 }
67
68 $redirect_url = ilUtil::getHtmlPath('./sso/index.php?force_mode_apache=1');
69
70 if ($this->http->wrapper()->query()->has('target')) {
71 $url = (string) ($this->http->request()->getServerParams()['REQUEST_URI'] ?? '');
72 if (str_starts_with($url, '/')) {
73 $url = substr($url, 1);
74 }
75
76 if (!str_starts_with($url, 'http')) {
77 $parts = parse_url(ILIAS_HTTP_PATH);
78 $url = $parts['scheme'] . '://' . $parts['host'] . '/' . $url;
79 }
80
81 $uri = new \ILIAS\Data\URI($url);
82 /*
83 * If `tryAuthenticationOnLoginPage` is called and a permanent-link "target" is provided,
84 * we ensure using `goto.php` as landing page after successful authentication
85 */
86 $uri = $uri->withPath(str_replace(['login.php', 'ilias.php'], 'goto.php', $uri->getPath()));
88 $redirect_url,
89 'r=' . urlencode($this->refinery->uri()->toString()->transform($uri))
90 );
91 }
92
93 $this->ctrl->redirectToURL($redirect_url);
94 }
95
96 protected function getSettings(): ilSetting
97 {
98 return $this->settings;
99 }
100
101 public function initFromRequest(): void
102 {
103 $mapping_field_name = $this->getSettings()->get('apache_auth_username_direct_mapping_fieldname', '');
104
105 $this->logger->dump($this->http->request()->getServerParams(), ilLogLevel::DEBUG);
106 $this->logger->debug($mapping_field_name);
107
108 switch ($this->getSettings()->get('apache_auth_username_config_type')) {
110 if (isset($this->http->request()->getServerParams()[$mapping_field_name])) {
111 $this->setUsername($this->http->request()->getServerParams()[$mapping_field_name]);
112 }
113 break;
114
116 $factory = new UsernameProviderFactory();
117 $resolver = new UsernameResolver($factory->fromClassNames(
118 require CollectUsernameProvidersObjective::PATH()
119 ), $this->logger);
120
121 $this->setUsername($resolver->resolve($this->http->request())->asString());
122 break;
123 }
124 }
125
126 public function hasValidTargetUrl(): bool
127 {
128 $target_url = trim(
129 $this->http->wrapper()->query()->retrieve('r', $this->refinery->byTrying([
130 $this->refinery->kindlyTo()->string(),
131 $this->refinery->always(''),
132 ]))
133 );
134 if ($target_url === '') {
135 return false;
136 }
137
138 $valid_hosts = [];
139 $path = ILIAS_DATA_DIR . '/' . CLIENT_ID . '/apache_auth_allowed_domains.txt';
140 if (file_exists($path) && is_readable($path)) {
141 foreach (file($path) as $line) {
142 if (trim($line)) {
143 $valid_hosts[] = trim($line);
144 }
145 }
146 }
147
148 return (new WhiteListUrlValidator($target_url, $valid_hosts))->isValid();
149 }
150
151 public function getTargetUrl(): string
152 {
153 $target_url = trim($this->http->wrapper()->query()->retrieve('r', $this->refinery->kindlyTo()->string()));
154
155 return ilUtil::appendUrlParameterString($target_url, 'passed_sso=1');
156 }
157}
const IL_CERT_SSO(isset($_GET['client_id']))
Definition: index.php:47
foreach($components as $component) $resolver
Resolves a username by selecting the first provider (by descending priority) that returns a non-empty...
tryAuthenticationOnLoginPage()
Check if an authentication attempt should be done when login page has been called.
__construct(private readonly GlobalHttpState $http, private readonly Factory $refinery, private readonly ilCtrlInterface $ctrl)
Builds data types.
Definition: Factory.php:36
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ilContext.php:26
static supportsRedirects()
Are redirects supported?
Definition: ilContext.php:89
Logging factory.
ILIAS Setting Class.
Util class various functions, usage as namespace.
static getHtmlPath(string $relative_path)
get url of path
static appendUrlParameterString(string $a_url, string $a_par, bool $xml_style=false)
const CLIENT_ID
Definition: constants.php:41
const ILIAS_DATA_DIR
Definition: constants.php:44
$http
Definition: deliver.php:30
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$url
Definition: shib_logout.php:70