rishabhdaim commented on code in PR #2957:
URL: https://github.com/apache/jackrabbit-oak/pull/2957#discussion_r3411182170


##########
oak-http/src/test/java/org/apache/jackrabbit/oak/http/OakServletTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.http;
+
+import javax.jcr.Credentials;
+import javax.jcr.NoSuchWorkspaceException;
+import javax.jcr.SimpleCredentials;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.util.Base64;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class OakServletTest {
+
+    private static String basicHeader(String userId, String password) {
+        return "Basic " + Base64.encode(userId + ":" + password);
+    }
+
+    /**
+     * Drives {@link OakServlet#service} and captures the credentials it 
derives
+     * from the {@code Authorization} header. The repository is stubbed to 
throw
+     * {@link NoSuchWorkspaceException} so the request short-circuits (handled 
as
+     * a 404) right after the credentials are parsed.
+     */
+    private static SimpleCredentials parsedCredentials(String authorization)
+            throws Exception {
+        ContentRepository repository = mock(ContentRepository.class);
+        ArgumentCaptor<Credentials> captor =
+                ArgumentCaptor.forClass(Credentials.class);
+        when(repository.login(captor.capture(), isNull()))
+                .thenThrow(new NoSuchWorkspaceException());
+
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        when(request.getHeader("Authorization")).thenReturn(authorization);
+        HttpServletResponse response = mock(HttpServletResponse.class);
+
+        new OakServlet(repository).service(request, response);
+
+        verify(response).sendError(HttpServletResponse.SC_NOT_FOUND);
+        Credentials credentials = captor.getValue();
+        assertTrue(credentials instanceof SimpleCredentials);
+        return (SimpleCredentials) credentials;
+    }
+
+    /**
+     * Asserts that a malformed {@code Authorization} header is rejected with a
+     * 401 challenge and never reaches the repository login.
+     */
+    private static void assertUnauthorized(String authorization)
+            throws Exception {
+        ContentRepository repository = mock(ContentRepository.class);
+        HttpServletRequest request = mock(HttpServletRequest.class);
+        when(request.getHeader("Authorization")).thenReturn(authorization);
+        HttpServletResponse response = mock(HttpServletResponse.class);
+
+        new OakServlet(repository).service(request, response);
+
+        verify(response).setHeader("WWW-Authenticate", "Basic realm=\"Oak\"");
+        verify(response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
+        verify(repository, never()).login(any(), any());
+    }

Review Comment:
   Nits : Move the helper method to the end of Test file.



##########
oak-http/src/main/java/org/apache/jackrabbit/oak/http/OakServlet.java:
##########
@@ -115,6 +107,30 @@ protected void service(
         }
     }
 
+    /**
+     * Parses the credentials from an HTTP {@code Authorization} header using 
the
+     * Basic authentication scheme (RFC 7617). The decoded value has the form
+     * {@code user-id ":" password}; the user-id must not contain a colon, but
+     * the password may, so only the first colon is treated as the separator.
+     * Splitting on every colon would silently truncate passwords that contain
+     * one (weakening authentication), and a missing colon would throw an
+     * {@link ArrayIndexOutOfBoundsException} instead of failing the login.
+     */
+    private Credentials parseBasicCredentials(String authorization)
+            throws LoginException {
+        if (authorization == null || !authorization.startsWith("Basic ")) {
+            throw new LoginException("Missing or unsupported Authorization 
header; expected Basic scheme");
+        }
+        String decoded = Base64.decode(authorization.substring("Basic 
".length()));
+        int colon = decoded.indexOf(':');
+        if (colon < 0) {
+            throw new LoginException("Malformed Basic credentials: missing ':' 
separator");
+        }
+        String userId = decoded.substring(0, colon);

Review Comment:
   Shouldn't we explicitly check for whether the username has a `:` or not, and 
throw an exception if it has ?
   
   cc @anchela @Amoratinos @reschke 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to