Skip to content

Commit 0dc6c8a

Browse files
keshav-725pedro-hos
authored andcommitted
[ELY-2548] BasicAuthenticationMechanism should return FORBIDDEN instead of UNAUTHORIZED
1 parent 197587a commit 0dc6c8a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

http/basic/src/main/java/org/wildfly/security/http/basic/BasicAuthenticationMechanism.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.wildfly.common.iteration.ByteIterator;
4343
import org.wildfly.security.auth.callback.AvailableRealmsCallback;
4444
import org.wildfly.security.http.HttpAuthenticationException;
45+
import org.wildfly.security.http.HttpConstants;
4546
import org.wildfly.security.http.HttpServerRequest;
4647
import org.wildfly.security.http.HttpServerResponse;
4748
import org.wildfly.security.mechanism.http.UsernamePasswordAuthenticationMechanism;
@@ -170,7 +171,7 @@ public void evaluateRequest(final HttpServerRequest request) throws HttpAuthenti
170171
httpBasic.debugf("User %s authorization failed.", username);
171172
fail();
172173

173-
request.authenticationFailed(httpBasic.authorizationFailed(username), response -> prepareResponse(request, displayRealmName, response));
174+
request.authenticationFailed(httpBasic.authorizationFailed(username), response -> response.setStatusCode(HttpConstants.FORBIDDEN));
174175
return;
175176
}
176177

tests/base/src/test/java/org/wildfly/security/http/basic/BasicAuthenticationMechanismTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@ public void testStatefulBasicRFC7617Examples() throws Exception {
112112
testStatefulBasic("Aladdin", "WallyWorld", "open sesame", "basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
113113
testStatefulBasic("test", "foo", "123\u00A3", "BASIC dGVzdDoxMjPCow==");
114114
}
115+
116+
@Test
117+
public void testBasicUnauthorizedUser() throws Exception {
118+
HttpServerAuthenticationMechanism mechanism = basicFactory.createAuthenticationMechanism(HttpConstants.BASIC_NAME,
119+
Collections.singletonMap(HttpConstants.CONFIG_REALM, "test-realm"), getCallbackHandler("unauthorizedUser", "test-realm", "password"));
120+
TestingHttpServerRequest request = new TestingHttpServerRequest(new String[] {"Basic dW5hdXRob3JpemVkVXNlcjpwYXNzd29yZA=="});
121+
mechanism.evaluateRequest(request);
122+
Assert.assertEquals(Status.FAILED, request.getResult());
123+
TestingHttpServerResponse response = request.getResponse();
124+
Assert.assertEquals(HttpConstants.FORBIDDEN, response.getStatusCode());
125+
}
115126
}

0 commit comments

Comments
 (0)