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}
0 commit comments