blob: cfb3f41567cfd7a9ea90c475e7e1de07ccd3bb9d [file] [log] [blame]
margarethaf0085122018-08-16 16:19:53 +02001package de.ids_mannheim.korap.web.controller;
2
3import static org.junit.Assert.assertEquals;
4
5import java.net.URI;
6
7import javax.ws.rs.core.MultivaluedMap;
8import javax.ws.rs.core.Response.Status;
9
10import org.apache.http.entity.ContentType;
margarethac750cbb2018-12-11 12:47:02 +010011import org.apache.oltu.oauth2.common.message.types.GrantType;
margarethaf0085122018-08-16 16:19:53 +020012import org.springframework.util.MultiValueMap;
13import org.springframework.web.util.UriComponentsBuilder;
14
15import com.fasterxml.jackson.databind.JsonNode;
16import com.google.common.net.HttpHeaders;
17import com.sun.jersey.api.client.ClientHandlerException;
18import com.sun.jersey.api.client.ClientResponse;
19import com.sun.jersey.api.client.UniformInterfaceException;
20import com.sun.jersey.core.util.MultivaluedMapImpl;
21
22import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
23import de.ids_mannheim.korap.config.Attributes;
24import de.ids_mannheim.korap.config.SpringJerseyTest;
25import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethac750cbb2018-12-11 12:47:02 +010026import de.ids_mannheim.korap.oauth2.constant.OAuth2Error;
margarethaf0085122018-08-16 16:19:53 +020027import de.ids_mannheim.korap.utils.JsonUtils;
28
margaretha230effb2018-11-29 17:28:18 +010029/**
30 * Provides common methods and variables for OAuth2 tests,
31 * and does not run any test.
margarethaf0085122018-08-16 16:19:53 +020032 *
33 * @author margaretha
34 *
35 */
margarethaf370f542018-08-23 18:51:49 +020036public abstract class OAuth2TestBase extends SpringJerseyTest {
margarethaf0085122018-08-16 16:19:53 +020037
38 protected String publicClientId = "8bIDtZnH6NvRkW2Fq";
39 protected String confidentialClientId = "9aHsGW6QflV13ixNpez";
40 protected String superClientId = "fCBbQkAyYzI4NzUxMg";
41 protected String clientSecret = "secret";
42
margarethac750cbb2018-12-11 12:47:02 +010043 protected ClientResponse requestAuthorizationCode (
margarethaf0085122018-08-16 16:19:53 +020044 MultivaluedMap<String, String> form, String authHeader)
45 throws KustvaktException {
46
margarethaee0cbfe2018-08-28 17:47:14 +020047 return resource().path(API_VERSION).path("oauth2").path("authorize")
margarethaf0085122018-08-16 16:19:53 +020048 .header(Attributes.AUTHORIZATION, authHeader)
49 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
50 .header(HttpHeaders.CONTENT_TYPE,
51 ContentType.APPLICATION_FORM_URLENCODED)
52 .entity(form).post(ClientResponse.class);
53 }
54
margarethac750cbb2018-12-11 12:47:02 +010055 protected String requestAuthorizationCode (String clientId,
margarethaf0085122018-08-16 16:19:53 +020056 String clientSecret, String scope, String authHeader)
57 throws KustvaktException {
58
59 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
60 form.add("response_type", "code");
61 form.add("client_id", clientId);
62 form.add("client_secret", clientSecret);
63 if (scope != null) {
64 form.add("scope", scope);
65 }
66
67 ClientResponse response = requestAuthorizationCode(form, authHeader);
68 assertEquals(Status.TEMPORARY_REDIRECT.getStatusCode(),
69 response.getStatus());
70 URI redirectUri = response.getLocation();
71
72 MultiValueMap<String, String> params = UriComponentsBuilder
73 .fromUri(redirectUri).build().getQueryParams();
74 return params.getFirst("code");
75 }
76
margarethac750cbb2018-12-11 12:47:02 +010077 protected ClientResponse requestToken (MultivaluedMap<String, String> form)
margarethaf0085122018-08-16 16:19:53 +020078 throws KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +020079 return resource().path(API_VERSION).path("oauth2").path("token")
margarethaf0085122018-08-16 16:19:53 +020080 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
81 .header(HttpHeaders.CONTENT_TYPE,
82 ContentType.APPLICATION_FORM_URLENCODED)
83 .entity(form).post(ClientResponse.class);
84 }
85
86 // client credentials as form params
margarethac750cbb2018-12-11 12:47:02 +010087 protected ClientResponse requestTokenWithAuthorizationCodeAndForm (
margarethaf0085122018-08-16 16:19:53 +020088 String clientId, String clientSecret, String code)
89 throws KustvaktException {
90
91 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
92 form.add("grant_type", "authorization_code");
93 form.add("client_id", clientId);
94 form.add("client_secret", clientSecret);
95 form.add("code", code);
96
margarethaee0cbfe2018-08-28 17:47:14 +020097 return resource().path(API_VERSION).path("oauth2").path("token")
margarethaf0085122018-08-16 16:19:53 +020098 .header(HttpHeaders.CONTENT_TYPE,
99 ContentType.APPLICATION_FORM_URLENCODED)
100 .entity(form).post(ClientResponse.class);
101 }
102
103 // client credentials in authorization header
margarethac750cbb2018-12-11 12:47:02 +0100104 protected JsonNode requestTokenWithAuthorizationCodeAndHeader (String clientId,
margarethaf0085122018-08-16 16:19:53 +0200105 String code, String authHeader) throws KustvaktException {
106 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
107 form.add("grant_type", "authorization_code");
108 form.add("client_id", clientId);
109 form.add("code", code);
110
margaretha230effb2018-11-29 17:28:18 +0100111 ClientResponse response = resource().path(API_VERSION).path("oauth2")
112 .path("token").header(Attributes.AUTHORIZATION, authHeader)
margarethaf0085122018-08-16 16:19:53 +0200113 .header(HttpHeaders.CONTENT_TYPE,
114 ContentType.APPLICATION_FORM_URLENCODED)
115 .entity(form).post(ClientResponse.class);
116
117 String entity = response.getEntity(String.class);
118 return JsonUtils.readTree(entity);
119 }
120
margarethac750cbb2018-12-11 12:47:02 +0100121 protected ClientResponse requestTokenWithDoryPassword (String clientId,
margarethaf0085122018-08-16 16:19:53 +0200122 String clientSecret) throws KustvaktException {
margaretha230effb2018-11-29 17:28:18 +0100123 return requestTokenWithPassword(clientId, clientSecret, "dory",
124 "password");
125 }
126
margarethac750cbb2018-12-11 12:47:02 +0100127 protected ClientResponse requestTokenWithPassword (String clientId,
margaretha230effb2018-11-29 17:28:18 +0100128 String clientSecret, String username, String password)
129 throws KustvaktException {
margarethaf0085122018-08-16 16:19:53 +0200130 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
131 form.add("grant_type", "password");
132 form.add("client_id", clientId);
133 form.add("client_secret", clientSecret);
margaretha230effb2018-11-29 17:28:18 +0100134 form.add("username", username);
135 form.add("password", password);
margarethaf0085122018-08-16 16:19:53 +0200136
137 return requestToken(form);
138 }
margarethac750cbb2018-12-11 12:47:02 +0100139
140 protected void testRequestTokenWithRevokedRefreshToken (String clientId,
141 String clientSecret, String refreshToken) throws KustvaktException {
142 MultivaluedMap<String, String> form = new MultivaluedMapImpl();
143 form.add("grant_type", GrantType.REFRESH_TOKEN.toString());
144 form.add("client_id", clientId);
145 form.add("refresh_token", refreshToken);
146 if (clientSecret != null) {
147 form.add("client_secret", clientSecret);
148 }
margarethaf0085122018-08-16 16:19:53 +0200149
margarethac750cbb2018-12-11 12:47:02 +0100150 ClientResponse response =
151 resource().path(API_VERSION).path("oauth2").path("token")
152 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
153 .header(HttpHeaders.CONTENT_TYPE,
154 ContentType.APPLICATION_FORM_URLENCODED)
155 .entity(form).post(ClientResponse.class);
156
157 String entity = response.getEntity(String.class);
158 JsonNode node = JsonUtils.readTree(entity);
159 assertEquals(OAuth2Error.INVALID_GRANT, node.at("/error").asText());
160 assertEquals("Refresh token has been revoked",
161 node.at("/error_description").asText());
162 }
163
164 protected void updateClientPrivilege (MultivaluedMap<String, String> form)
margarethaf0085122018-08-16 16:19:53 +0200165 throws UniformInterfaceException, ClientHandlerException,
166 KustvaktException {
margaretha230effb2018-11-29 17:28:18 +0100167 ClientResponse response = resource().path(API_VERSION).path("oauth2")
168 .path("client").path("privilege")
margarethaf0085122018-08-16 16:19:53 +0200169 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
170 .createBasicAuthorizationHeaderValue("admin", "pass"))
171 .header(HttpHeaders.CONTENT_TYPE,
172 ContentType.APPLICATION_FORM_URLENCODED)
173 .entity(form).post(ClientResponse.class);
174
175 assertEquals(Status.OK.getStatusCode(), response.getStatus());
176 }
margaretha230effb2018-11-29 17:28:18 +0100177
margarethac750cbb2018-12-11 12:47:02 +0100178 protected ClientResponse searchWithAccessToken (String accessToken) {
margaretha230effb2018-11-29 17:28:18 +0100179 return resource().path(API_VERSION).path("search")
180 .queryParam("q", "Wasser").queryParam("ql", "poliqarp")
margarethaf0085122018-08-16 16:19:53 +0200181 .header(Attributes.AUTHORIZATION, "Bearer " + accessToken)
182 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
183 .get(ClientResponse.class);
184 }
185
186}