1414use OCP \Defaults ;
1515use OCP \IRequest ;
1616use OCP \ISession ;
17+ use OCP \IURLGenerator ;
1718use OCP \Security \Bruteforce \IThrottler ;
1819use OCP \Share \Exceptions \ShareNotFound ;
1920use OCP \Share \IManager ;
2223use Sabre \DAV \Auth \Backend \AbstractBasic ;
2324use Sabre \DAV \Exception \NotAuthenticated ;
2425use Sabre \DAV \Exception \NotFound ;
26+ use Sabre \DAV \Exception \PreconditionFailed ;
2527use Sabre \DAV \Exception \ServiceUnavailable ;
2628use Sabre \HTTP ;
2729use Sabre \HTTP \RequestInterface ;
@@ -44,24 +46,25 @@ public function __construct(
4446 private ISession $ session ,
4547 private IThrottler $ throttler ,
4648 private LoggerInterface $ logger ,
49+ private IURLGenerator $ urlGenerator ,
4750 ) {
4851 // setup realm
4952 $ defaults = new Defaults ();
5053 $ this ->realm = $ defaults ->getName ();
5154 }
5255
5356 /**
54- * @param RequestInterface $request
55- * @param ResponseInterface $response
56- *
57- * @return array
5857 * @throws NotAuthenticated
5958 * @throws ServiceUnavailable
6059 */
6160 public function check (RequestInterface $ request , ResponseInterface $ response ): array {
6261 try {
6362 $ this ->throttler ->sleepDelayOrThrowOnMax ($ this ->request ->getRemoteAddress (), self ::BRUTEFORCE_ACTION );
6463
64+ if (count ($ _COOKIE ) > 0 && !$ this ->request ->passesStrictCookieCheck () && $ this ->getShare ()->getPassword () !== null ) {
65+ throw new PreconditionFailed ('Strict cookie check failed ' );
66+ }
67+
6568 $ auth = new HTTP \Auth \Basic (
6669 $ this ->realm ,
6770 $ request ,
@@ -77,6 +80,15 @@ public function check(RequestInterface $request, ResponseInterface $response): a
7780 return $ this ->checkToken ();
7881 } catch (NotAuthenticated $ e ) {
7982 throw $ e ;
83+ } catch (PreconditionFailed $ e ) {
84+ $ response ->setHeader (
85+ 'Location ' ,
86+ $ this ->urlGenerator ->linkToRoute (
87+ 'files_sharing.share.showShare ' ,
88+ [ 'token ' => $ this ->getToken () ],
89+ ),
90+ );
91+ throw $ e ;
8092 } catch (\Exception $ e ) {
8193 $ class = get_class ($ e );
8294 $ msg = $ e ->getMessage ();
@@ -87,7 +99,6 @@ public function check(RequestInterface $request, ResponseInterface $response): a
8799
88100 /**
89101 * Extract token from request url
90- * @return string
91102 * @throws NotFound
92103 */
93104 private function getToken (): string {
@@ -104,7 +115,7 @@ private function getToken(): string {
104115
105116 /**
106117 * Check token validity
107- * @return array
118+ *
108119 * @throws NotFound
109120 * @throws NotAuthenticated
110121 */
@@ -152,15 +163,13 @@ private function checkToken(): array {
152163 protected function validateUserPass ($ username , $ password ) {
153164 $ this ->throttler ->sleepDelayOrThrowOnMax ($ this ->request ->getRemoteAddress (), self ::BRUTEFORCE_ACTION );
154165
155- $ token = $ this ->getToken ();
156166 try {
157- $ share = $ this ->shareManager -> getShareByToken ( $ token );
167+ $ share = $ this ->getShare ( );
158168 } catch (ShareNotFound $ e ) {
159169 $ this ->throttler ->registerAttempt (self ::BRUTEFORCE_ACTION , $ this ->request ->getRemoteAddress ());
160170 return false ;
161171 }
162172
163- $ this ->share = $ share ;
164173 \OC_User::setIncognitoMode (true );
165174
166175 // check if the share is password protected
@@ -203,7 +212,13 @@ protected function validateUserPass($username, $password) {
203212 }
204213
205214 public function getShare (): IShare {
206- assert ($ this ->share !== null );
215+ $ token = $ this ->getToken ();
216+
217+ if ($ this ->share === null ) {
218+ $ share = $ this ->shareManager ->getShareByToken ($ token );
219+ $ this ->share = $ share ;
220+ }
221+
207222 return $ this ->share ;
208223 }
209224}
0 commit comments