Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Commit b09af7d

Browse files
committed
[NEXUS-8036] Use custom 401 status line message when content protection by token is enabled
1 parent 119b2f4 commit b09af7d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

components/nexus-core/src/main/java/org/sonatype/nexus/security/filter/authc/NexusHttpAuthenticationFilter.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,21 @@ protected boolean onAccessDenied(ServletRequest request, ServletResponse respons
180180
return loggedIn;
181181
}
182182

183+
/**
184+
* Allow customization of 401 status line message.
185+
*/
186+
protected String getUnauthorizedMessage(final ServletRequest request) {
187+
return "Unauthorized";
188+
}
189+
183190
/**
184191
* If request comes from a web-browser render an error page, else perform default challenge.
185192
*/
186193
@Override
187194
protected boolean sendChallenge(final ServletRequest request, final ServletResponse response) {
188195
if (browserDetector.isBrowserInitiated(request)) {
189196
HttpServletResponse httpResponse = WebUtils.toHttp(response);
190-
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
197+
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED, getUnauthorizedMessage(request));
191198
// omit WWW-Authenticate we do NOT want to have browser prompt
192199

193200
Map<String,Object> params = ImmutableMap.of(
@@ -209,7 +216,13 @@ protected boolean sendChallenge(final ServletRequest request, final ServletRespo
209216
return false;
210217
}
211218
else {
212-
return super.sendChallenge(request, response);
219+
String message = getUnauthorizedMessage(request);
220+
getLogger().debug("Authentication required: sending 401 Authentication challenge response: {}", message);
221+
HttpServletResponse httpResponse = WebUtils.toHttp(response);
222+
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED, message);
223+
String authcHeader = getAuthcScheme() + " realm=\"" + getApplicationName() + "\"";
224+
httpResponse.setHeader(AUTHENTICATE_HEADER, authcHeader);
225+
return false;
213226
}
214227
}
215228

plugins/basic/nexus-content-plugin/src/main/java/org/sonatype/nexus/content/internal/ContentAuthenticationFilter.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,33 @@ private boolean isRestricted(final ServletRequest request) {
6666
return false;
6767
}
6868

69+
/**
70+
* Servlet request attribute to mark request as protected.
71+
*/
72+
private static final String RESTRICTED_ATTR = ContentRestrictedToken.class.getSimpleName();
73+
74+
/**
75+
* Return custom error message when content access is protected.
76+
*/
77+
@Override
78+
protected String getUnauthorizedMessage(final ServletRequest request) {
79+
Object attr = request.getAttribute(RESTRICTED_ATTR);
80+
if (attr != null) {
81+
return "Content access is protected by token";
82+
}
83+
else {
84+
return super.getUnauthorizedMessage(request);
85+
}
86+
}
87+
6988
@Override
7089
protected AuthenticationToken createToken(final ServletRequest request, final ServletResponse response) {
7190
if (isRestricted(request)) {
7291
getLogger().debug("Content authentication for request is restricted");
7392

93+
// mark request as protected for better error messaging
94+
request.setAttribute(RESTRICTED_ATTR, true);
95+
7496
// We know our super-class makes UsernamePasswordTokens, ask super to pull out the relevant details
7597
UsernamePasswordToken basis = (UsernamePasswordToken) super.createToken(request, response);
7698

0 commit comments

Comments
 (0)