mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
remove legacy exception handling from AuthenticationResource
This commit is contained in:
@@ -5,14 +5,10 @@ import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
|||||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||||
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.SecurityUtils;
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
import org.apache.shiro.authc.DisabledAccountException;
|
|
||||||
import org.apache.shiro.authc.ExcessiveAttemptsException;
|
|
||||||
import org.apache.shiro.subject.Subject;
|
import org.apache.shiro.subject.Subject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.api.rest.RestActionResult;
|
|
||||||
import sonia.scm.security.*;
|
import sonia.scm.security.*;
|
||||||
import sonia.scm.util.HttpUtil;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@@ -20,9 +16,6 @@ import javax.ws.rs.*;
|
|||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.xml.bind.annotation.XmlAccessType;
|
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
|
||||||
|
|
||||||
@Path(AuthenticationResource.PATH)
|
@Path(AuthenticationResource.PATH)
|
||||||
public class AuthenticationResource {
|
public class AuthenticationResource {
|
||||||
@@ -106,38 +99,6 @@ public class AuthenticationResource {
|
|||||||
res = Response.ok( token.compact() ).build();
|
res = Response.ok( token.compact() ).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (DisabledAccountException ex)
|
|
||||||
{
|
|
||||||
if (LOG.isTraceEnabled())
|
|
||||||
{
|
|
||||||
LOG.trace(
|
|
||||||
"authentication failed, account user ".concat(authentication.getUsername()).concat(
|
|
||||||
" is locked"), ex);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG.warn("authentication failed, account {} is locked", authentication.getUsername());
|
|
||||||
}
|
|
||||||
|
|
||||||
res = handleFailedAuthentication(request, ex, Response.Status.FORBIDDEN,
|
|
||||||
WUIAuthenticationFailure.LOCKED);
|
|
||||||
}
|
|
||||||
catch (ExcessiveAttemptsException ex)
|
|
||||||
{
|
|
||||||
if (LOG.isTraceEnabled())
|
|
||||||
{
|
|
||||||
LOG.trace(
|
|
||||||
"authentication failed, account user ".concat(authentication.getUsername()).concat(
|
|
||||||
" is temporary locked"), ex);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOG.warn("authentication failed, account {} is temporary locked", authentication.getUsername());
|
|
||||||
}
|
|
||||||
|
|
||||||
res = handleFailedAuthentication(request, ex, Response.Status.FORBIDDEN,
|
|
||||||
WUIAuthenticationFailure.TEMPORARY_LOCKED);
|
|
||||||
}
|
|
||||||
catch (AuthenticationException ex)
|
catch (AuthenticationException ex)
|
||||||
{
|
{
|
||||||
if (LOG.isTraceEnabled())
|
if (LOG.isTraceEnabled())
|
||||||
@@ -149,8 +110,9 @@ public class AuthenticationResource {
|
|||||||
LOG.warn("authentication failed for user {}", authentication.getUsername());
|
LOG.warn("authentication failed for user {}", authentication.getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
res = handleFailedAuthentication(request, ex, Response.Status.UNAUTHORIZED,
|
// TODO DisabledAccountException, ExcessiveAttemptsException for ui?
|
||||||
WUIAuthenticationFailure.WRONG_CREDENTIALS);
|
|
||||||
|
return Response.status(Response.Status.UNAUTHORIZED).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
@@ -175,45 +137,4 @@ public class AuthenticationResource {
|
|||||||
return Response.noContent().build();
|
return Response.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Response handleFailedAuthentication(HttpServletRequest request,
|
|
||||||
AuthenticationException ex, Response.Status status,
|
|
||||||
WUIAuthenticationFailure failure) {
|
|
||||||
Response response;
|
|
||||||
|
|
||||||
if (HttpUtil.isWUIRequest(request)) {
|
|
||||||
response = Response.ok(new WUIAuthenticationFailedResult(failure,
|
|
||||||
ex.getMessage())).build();
|
|
||||||
} else {
|
|
||||||
response = Response.status(status).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum WUIAuthenticationFailure { LOCKED, TEMPORARY_LOCKED, WRONG_CREDENTIALS }
|
|
||||||
|
|
||||||
@XmlRootElement(name = "result")
|
|
||||||
@XmlAccessorType(XmlAccessType.FIELD)
|
|
||||||
private static final class WUIAuthenticationFailedResult extends RestActionResult {
|
|
||||||
|
|
||||||
private final WUIAuthenticationFailure failure;
|
|
||||||
private final String message;
|
|
||||||
|
|
||||||
public WUIAuthenticationFailedResult(WUIAuthenticationFailure failure, String message) {
|
|
||||||
super(false);
|
|
||||||
this.failure = failure;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public WUIAuthenticationFailure getFailure() {
|
|
||||||
return failure;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user