Implemented OAuth2 exception handler.
Change-Id: I5363e42ee78fa74de982cda3215247c7e73137bb
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
index 6251147..483c6e2 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ClientControllerTest.java
@@ -4,6 +4,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+import java.util.List;
+import java.util.Set;
+import java.util.Map.Entry;
+
import javax.ws.rs.core.MultivaluedMap;
import org.apache.http.entity.ContentType;
@@ -18,6 +22,7 @@
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.core.util.MultivaluedMapImpl;
+import com.sun.jersey.spi.container.ContainerRequest;
import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
import de.ids_mannheim.korap.config.Attributes;
@@ -38,6 +43,18 @@
private HttpAuthorizationHandler handler;
private String username = "OAuth2ClientControllerTest";
+ private void checkWWWAuthenticateHeader (ClientResponse response) {
+ Set<Entry<String, List<String>>> headers =
+ response.getHeaders().entrySet();
+
+ for (Entry<String, List<String>> header : headers) {
+ if (header.getKey().equals(ContainerRequest.WWW_AUTHENTICATE)) {
+ assertEquals("Basic realm=\"Kustvakt\"",
+ header.getValue().get(0));
+ }
+ }
+ }
+
private ClientResponse testRegisterConfidentialClient ()
throws KustvaktException {
@@ -70,8 +87,8 @@
response = testRegisterConfidentialClient();
assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
node = JsonUtils.readTree(response.getEntity(String.class));
- assertEquals(StatusCodes.CLIENT_REGISTRATION_FAILED,
- node.at("/errors/0/0").asInt());
+ assertEquals(OAuthError.TokenResponse.INVALID_REQUEST,
+ node.at("/error").asText());
testDeregisterClientIncorrectCredentials(clientId);
testDeregisterConfidentialClient(clientId, clientSecret);
@@ -161,7 +178,6 @@
.entity(form).delete(ClientResponse.class);
String entity = response.getEntity(String.class);
-// System.out.println(entity);
assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
JsonNode node = JsonUtils.readTree(entity);
@@ -169,5 +185,7 @@
node.at("/error").asText());
assertEquals("Invalid client credentials.",
node.at("/error_description").asText());
+
+ checkWWWAuthenticateHeader(response);
}
}
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ControllerTest.java
index f52c883..794ea6b 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/OAuth2ControllerTest.java
@@ -1,12 +1,17 @@
package de.ids_mannheim.korap.web.controller;
+import static org.junit.Assert.assertEquals;
+
import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response.Status;
import org.apache.http.entity.ContentType;
+import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
+import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.net.HttpHeaders;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
@@ -17,6 +22,7 @@
import de.ids_mannheim.korap.config.Attributes;
import de.ids_mannheim.korap.config.SpringJerseyTest;
import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.utils.JsonUtils;
/**
* @author margaretha
@@ -34,9 +40,8 @@
KustvaktException {
MultivaluedMap<String, String> form = new MultivaluedMapImpl();
-// form.add("grant_type", "blahblah");
- form.add("grant_type", GrantType.REFRESH_TOKEN.name());
-
+ form.add("grant_type", "blahblah");
+
ClientResponse response = resource().path("oauth2").path("token")
.header(Attributes.AUTHORIZATION,
handler.createBasicAuthorizationHeaderValue(username,
@@ -46,8 +51,15 @@
ContentType.APPLICATION_FORM_URLENCODED)
.entity(form).post(ClientResponse.class);
- System.out.println(response.getStatus());
- System.out.println(response.getEntity(String.class));
+ String entity = response.getEntity(String.class);
+// System.out.println(entity);
+ assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+
+ JsonNode node = JsonUtils.readTree(entity);
+ assertEquals("blahblah is not supported.",
+ node.get("error_description").asText());
+ assertEquals(OAuthError.TokenResponse.UNSUPPORTED_GRANT_TYPE,
+ node.get("error"));
}
}
diff --git a/full/src/test/resources/test-config.xml b/full/src/test/resources/test-config.xml
index 1757481..66b6c97 100644
--- a/full/src/test/resources/test-config.xml
+++ b/full/src/test/resources/test-config.xml
@@ -181,7 +181,10 @@
<constructor-arg ref="kustvakt_db" />
</bean>
- <bean id="kustvakt_response" class="de.ids_mannheim.korap.web.FullResponseHandler">
+ <bean id="kustvaktExceptionHandler" class="de.ids_mannheim.korap.web.KustvaktExceptionHandler">
+ <constructor-arg index="0" name="iface" ref="kustvakt_auditing" />
+ </bean>
+ <bean id="oauth2ExceptionHandler" class="de.ids_mannheim.korap.web.OAuth2ExceptionHandler">
<constructor-arg index="0" name="iface" ref="kustvakt_auditing" />
</bean>