Skip to content

Commit 277e564

Browse files
Apply changes for PR surpuplied by Maxence
Signed-off-by: Micke Nordin <[email protected]> Co-authored-by: Maxence Lange <[email protected]>
1 parent d0d95fc commit 277e564

File tree

4 files changed

+62
-70
lines changed

4 files changed

+62
-70
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<name>Global Site Selector</name>
55
<summary>Nextcloud Portal to redirect users to the right instance</summary>
66
<description>The Global Site Selector allows you to run multiple small Nextcloud instances and redirect users to the right server</description>
7-
<version>2.3.1</version>
7+
<version>2.5.0-beta1</version>
88
<licence>agpl</licence>
99
<author>Bjoern Schiessle</author>
1010
<author>Maxence Lange</author>

lib/Controller/SlaveController.php

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* @copyright Copyright (c) 2017 Bjoern Schiessle <[email protected]>
54
*
@@ -78,32 +77,6 @@ public function __construct(
7877
parent::__construct($appName, $request);
7978
}
8079

81-
private function modifyRedirectUriForClient() {
82-
83-
$requestUri = $this->request->getRequestUri();
84-
// check for both possible direct webdav end-points
85-
$isDirectWebDavAccess = strpos($requestUri, 'remote.php/webdav') !== false || strpos($requestUri, 'remote.php/dav') !== false;
86-
// direct webdav access with old client or general purpose webdav clients
87-
if ($isDirectWebDavAccess) {
88-
$this->logger->debug('redirectUser: client direct webdav request');
89-
$redirectUrl = $target . '/remote.php/webdav/';
90-
} else {
91-
$this->logger->debug('redirectUser: client request generating apptoken');
92-
$data = $this->createAppToken($jwt)->getData();
93-
if (!isset($data['token'])) {
94-
$info = 'getAppToken - data doesn\'t contain token: ' . json_encode($data);
95-
throw new \Exception($info);
96-
}
97-
$appToken = $data['token'];
98-
99-
$redirectUrl =
100-
'nc://login/server:' . $requestUri . '&user:' . urlencode($uid) . '&password:' . urlencode(
101-
$appToken
102-
);
103-
}
104-
return $redirectUrl;
105-
}
106-
10780
/**
10881
* @PublicPage
10982
* @NoCSRFRequired
@@ -134,7 +107,7 @@ public function autoLogin(string $jwt): RedirectResponse {
134107
list($uid, $password, $options) = $this->decodeJwt($jwt);
135108
$this->logger->debug('uid: ' . $uid . ', options: ' . json_encode($options));
136109

137-
$target = $options['target'];
110+
$target = (string) $options['target'];
138111
if (($options['backend'] ?? '') === 'saml') {
139112
$this->logger->debug('saml enabled');
140113
$this->autoprovisionIfNeeded($uid, $options);
@@ -170,7 +143,6 @@ public function autoLogin(string $jwt): RedirectResponse {
170143
return new RedirectResponse($masterUrl);
171144
} catch (\Exception $e) {
172145
$this->logger->warning('issue during login process', ['exception' => $e]);
173-
174146
return new RedirectResponse($masterUrl);
175147
}
176148

@@ -182,30 +154,25 @@ public function autoLogin(string $jwt): RedirectResponse {
182154

183155
$user = $this->userManager->get($uid);
184156
if ($user instanceof IUser) {
185-
$this->logger->debug('emiting AfterLoginOnSlaveEvent event');
186-
$this->eventDispatcher->dispatchTyped(
187-
new AfterLoginOnSlaveEvent($user)
188-
);
157+
$this->logger->debug('emitting AfterLoginOnSlaveEvent event');
158+
$this->eventDispatcher->dispatchTyped(new AfterLoginOnSlaveEvent($user));
189159
}
190-
$redirectUrl = $this->urlGenerator->getAbsoluteURL($target);
191160

192161
/* see if we need to handle client login */
193-
$clientFeatureEnabled = filter_var($this->config->getAppValue('globalsiteselector', 'client_feature_enabled', 'false'), FILTER_VALIDATE_BOOLEAN);
194-
if ($clientFeatureEnabled) {
195-
$this->logger->debug('Client redirect feature enabled');
196-
197-
$isClient = $this->request->isUserAgent(
162+
$clientFeatureEnabled = ($this->config->getAppValue(Application::APP_ID, 'client_feature_enabled', 'false') === 'true');
163+
if ($clientFeatureEnabled
164+
&& $this->request->isUserAgent(
198165
[
199166
IRequest::USER_AGENT_CLIENT_IOS,
200167
IRequest::USER_AGENT_CLIENT_ANDROID,
201168
IRequest::USER_AGENT_CLIENT_DESKTOP,
202169
'/^.*\(Android\)$/'
203170
]
204-
);
205-
}
206-
207-
if ($isClient) {
208-
$redirectUrl = $this->modifyRedirectUriForClient();
171+
)) {
172+
$this->logger->debug('managing request as emerging from client');
173+
$redirectUrl = $this->modifyRedirectUriForClient($uid, $target, $jwt);
174+
} else {
175+
$redirectUrl = $this->urlGenerator->getAbsoluteURL($target);
209176
}
210177

211178
$this->logger->debug('redirecting to ' . $redirectUrl);
@@ -295,4 +262,32 @@ protected function autoprovisionIfNeeded($uid, $options) {
295262
$this->userBackend->createUserIfNotExists($uid);
296263
$this->userBackend->updateAttributes($uid, $options);
297264
}
265+
266+
267+
private function modifyRedirectUriForClient(
268+
string $uid,
269+
string $target,
270+
string $jwt
271+
): string {
272+
$requestUri = $this->request->getRequestUri();
273+
$isDirectWebDavAccess = str_contains($requestUri, 'remote.php/webdav') || str_contains($requestUri, 'remote.php/dav');
274+
275+
// direct webdav access with old client or general purpose webdav clients
276+
if ($isDirectWebDavAccess) {
277+
$this->logger->debug('redirectUser: client direct webdav request to ' . $target);
278+
$redirectUrl = $target . '/remote.php/webdav/';
279+
} else {
280+
$this->logger->debug('redirectUser: client request generating apptoken');
281+
$data = $this->createAppToken($jwt)->getData();
282+
if (!isset($data['token'])) {
283+
throw new \Exception('getAppToken - data missing token: ' . json_encode($data));
284+
}
285+
$appToken = $data['token'];
286+
287+
$redirectUrl = 'nc://login/server:' . $requestUri . '&user:' . urlencode($uid) . '&password:' . urlencode($appToken);
288+
}
289+
290+
$this->logger->debug('generated client redirect url: ' . $redirectUrl);
291+
return $redirectUrl;
292+
}
298293
}

