Skip to content

Commit b92e2fc

Browse files
committed
Fix authz 500 Internal Server error when not authorized; DataPackageManager: Change exception from 401 Unauthorized to 403 Forbidden in DataPackageManagerResource (#165)
1 parent dd5028c commit b92e2fc

2 files changed

Lines changed: 37 additions & 52 deletions

File tree

DataPackageManager/src/edu/lternet/pasta/datapackagemanager/DataPackageManager.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import edu.lternet.pasta.common.eml.DataPackage.DataDescendant;
2929
import edu.lternet.pasta.common.eml.DataPackage.DataSource;
3030
import edu.lternet.pasta.common.eml.EMLParser;
31+
import edu.lternet.pasta.common.security.access.ForbiddenException;
3132
import edu.lternet.pasta.common.security.access.UnauthorizedException;
3233
import edu.lternet.pasta.common.security.authorization.AccessMatrix;
3334
import edu.lternet.pasta.common.security.authorization.Rule;
@@ -1675,11 +1676,13 @@ public Integer getOldestRevision(String scope, Integer identifier)
16751676
* @param permission
16761677
* The requested permission for accessing the resource (e.g., READ).
16771678
* @return The boolean result of the request.
1678-
* @throws IllegalArgumentException, UnauthorizedException
1679+
* @throws IllegalArgumentException, ForbiddenException
16791680
*/
1680-
public Boolean isAuthorized(AuthToken authToken, String resourceId,
1681-
Rule.Permission permission) throws IllegalArgumentException,
1682-
UnauthorizedException {
1681+
public Boolean isAuthorized(
1682+
AuthToken authToken,
1683+
String resourceId,
1684+
Rule.Permission permission
1685+
) throws IllegalArgumentException, ForbiddenException {
16831686

16841687
Boolean isAuthorized = null;
16851688
DataPackageRegistry dpr = null;
@@ -1692,34 +1695,21 @@ public Boolean isAuthorized(AuthToken authToken, String resourceId,
16921695

16931696
try {
16941697
dpr = new DataPackageRegistry(dbDriver, dbURL, dbUser, dbPassword);
1695-
} catch (ClassNotFoundException e) {
1698+
} catch (ClassNotFoundException | SQLException e) {
16961699
logger.error(e.getMessage());
1697-
e.printStackTrace();
1698-
} catch (SQLException e) {
1699-
logger.error(e.getMessage());
1700-
e.printStackTrace();
17011700
}
1702-
1703-
Authorizer authorizer = new Authorizer(dpr);
1704-
try {
1705-
isAuthorized = authorizer.isAuthorized(authToken, resourceId, permission);
1706-
1707-
if (!isAuthorized) {
1708-
String gripe = "User \"" + userId + "\" is not authorized to "
1709-
+ permission + " this " + resourceId + " resource!";
1710-
throw new UnauthorizedException(gripe);
1711-
}
1712-
1713-
} catch (ClassNotFoundException e) {
1714-
logger.error(e.getMessage());
1715-
e.printStackTrace();
1716-
} catch (SQLException e) {
1701+
1702+
Authorizer authorizer = new Authorizer(dpr);
1703+
try {
1704+
isAuthorized = authorizer.isAuthorized(authToken, resourceId, permission);
1705+
if (!isAuthorized) {
1706+
String msg = String.format("User %s is not authorized at the requested level to %s", userId, resourceId);
1707+
throw new ForbiddenException(msg);
1708+
}
1709+
} catch (ClassNotFoundException | SQLException e) {
17171710
logger.error(e.getMessage());
1718-
e.printStackTrace();
17191711
}
1720-
1721-
return isAuthorized;
1722-
1712+
return isAuthorized;
17231713
}
17241714

17251715

DataPackageManager/src/edu/lternet/pasta/datapackagemanager/DataPackageManagerResource.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import edu.lternet.pasta.common.audit.AuditRecord;
3131
import edu.lternet.pasta.common.eml.DataPackage;
3232
import edu.lternet.pasta.common.eml.EMLParser;
33+
import edu.lternet.pasta.common.security.access.ForbiddenException;
3334
import edu.lternet.pasta.common.security.access.UnauthorizedException;
3435
import edu.lternet.pasta.common.security.authorization.AccessMatrix;
3536
import edu.lternet.pasta.common.security.authorization.InvalidPermissionException;
@@ -1997,12 +1998,10 @@ public Response appendProvenance(@Context HttpHeaders headers,
19971998
}
19981999
*/
19992000

2000-
/**
2001-
* <strong>Is Authorized</strong> (to <em>READ</em> resource) operation,
2002-
* determines whether the user as defined in the authentication token has
2003-
* permission to access the specified data package resource. Allowed permissions
2004-
* are "read", "write", or "changePermission" and must be verbatim.
2005-
*
2001+
/**
2002+
* <strong>Is Authorized</strong> operation, determines whether the user as, defined in the
2003+
* authentication token, has the provided permission (read, write, or changePermission) on the
2004+
* specified data package resource. *
20062005
* <h4>Requests:</h4>
20072006
* <table border="1" cellspacing="0" cellpadding="3">
20082007
* <tr>
@@ -2097,6 +2096,7 @@ public Response isAuthorized (
20972096
) {
20982097

20992098
AuthToken authToken = null;
2099+
21002100
String entryText = String.format("/package/authz?resourceId=%s&permission=%s", resourceId, permission);
21012101
ResponseBuilder responseBuilder = null;
21022102
Response response = null;
@@ -2107,37 +2107,32 @@ public Response isAuthorized (
21072107
authToken = getAuthToken(headers);
21082108
String userId = authToken.getUserId();
21092109

2110-
// Is user authorized to run the service method?
2111-
boolean serviceMethodAuthorized =
2112-
isServiceMethodAuthorized(serviceMethodName, servicePermission, authToken);
2113-
if (!serviceMethodAuthorized) {
2114-
throw new UnauthorizedException(
2115-
"User " + userId + " is not authorized to execute service method " +
2116-
serviceMethodName);
2117-
}
2118-
21192110
try {
2111+
// Is user authorized to run the service method?
2112+
boolean serviceMethodAuthorized = isServiceMethodAuthorized(serviceMethodName, servicePermission, authToken);
2113+
if (!serviceMethodAuthorized) {
2114+
String msg = String.format("User %s is not authorized to execute service method %s", userId, serviceMethodName);
2115+
throw new ForbiddenException(msg);
2116+
}
21202117

21212118
DataPackageManager dpm = new DataPackageManager();
2122-
Boolean isAuthorized = dpm.isAuthorized(authToken, resourceId, resourcePermission);
2119+
dpm.isAuthorized(authToken, resourceId, resourcePermission);
21232120

2124-
if (isAuthorized != null && isAuthorized) {
2125-
responseBuilder = Response.ok(resourceId);
2126-
response = responseBuilder.build();
2127-
}
2121+
responseBuilder = Response.ok(resourceId);
2122+
response = responseBuilder.build();
21282123
} catch (IllegalArgumentException e) {
21292124
entryText = e.getMessage();
21302125
response = WebExceptionFactory.makeBadRequest(e).getResponse();
21312126
} catch (UnauthorizedException e) {
21322127
entryText = e.getMessage();
21332128
response = WebExceptionFactory.makeUnauthorized(e).getResponse();
2129+
} catch (ForbiddenException e) {
2130+
entryText = e.getMessage();
2131+
response = WebExceptionFactory.makeForbidden(e).getResponse();
21342132
} catch (ResourceNotFoundException e) {
21352133
entryText = e.getMessage();
21362134
response = WebExceptionFactory.makeNotFound(e).getResponse();
2137-
} catch (ResourceDeletedException e) {
2138-
entryText = e.getMessage();
2139-
response = WebExceptionFactory.makeConflict(e).getResponse();
2140-
} catch (ResourceExistsException e) {
2135+
} catch (ResourceDeletedException | ResourceExistsException e) {
21412136
entryText = e.getMessage();
21422137
response = WebExceptionFactory.makeConflict(e).getResponse();
21432138
} catch (UserErrorException e) {

0 commit comments

Comments
 (0)