blob: 92461adc5986ac5f130625f9d191b17d7a9e8a1a [file] [log] [blame]
margarethaa0486272018-04-12 19:59:31 +02001package de.ids_mannheim.korap.web.controller;
2
margaretha05122312018-04-16 15:01:34 +02003import static org.junit.Assert.assertEquals;
margarethaf839dde2018-04-16 17:52:57 +02004import static org.junit.Assert.assertNotNull;
margarethafb027f92018-04-23 20:00:13 +02005import static org.junit.Assert.assertTrue;
6
7import java.net.URI;
margaretha05122312018-04-16 15:01:34 +02008
margarethaa0486272018-04-12 19:59:31 +02009import javax.ws.rs.core.MultivaluedMap;
margaretha05122312018-04-16 15:01:34 +020010import javax.ws.rs.core.Response.Status;
margarethaa0486272018-04-12 19:59:31 +020011
12import org.apache.http.entity.ContentType;
margaretha05122312018-04-16 15:01:34 +020013import org.apache.oltu.oauth2.common.error.OAuthError;
margarethaf839dde2018-04-16 17:52:57 +020014import org.apache.oltu.oauth2.common.message.types.TokenType;
margarethaa0486272018-04-12 19:59:31 +020015import org.junit.Test;
16import org.springframework.beans.factory.annotation.Autowired;
17
margaretha05122312018-04-16 15:01:34 +020018import com.fasterxml.jackson.databind.JsonNode;
margarethaa0486272018-04-12 19:59:31 +020019import com.google.common.net.HttpHeaders;
margarethaa0486272018-04-12 19:59:31 +020020import com.sun.jersey.api.client.ClientResponse;
margarethaa0486272018-04-12 19:59:31 +020021import com.sun.jersey.core.util.MultivaluedMapImpl;
22
23import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
24import de.ids_mannheim.korap.config.Attributes;
25import de.ids_mannheim.korap.config.SpringJerseyTest;
26import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha05122312018-04-16 15:01:34 +020027import de.ids_mannheim.korap.utils.JsonUtils;
margarethaa0486272018-04-12 19:59:31 +020028
29/**
30 * @author margaretha
31 *
32 */
33public class OAuth2ControllerTest extends SpringJerseyTest {
34
35 @Autowired
36 private HttpAuthorizationHandler handler;
margarethab4ce6602018-04-26 20:23:57 +020037
margarethafb027f92018-04-23 20:00:13 +020038 private ClientResponse requestAuthorizationConfidentialClient (
39 MultivaluedMap<String, String> form) throws KustvaktException {
40
41 return resource().path("oauth2").path("authorize")
margarethaf839dde2018-04-16 17:52:57 +020042 .header(Attributes.AUTHORIZATION,
43 handler.createBasicAuthorizationHeaderValue(
margarethafb027f92018-04-23 20:00:13 +020044 "fCBbQkAyYzI4NzUxMg", "secret"))
margarethaf839dde2018-04-16 17:52:57 +020045 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
46 .header(HttpHeaders.CONTENT_TYPE,
47 ContentType.APPLICATION_FORM_URLENCODED)
48 .entity(form).post(ClientResponse.class);
49 }
50
margarethafb027f92018-04-23 20:00:13 +020051 @Test
52 public void testAuthorizeConfidentialClient () throws KustvaktException {
53 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
54 form.add("response_type", "code");
55 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
56 form.add("username", "dory");
57 form.add("password", "password");
margarethaa452c5e2018-04-25 22:48:09 +020058
margarethafb027f92018-04-23 20:00:13 +020059 ClientResponse response = requestAuthorizationConfidentialClient(form);
60
61 assertEquals(Status.TEMPORARY_REDIRECT.getStatusCode(),
62 response.getStatus());
63 URI redirectUri = response.getLocation();
64 assertTrue(redirectUri.getQuery().startsWith("code"));
65 }
66
67 @Test
68 public void testAuthorizeInvalidRedirectUri () throws KustvaktException {
69 String redirectUri = "https://different.uri/redirect";
70
71 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
72 form.add("response_type", "code");
73 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
74 form.add("username", "dory");
75 form.add("password", "password");
76 form.add("redirect_uri", redirectUri);
77 ClientResponse response = requestAuthorizationConfidentialClient(form);
78
79 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
80
81 String entity = response.getEntity(String.class);
82 JsonNode node = JsonUtils.readTree(entity);
83 assertEquals(OAuthError.CodeResponse.INVALID_REQUEST,
84 node.at("/error").asText());
85 assertEquals(redirectUri + " is unknown",
86 node.at("/error_description").asText());
87 }
88
89 @Test
90 public void testAuthorizeMissingRequiredParameters ()
91 throws KustvaktException {
92 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
93 // missing code
94 ClientResponse response = requestAuthorizationConfidentialClient(form);
95
96 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
97
98 String entity = response.getEntity(String.class);
99 JsonNode node = JsonUtils.readTree(entity);
100 assertEquals(OAuthError.CodeResponse.INVALID_REQUEST,
101 node.at("/error").asText());
102 assertEquals("Missing response_type parameter value",
103 node.at("/error_description").asText());
104
105 // missing client_id
106 form.add("response_type", "code");
107 response = requestAuthorizationConfidentialClient(form);
108 entity = response.getEntity(String.class);
109 node = JsonUtils.readTree(entity);
110 assertEquals("Missing parameters: client_id",
111 node.at("/error_description").asText());
112 }
113
114 @Test
115 public void testAuthorizeInvalidResponseType () throws KustvaktException {
116 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
117 form.add("response_type", "string");
118
119 ClientResponse response = requestAuthorizationConfidentialClient(form);
120 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
121
122 String entity = response.getEntity(String.class);
123 JsonNode node = JsonUtils.readTree(entity);
124 assertEquals(OAuthError.CodeResponse.INVALID_REQUEST,
125 node.at("/error").asText());
126 assertEquals("Invalid response_type parameter value",
127 node.at("/error_description").asText());
128 }
129
130 private ClientResponse requestToken (MultivaluedMap<String, String> form)
131 throws KustvaktException {
margaretha6374f722018-04-17 18:45:57 +0200132 return resource().path("oauth2").path("token")
133 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
134 .header(HttpHeaders.CONTENT_TYPE,
135 ContentType.APPLICATION_FORM_URLENCODED)
136 .entity(form).post(ClientResponse.class);
137 }
138
139 @Test
margarethaa452c5e2018-04-25 22:48:09 +0200140 public void testRequestTokenAuthorizationConfidential ()
141 throws KustvaktException {
142
143 MultivaluedMap<String, String> authForm = new MultivaluedMapImpl();
144 authForm.add("response_type", "code");
145 authForm.add("client_id", "fCBbQkAyYzI4NzUxMg");
146 authForm.add("username", "dory");
147 authForm.add("password", "password");
148// form.add("scope", "read");
149 ClientResponse response = requestAuthorizationConfidentialClient(authForm);
150 URI redirectUri = response.getLocation();
151 String code = redirectUri.getQuery().split("=")[1];
152
153 MultivaluedMap<String, String> tokenForm = new MultivaluedMapImpl();
154 tokenForm.add("grant_type", "authorization_code");
155 tokenForm.add("client_id", "fCBbQkAyYzI4NzUxMg");
156 tokenForm.add("client_secret", "secret");
157 tokenForm.add("code", code);
158
159 response = requestToken(tokenForm);
160 String entity = response.getEntity(String.class);
161 JsonNode node = JsonUtils.readTree(entity);
162 assertNotNull(node.at("/access_token").asText());
163 assertNotNull(node.at("/refresh_token").asText());
164 assertEquals(TokenType.BEARER.toString(),
165 node.at("/token_type").asText());
166 assertNotNull(node.at("/expires_in").asText());
167 }
168
169 @Test
margaretha6374f722018-04-17 18:45:57 +0200170 public void testRequestTokenPasswordGrantConfidential ()
margarethafb027f92018-04-23 20:00:13 +0200171 throws KustvaktException {
margaretha6374f722018-04-17 18:45:57 +0200172 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
173 form.add("grant_type", "password");
margarethafb027f92018-04-23 20:00:13 +0200174 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
175 form.add("client_secret", "secret");
176 form.add("username", "dory");
177 form.add("password", "password");
margaretha6374f722018-04-17 18:45:57 +0200178
margarethafb027f92018-04-23 20:00:13 +0200179 ClientResponse response = requestToken(form);
margaretha6374f722018-04-17 18:45:57 +0200180 String entity = response.getEntity(String.class);
181
182 JsonNode node = JsonUtils.readTree(entity);
183 assertNotNull(node.at("/access_token").asText());
184 assertNotNull(node.at("/refresh_token").asText());
185 assertEquals(TokenType.BEARER.toString(),
186 node.at("/token_type").asText());
187 assertNotNull(node.at("/expires_in").asText());
188 }
189
190 @Test
margarethafb027f92018-04-23 20:00:13 +0200191 public void testRequestTokenPasswordGrantMissingClientSecret ()
192 throws KustvaktException {
margaretha6374f722018-04-17 18:45:57 +0200193
194 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
195 form.add("grant_type", "password");
margarethafb027f92018-04-23 20:00:13 +0200196 form.add("username", "dory");
197 form.add("password", "password");
198 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
margaretha6374f722018-04-17 18:45:57 +0200199
margarethafb027f92018-04-23 20:00:13 +0200200 ClientResponse response = requestToken(form);
201 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
margaretha6374f722018-04-17 18:45:57 +0200202
203 String entity = response.getEntity(String.class);
204 JsonNode node = JsonUtils.readTree(entity);
margarethafb027f92018-04-23 20:00:13 +0200205 assertEquals(OAuthError.TokenResponse.INVALID_REQUEST,
margaretha6374f722018-04-17 18:45:57 +0200206 node.at("/error").asText());
margarethafb027f92018-04-23 20:00:13 +0200207 assertEquals("Missing parameters: client_secret",
208 node.at("/error_description").asText());
margaretha6374f722018-04-17 18:45:57 +0200209 }
210
211 @Test
212 public void testRequestTokenPasswordGrantMissingClientId ()
margarethafb027f92018-04-23 20:00:13 +0200213 throws KustvaktException {
margaretha6374f722018-04-17 18:45:57 +0200214 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
215 form.add("grant_type", "password");
margarethafb027f92018-04-23 20:00:13 +0200216 form.add("username", "dory");
217 form.add("password", "password");
218 form.add("client_secret", "secret");
margaretha6374f722018-04-17 18:45:57 +0200219
margarethafb027f92018-04-23 20:00:13 +0200220 ClientResponse response = requestToken(form);
margaretha6374f722018-04-17 18:45:57 +0200221 String entity = response.getEntity(String.class);
222 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
223
224 JsonNode node = JsonUtils.readTree(entity);
225 assertEquals(OAuthError.TokenResponse.INVALID_REQUEST,
226 node.at("/error").asText());
margarethafb027f92018-04-23 20:00:13 +0200227 assertEquals("Missing parameters: client_id",
margaretha6374f722018-04-17 18:45:57 +0200228 node.at("/error_description").asText());
229 }
margarethafb027f92018-04-23 20:00:13 +0200230
231 @Test
232 public void testRequestTokenPasswordGrantPublic ()
233 throws KustvaktException {
234 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
235 form.add("grant_type", "password");
236 form.add("username", "dory");
237 form.add("password", "password");
238 form.add("client_id", "iBr3LsTCxOj7D2o0A5m");
239
240 ClientResponse response = requestToken(form);
241 String entity = response.getEntity(String.class);
242
243 JsonNode node = JsonUtils.readTree(entity);
244 assertNotNull(node.at("/access_token").asText());
245 assertNotNull(node.at("/refresh_token").asText());
246 assertEquals(TokenType.BEARER.toString(),
247 node.at("/token_type").asText());
248 assertNotNull(node.at("/expires_in").asText());
249 }
margaretha6374f722018-04-17 18:45:57 +0200250
251 @Test
252 public void testRequestTokenPasswordGrantNonNative ()
margarethafb027f92018-04-23 20:00:13 +0200253 throws KustvaktException {
margaretha6374f722018-04-17 18:45:57 +0200254 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
255 form.add("grant_type", "password");
margarethafb027f92018-04-23 20:00:13 +0200256 form.add("username", "dory");
257 form.add("password", "password");
258 // confidential nonnative
259 form.add("client_id", "9aHsGW6QflV13ixNpez");
260 form.add("client_secret", "secret");
margaretha6374f722018-04-17 18:45:57 +0200261
margarethafb027f92018-04-23 20:00:13 +0200262 ClientResponse response = requestToken(form);
margaretha6374f722018-04-17 18:45:57 +0200263 String entity = response.getEntity(String.class);
margarethafb027f92018-04-23 20:00:13 +0200264 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
margaretha6374f722018-04-17 18:45:57 +0200265
266 JsonNode node = JsonUtils.readTree(entity);
267 assertEquals(OAuthError.TokenResponse.UNAUTHORIZED_CLIENT,
268 node.at("/error").asText());
269 assertEquals("Password grant is not allowed for third party clients",
270 node.at("/error_description").asText());
271 }
272
margarethaf839dde2018-04-16 17:52:57 +0200273 @Test
274 public void testRequestTokenClientCredentialsGrant ()
margarethafb027f92018-04-23 20:00:13 +0200275 throws KustvaktException {
margarethaf839dde2018-04-16 17:52:57 +0200276
277 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
278 form.add("grant_type", "client_credentials");
margarethafb027f92018-04-23 20:00:13 +0200279 form.add("client_id", "fCBbQkAyYzI4NzUxMg");
280 form.add("client_secret", "secret");
281 ClientResponse response = requestToken(form);
margarethaf839dde2018-04-16 17:52:57 +0200282 String entity = response.getEntity(String.class);
283 assertEquals(Status.OK.getStatusCode(), response.getStatus());
284
285 JsonNode node = JsonUtils.readTree(entity);
286 // length?
287 assertNotNull(node.at("/access_token").asText());
288 assertNotNull(node.at("/refresh_token").asText());
289 assertEquals(TokenType.BEARER.toString(),
290 node.at("/token_type").asText());
291 assertNotNull(node.at("/expires_in").asText());
292 }
293
294 @Test
margarethafb027f92018-04-23 20:00:13 +0200295 public void testRequestTokenMissingGrantType () throws KustvaktException {
margarethaf839dde2018-04-16 17:52:57 +0200296 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
margarethafb027f92018-04-23 20:00:13 +0200297 ClientResponse response = requestToken(form);
margarethaf839dde2018-04-16 17:52:57 +0200298 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
299
300 String entity = response.getEntity(String.class);
301 JsonNode node = JsonUtils.readTree(entity);
302 assertEquals(OAuthError.TokenResponse.INVALID_REQUEST,
303 node.at("/error").asText());
304 }
margarethaa0486272018-04-12 19:59:31 +0200305
306 @Test
margarethafb027f92018-04-23 20:00:13 +0200307 public void testRequestTokenUnsupportedGrant () throws KustvaktException {
margarethaa0486272018-04-12 19:59:31 +0200308
309 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
margaretha05122312018-04-16 15:01:34 +0200310 form.add("grant_type", "blahblah");
311
margarethaa0486272018-04-12 19:59:31 +0200312 ClientResponse response = resource().path("oauth2").path("token")
margarethaa0486272018-04-12 19:59:31 +0200313 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
314 .header(HttpHeaders.CONTENT_TYPE,
315 ContentType.APPLICATION_FORM_URLENCODED)
316 .entity(form).post(ClientResponse.class);
317
margaretha05122312018-04-16 15:01:34 +0200318 String entity = response.getEntity(String.class);
margaretha05122312018-04-16 15:01:34 +0200319 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
320
321 JsonNode node = JsonUtils.readTree(entity);
margarethaf839dde2018-04-16 17:52:57 +0200322 assertEquals("Invalid grant_type parameter value",
margaretha05122312018-04-16 15:01:34 +0200323 node.get("error_description").asText());
margarethaf839dde2018-04-16 17:52:57 +0200324 assertEquals(OAuthError.TokenResponse.INVALID_REQUEST,
325 node.get("error").asText());
margarethaa0486272018-04-12 19:59:31 +0200326 }
327
328}