lib/Events/AfterLoginOnSlaveEvent.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
* This event is triggered after GSS login is finalized on the slave.
3333
**/
3434
class AfterLoginOnSlaveEvent extends Event {
35-
public function __construct(private IUser $user)
36-
{
37-
parent::__construct();
38-
}
39-
public function getUser(): IUser
40-
{
41-
return $this->user;
42-
}
35+
public function __construct(private IUser $user) {
36+
parent::__construct();
37+
}
38+
39+
public function getUser(): IUser {
40+
return $this->user;
41+
}
4342
}

lib/Master.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,33 +234,31 @@ protected function redirectUser($uid, $password, $location, array $options = [])
234234
$this->logger->debug('redirectUser: direct login so forward to target node');
235235
$jwt = $this->createJwt($uid, $password, $options);
236236
$redirectUrl = $location . '/index.php/apps/globalsiteselector/autologin?jwt=' . $jwt;
237-
238-
$clientFeatureEnabled = filter_var($this->config->getAppValue('globalsiteselector', 'client_feature_enabled', 'false'), FILTER_VALIDATE_BOOLEAN);
239-
if (!$clientFeatureEnabled) {
240-
$isClient = $this->request->isUserAgent(
241-
[
242-
IRequest::USER_AGENT_CLIENT_IOS,
243-
IRequest::USER_AGENT_CLIENT_ANDROID,
244-
IRequest::USER_AGENT_CLIENT_DESKTOP,
245-
'/^.*\(Android\)$/'
246-
]
247-
);
248237

238+
$clientFeatureEnabled = ($this->config->getAppValue(Application::APP_ID, 'client_feature_enabled', 'false') === 'true');
239+
$isClient = $this->request->isUserAgent(
240+
[
241+
IRequest::USER_AGENT_CLIENT_IOS,
242+
IRequest::USER_AGENT_CLIENT_ANDROID,
243+
IRequest::USER_AGENT_CLIENT_DESKTOP,
244+
'/^.*\(Android\)$/'
245+
]
246+
);
247+
248+
$this->logger->debug('redirectUser client checks: ' . json_encode(['enabled' => $clientFeatureEnabled, 'isClient' => $isClient]));
249+
if (!$clientFeatureEnabled && $isClient) {
249250
$requestUri = $this->request->getRequestUri();
250251
// check for both possible direct webdav end-points
251252
$isDirectWebDavAccess = strpos($requestUri, 'remote.php/webdav') !== false;
252253
$isDirectWebDavAccess = $isDirectWebDavAccess || strpos($requestUri, 'remote.php/dav') !== false;
253254
// direct webdav access with old client or general purpose webdav clients
254-
if ($isClient && $isDirectWebDavAccess) {
255+
if ($isDirectWebDavAccess) {
255256
$this->logger->debug('redirectUser: client direct webdav request');
256257
$redirectUrl = $location . '/remote.php/webdav/';
257-
} elseif ($isClient && !$isDirectWebDavAccess) {
258+
} else {
258259
$this->logger->debug('redirectUser: client request generating apptoken');
259260
$appToken = $this->getAppToken($location, $uid, $password, $options);
260-
$redirectUrl =
261-
'nc://login/server:' . $location . '&user:' . urlencode($uid) . '&password:' . urlencode(
262-
$appToken
263-
);
261+
$redirectUrl = 'nc://login/server:' . $location . '&user:' . urlencode($uid) . '&password:' . urlencode($appToken);
264262
}
265263
}
266264

0 commit comments

Comments
 (0)