1616use OCP \Share \IManager ;
1717use OCP \Share \IShare ;
1818use Sabre \DAV \Auth \Backend \AbstractBasic ;
19+ use Sabre \DAV \Exception \NotAuthenticated ;
1920
2021/**
2122 * Class PublicAuth
@@ -69,22 +70,29 @@ protected function validateUserPass($username, $password) {
6970 if ($ share ->getShareType () === IShare::TYPE_LINK
7071 || $ share ->getShareType () === IShare::TYPE_EMAIL
7172 || $ share ->getShareType () === IShare::TYPE_CIRCLE ) {
73+ // Validate password if provided
7274 if ($ this ->shareManager ->checkPassword ($ share , $ password )) {
75+ // If not set, set authenticated session cookie
76+ if (!$ this ->isShareInSession ($ share )) {
77+ $ this ->addShareToSession ($ share );
78+ }
7379 return true ;
74- } elseif ($ this ->session ->exists (PublicAuth::DAV_AUTHENTICATED )
75- && $ this ->session ->get (PublicAuth::DAV_AUTHENTICATED ) === $ share ->getId ()) {
80+ }
81+
82+ // We are already authenticated for this share in the session
83+ if ($ this ->isShareInSession ($ share )) {
7684 return true ;
77- } else {
78- if (in_array ('XMLHttpRequest ' , explode (', ' , $ this ->request ->getHeader ('X-Requested-With ' )))) {
79- // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
80- http_response_code (401 );
81- header ('WWW-Authenticate: DummyBasic realm=" ' . $ this ->realm . '" ' );
82- throw new \Sabre \DAV \Exception \NotAuthenticated ('Cannot authenticate over ajax calls ' );
83- }
85+ }
8486
85- $ this ->throttler ->registerAttempt (self ::BRUTEFORCE_ACTION , $ this ->request ->getRemoteAddress ());
86- return false ;
87+ if (in_array ('XMLHttpRequest ' , explode (', ' , $ this ->request ->getHeader ('X-Requested-With ' )))) {
88+ // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
89+ http_response_code (401 );
90+ header ('WWW-Authenticate: DummyBasic realm=" ' . $ this ->realm . '" ' );
91+ throw new NotAuthenticated ('Cannot authenticate over ajax calls ' );
8792 }
93+
94+ $ this ->throttler ->registerAttempt (self ::BRUTEFORCE_ACTION , $ this ->request ->getRemoteAddress ());
95+ return false ;
8896 } elseif ($ share ->getShareType () === IShare::TYPE_REMOTE ) {
8997 return true ;
9098 } else {
@@ -95,6 +103,29 @@ protected function validateUserPass($username, $password) {
95103 return true ;
96104 }
97105
106+ private function addShareToSession (IShare $ share ): void {
107+ $ allowedShareIds = $ this ->session ->get (PublicAuth::DAV_AUTHENTICATED ) ?? [];
108+ if (!is_array ($ allowedShareIds )) {
109+ $ allowedShareIds = [];
110+ }
111+
112+ $ allowedShareIds [] = $ share ->getId ();
113+ $ this ->session ->set (PublicAuth::DAV_AUTHENTICATED , $ allowedShareIds );
114+ }
115+
116+ private function isShareInSession (IShare $ share ): bool {
117+ if (!$ this ->session ->exists (PublicAuth::DAV_AUTHENTICATED )) {
118+ return false ;
119+ }
120+
121+ $ allowedShareIds = $ this ->session ->get (PublicAuth::DAV_AUTHENTICATED );
122+ if (!is_array ($ allowedShareIds )) {
123+ return false ;
124+ }
125+
126+ return in_array ($ share ->getId (), $ allowedShareIds );
127+ }
128+
98129 public function getShare (): IShare {
99130 assert ($ this ->share !== null );
100131 return $ this ->share ;
0 commit comments