Skip to content

Commit

Permalink
Making tests fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jun 10, 2013
1 parent 3204dec commit 74794a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
@@ -1,7 +1,6 @@


package org.fcrepo.auth.oauth.filter; package org.fcrepo.auth.oauth.filter;


import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;


Expand All @@ -22,6 +21,8 @@ public class RestrictToAuthNFilter implements Filter {


private static final Logger LOGGER = getLogger(RestrictToAuthNFilter.class); private static final Logger LOGGER = getLogger(RestrictToAuthNFilter.class);


private static final String AUTHENTICATED_SECTION = "/authenticated/";

@Override @Override
public void init(final FilterConfig filterConfig) throws ServletException { public void init(final FilterConfig filterConfig) throws ServletException {
LOGGER.debug("Initialized {}", this.getClass().getName()); LOGGER.debug("Initialized {}", this.getClass().getName());
Expand All @@ -41,14 +42,22 @@ public void doFilter(final ServletRequest request,
throws IOException, ServletException { throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request; final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse res = (HttpServletResponse) response; final HttpServletResponse res = (HttpServletResponse) response;
if (req.getUserPrincipal() != null) { final String requestURI = req.getRequestURI();
res.sendError(SC_UNAUTHORIZED); LOGGER.debug("Received request at URI: {}", requestURI);
} if (requestURI.contains(AUTHENTICATED_SECTION)) {
if (req.isUserInRole("kosher")) { // a protected resource
chain.doFilter(request, response); LOGGER.debug("{} is a protected resource.", requestURI);
return; if (req.getUserPrincipal() != null) {
LOGGER.debug("Couldn't find authenticated user!");
res.sendError(SC_UNAUTHORIZED);
} else {
LOGGER.debug("Found authenticated user.");
chain.doFilter(request, response);
}
} else { } else {
res.sendError(SC_FORBIDDEN); // not a protected resource
LOGGER.debug("{} is not a protected resource.", requestURI);
chain.doFilter(request, response);
} }


} }
Expand Down
@@ -1,6 +1,7 @@


package org.fcrepo.auth.oauth.integration.api; package org.fcrepo.auth.oauth.integration.api;


import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED; import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -38,6 +39,14 @@ public void testGetToken() throws Exception {
@Test @Test
public void testUseToken() throws ClientProtocolException, IOException { public void testUseToken() throws ClientProtocolException, IOException {
logger.trace("Entering testUseToken()..."); logger.trace("Entering testUseToken()...");
logger.debug("Trying to write an object to authenticated area without authentication via token...");
final HttpResponse failure =
client.execute(postObjMethod("authenticated/testUseToken"));
assertEquals(
"Was able to write to an authenticated area when I shouldn't be able to",
SC_UNAUTHORIZED, failure.getStatusLine().getStatusCode());
logger.debug("Failed as expected.");
logger.debug("Now trying with authentication via token...");
final HttpPost post = final HttpPost post =
new HttpPost( new HttpPost(
tokenEndpoint + tokenEndpoint +
Expand Down
2 changes: 0 additions & 2 deletions src/test/resources/spring-test/rest.xml
Expand Up @@ -15,8 +15,6 @@
<!-- Mints PIDs--> <!-- Mints PIDs-->
<bean class="org.fcrepo.identifiers.UUIDPidMinter"/> <bean class="org.fcrepo.identifiers.UUIDPidMinter"/>


<!-- AuthN filters -->

<!-- used by (de)serialization endpoints --> <!-- used by (de)serialization endpoints -->
<util:map id="serializers" key-type="java.lang.String" map-class="java.util.HashMap" <util:map id="serializers" key-type="java.lang.String" map-class="java.util.HashMap"
value-type="org.fcrepo.serialization.FedoraObjectSerializer"> value-type="org.fcrepo.serialization.FedoraObjectSerializer">
Expand Down

0 comments on commit 74794a1

Please sign in to comment.