Remove auth module challenge, fix api descriptions.
lbs-gitea/maptology/master This commit looks good

Remove auth module challenge by sending forbidden instead of unauthoized
when no credentials are provided to work around automatic credential
storage in browser and ugly pop-up dialogs that will not look good in a
webapp. To support direct testing of get requests, consider using the
api console instead. Alternatively, provide the new magic "testAuth"
parameter as part of the query string. This will restore the original
behavior, i.e. /user/current will fail immediately and
/user/current?testAuth will generate a challenge, if no authorization
header is provided as part of the request. 
Cleanup api responses and work around problems with swagger plugin that
will not pickup security annotations on methods.
This commit is contained in:
2017-11-12 10:36:31 +01:00
parent c83e239e88
commit cd697a52e4
4 changed files with 68 additions and 18 deletions
@@ -32,7 +32,7 @@ import io.swagger.annotations.ResponseHeader;
*
* @author Marcus
*/
@Api
@Api(authorizations= {@Authorization(value="BasicAuth")})
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/")
@@ -45,7 +45,10 @@ public interface INoticeService {
*/
@GET
@Path("notice")
@ApiOperation(value = "Returns a page of notices.", response = NoticePage.class)
@ApiOperation(value = "Returns a page of notices.")
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_OK, message="Notice page retrieved successfully.", response=NoticePage.class)
})
Response getNotices(@BeanParam PageConfig pageConfig);
/**
@@ -58,7 +61,8 @@ public interface INoticeService {
@Path("notice")
@ApiOperation(value = "Adds a new notice and returns the notice as well as its location.")
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_CREATED, message="Notice created successfully.", response = Notice.class, responseHeaders= {@ResponseHeader(name=HttpHeaders.LOCATION, description="Url of the added notice.")})
@ApiResponse(code=HttpServletResponse.SC_CREATED, message="Notice created successfully.", response = Notice.class, responseHeaders= {@ResponseHeader(name=HttpHeaders.LOCATION, description="Url of the added notice.")}),
@ApiResponse(code=HttpServletResponse.SC_FORBIDDEN, message="Insufficient privileges for addition.")
})
@RoleRequired(value=Roles.ADMIN)
@Authorization(value="BasicAuth")
@@ -88,15 +92,12 @@ public interface INoticeService {
@PUT
@Path("notice/{id:-?\\d+}")
@ApiOperation(value = "Updates an existing notice.")
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_OK, message="Notice updated successfully.", response=Notice.class),
@ApiResponse(code=HttpServletResponse.SC_CONFLICT, message="Update conflicts with another update."),
@ApiResponse(code=HttpServletResponse.SC_FORBIDDEN, message="Insufficient privileges for modification.")
})
@RoleRequired(value=Roles.ADMIN)
@Authorization(value="BasicAuth")
Response setNotice(
@ApiParam(value="The id of the notice.") @PathParam("id") long id,
@ApiParam(value="The values of the notice.") Notice notice);
@@ -116,8 +117,4 @@ public interface INoticeService {
@ApiParam(defaultValue="0", value="The earlies time in epoch milliseconds.")
@QueryParam("since") @DefaultValue("0") long since);
}
@@ -1,5 +1,6 @@
package com.maptology.api.user;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@@ -14,6 +15,8 @@ import com.maptology.api.access.Roles;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
/**
@@ -33,19 +36,27 @@ public interface IUserService {
*/
@GET
@Path("user")
@ApiOperation(value = "Returns a list of users.", response = UserPage.class)
@ApiOperation(value = "Returns a list of users.")
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_OK, message="User page retrieved successfully.", response=UserPage.class),
@ApiResponse(code=HttpServletResponse.SC_FORBIDDEN, message="Insufficient privileges for retrieval.")
})
@RoleRequired({ Roles.ADMIN })
Response getUsers(@BeanParam PageConfig pageConfig);
/**
* Returns the user that is logged in.
* Returns the user that is authorized.
*
* @return The user that is currently logged in.
* @return The user that is currently authorized.
*/
@GET
@Path("user/current")
@ApiOperation(value = "Returns the current (logged-in) user.", response = User.class)
@ApiOperation(value = "Returns the user currently authorized.")
@PrincipalRequired
@ApiResponses({
@ApiResponse(code=HttpServletResponse.SC_OK, message="User retrieved successfully.", response=User.class),
@ApiResponse(code=HttpServletResponse.SC_FORBIDDEN, message="No valid authorization found.")
})
Response getCurrentUser();
}
@@ -2,6 +2,7 @@ package com.maptology.service.auth;
import java.io.IOException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -36,7 +37,16 @@ import com.maptology.service.entity.user.UserEntity;
*/
public class MaptologyAuthModule extends AbstractHttpServletAuthModule {
public static final String REALM = "Basic realm=\"maptology-api\"";
/**
* The realm transmitted as part of a challenge.
*/
public static final String REALM_CHALLENGE = "Basic realm=\"maptology-api\"";
/**
* The "magic" parameter that enables challenges.
*/
public static final String PARAMETER_CHALLENGE = "testAuth";
/**
* The logger to write debug data to the console.
@@ -66,6 +76,8 @@ public class MaptologyAuthModule extends AbstractHttpServletAuthModule {
if (!messageInfo.isAuthMandatory()) {
// no auth needed, so this is ok
return AuthStatus.SUCCESS;
} else if (requiresAuthChallenge(request)) {
return sendUnauthorized(messageInfo, "Access not allowed.");
} else {
return sendForbidden(messageInfo, "Access not allowed.");
}
@@ -116,6 +128,23 @@ public class MaptologyAuthModule extends AbstractHttpServletAuthModule {
}
}
/**
* Tests whether the request requires an auth challenge to be sent out.
* This is done, if the caller has set the
*
* @param request The request.
* @return
*/
private boolean requiresAuthChallenge(HttpServletRequest request) {
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
if (e.nextElement().equals(PARAMETER_CHALLENGE)) {
return true;
}
}
return false;
}
/**
* Loads the user and its roles in a single transaction.
*
@@ -186,7 +215,20 @@ public class MaptologyAuthModule extends AbstractHttpServletAuthModule {
*/
private AuthStatus sendForbidden(HttpServletMessageInfo messageInfo, String message) {
HttpServletResponse response = messageInfo.getResponse();
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, REALM);
return sendError(response, HttpServletResponse.SC_FORBIDDEN, message);
}
/**
* Sends an unauthorized message and returns failed. A browser will create a
* credentials dialog in response to receiving this message.
*
* @param messageInfo The message info.
* @param message The error message.
* @return AuthStatus.FAILED
*/
private AuthStatus sendUnauthorized(HttpServletMessageInfo messageInfo, String message) {
HttpServletResponse response = messageInfo.getResponse();
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, REALM_CHALLENGE);
return sendError(response, HttpServletResponse.SC_UNAUTHORIZED, message);
}
@@ -26,9 +26,9 @@ public class UserResourceTest extends AbstractResourceTest {
public void testUnauthorizedNoLogin() {
IUserService service = getTarget().proxy(IUserService.class);
Response r1 = service.getCurrentUser();
Assert.assertEquals("GET users without auth should fail.", HttpServletResponse.SC_UNAUTHORIZED, r1.getStatus());
Assert.assertEquals("GET users without auth should fail.", HttpServletResponse.SC_FORBIDDEN, r1.getStatus());
Response r2 = service.getUsers(new PageConfig(0, 10));
Assert.assertEquals("GET users without auth should fail.", HttpServletResponse.SC_UNAUTHORIZED, r2.getStatus());
Assert.assertEquals("GET users without auth should fail.", HttpServletResponse.SC_FORBIDDEN, r2.getStatus());
}
@Test