ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
AuthFrontendCredentialsApache.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Psr\Http\Message\ServerRequestInterface;
25use ilUtil;
26use ilSetting;
27use ilContext;
28use ilLogLevel;
33
35{
36 private ServerRequestInterface $http_request;
39
40 public function __construct(ServerRequestInterface $http_request, ilCtrlInterface $ctrl)
41 {
42 $this->http_request = $http_request;
43 $this->ctrl = $ctrl;
44 $this->settings = new ilSetting('apache_auth');
46 }
47
52 public function tryAuthenticationOnLoginPage(): void
53 {
54 $cmd = (string) ($this->http_request->getQueryParams()['cmd'] ?? '');
55 if ($cmd === '') {
56 $cmd = (string) ($this->http_request->getParsedBody()['cmd'] ?? '');
57 }
58
59 if ($cmd === 'force_login') {
60 return;
61 }
62
63 if (!$this->getSettings()->get('apache_enable_auth', '0')) {
64 return;
65 }
66
67 if (!$this->getSettings()->get('apache_auth_authenticate_on_login_page', '0')) {
68 return;
69 }
70
71 if ((\defined('IL_CERT_SSO') && \IL_CERT_SSO === true) ||
73 isset($this->http_request->getQueryParams()['passed_sso'])) {
74 return;
75 }
76
77 $path = (string) ($this->http_request->getServerParams()['REQUEST_URI'] ?? '');
78 if (str_starts_with($path, '/')) {
79 $path = substr($path, 1);
80 }
81
82 if (!str_starts_with($path, 'http')) {
83 $parts = parse_url(ILIAS_HTTP_PATH);
84 $path = $parts['scheme'] . '://' . $parts['host'] . '/' . $path;
85 }
86
87 $this->ctrl->redirectToURL(
89 './sso/index.php?' . http_build_query([
90 'force_mode_apache' => 1,
91 'r' => $path,
92 'cookie_path' => IL_COOKIE_PATH,
93 'ilias_path' => ILIAS_HTTP_PATH,
94 ])
95 )
96 );
97 }
98
99 protected function getSettings(): ilSetting
100 {
101 return $this->settings;
102 }
103
104 public function initFromRequest(): void
105 {
106 $mapping_field_name = $this->getSettings()->get('apache_auth_username_direct_mapping_fieldname', '');
107
108 $this->logger->dump($this->http_request->getServerParams(), ilLogLevel::DEBUG);
109 $this->logger->debug($mapping_field_name);
110
111 switch ($this->getSettings()->get('apache_auth_username_config_type')) {
113 if (isset($this->http_request->getServerParams()[$mapping_field_name])) {
114 $this->setUsername($this->http_request->getServerParams()[$mapping_field_name]);
115 }
116 break;
117
119 $factory = new UsernameProviderFactory();
120 $resolver = new UsernameResolver($factory->fromClassNames(
121 require CollectUsernameProvidersObjective::PATH()
122 ), $this->logger);
123
124 $this->setUsername($resolver->resolve($this->http_request)->asString());
125 break;
126 }
127 }
128
129 public function hasValidTargetUrl(): bool
130 {
131 $target_url = trim((string) ($this->http_request->getQueryParams()['r'] ?? ''));
132 if ($target_url === '') {
133 return false;
134 }
135
136 $valid_hosts = [];
137 $path = ILIAS_DATA_DIR . '/' . CLIENT_ID . '/apache_auth_allowed_domains.txt';
138 if (file_exists($path) && is_readable($path)) {
139 foreach (file($path) as $line) {
140 if (trim($line)) {
141 $valid_hosts[] = trim($line);
142 }
143 }
144 }
145
146 return (new WhiteListUrlValidator($target_url, $valid_hosts))->isValid();
147 }
148
149 public function getTargetUrl(): string
150 {
151 return ilUtil::appendUrlParameterString(trim($this->http_request->getQueryParams()['r']), 'passed_sso=1');
152 }
153}
const IL_CERT_SSO(isset($_GET['client_id']))
Definition: index.php:47
const IL_COOKIE_PATH
Definition: index.php:48
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(ServerRequestInterface $http_request, ilCtrlInterface $ctrl)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static supportsRedirects()
Are redirects supported?
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
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
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc