| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import java.net.URI; |
| 6 | |
| 7 | import javax.ws.rs.core.MultivaluedMap; |
| 8 | import javax.ws.rs.core.Response.Status; |
| 9 | |
| 10 | import org.apache.http.entity.ContentType; |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 11 | import org.apache.oltu.oauth2.common.message.types.GrantType; |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 12 | import org.springframework.util.MultiValueMap; |
| 13 | import org.springframework.web.util.UriComponentsBuilder; |
| 14 | |
| 15 | import com.fasterxml.jackson.databind.JsonNode; |
| 16 | import com.google.common.net.HttpHeaders; |
| 17 | import com.sun.jersey.api.client.ClientHandlerException; |
| 18 | import com.sun.jersey.api.client.ClientResponse; |
| 19 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 20 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 21 | |
| 22 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 23 | import de.ids_mannheim.korap.config.Attributes; |
| 24 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 25 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 26 | import de.ids_mannheim.korap.oauth2.constant.OAuth2Error; |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 27 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 28 | |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 29 | /** |
| 30 | * Provides common methods and variables for OAuth2 tests, |
| 31 | * and does not run any test. |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 32 | * |
| 33 | * @author margaretha |
| 34 | * |
| 35 | */ |
| margaretha | f370f54 | 2018-08-23 18:51:49 +0200 | [diff] [blame] | 36 | public abstract class OAuth2TestBase extends SpringJerseyTest { |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 37 | |
| 38 | protected String publicClientId = "8bIDtZnH6NvRkW2Fq"; |
| 39 | protected String confidentialClientId = "9aHsGW6QflV13ixNpez"; |
| 40 | protected String superClientId = "fCBbQkAyYzI4NzUxMg"; |
| 41 | protected String clientSecret = "secret"; |
| 42 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 43 | protected ClientResponse requestAuthorizationCode ( |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 44 | MultivaluedMap<String, String> form, String authHeader) |
| 45 | throws KustvaktException { |
| 46 | |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 47 | return resource().path(API_VERSION).path("oauth2").path("authorize") |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 48 | .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 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 55 | protected String requestAuthorizationCode (String clientId, |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 56 | 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 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 77 | protected ClientResponse requestToken (MultivaluedMap<String, String> form) |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 78 | throws KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 79 | return resource().path(API_VERSION).path("oauth2").path("token") |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 80 | .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 |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 87 | protected ClientResponse requestTokenWithAuthorizationCodeAndForm ( |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 88 | 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 | |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 97 | return resource().path(API_VERSION).path("oauth2").path("token") |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 98 | .header(HttpHeaders.CONTENT_TYPE, |
| 99 | ContentType.APPLICATION_FORM_URLENCODED) |
| 100 | .entity(form).post(ClientResponse.class); |
| 101 | } |
| 102 | |
| 103 | // client credentials in authorization header |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 104 | protected JsonNode requestTokenWithAuthorizationCodeAndHeader (String clientId, |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 105 | 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 | |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 111 | ClientResponse response = resource().path(API_VERSION).path("oauth2") |
| 112 | .path("token").header(Attributes.AUTHORIZATION, authHeader) |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 113 | .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 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 121 | protected ClientResponse requestTokenWithDoryPassword (String clientId, |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 122 | String clientSecret) throws KustvaktException { |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 123 | return requestTokenWithPassword(clientId, clientSecret, "dory", |
| 124 | "password"); |
| 125 | } |
| 126 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 127 | protected ClientResponse requestTokenWithPassword (String clientId, |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 128 | String clientSecret, String username, String password) |
| 129 | throws KustvaktException { |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 130 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 131 | form.add("grant_type", "password"); |
| 132 | form.add("client_id", clientId); |
| 133 | form.add("client_secret", clientSecret); |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 134 | form.add("username", username); |
| 135 | form.add("password", password); |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 136 | |
| 137 | return requestToken(form); |
| 138 | } |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 139 | |
| 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 | } |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 149 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 150 | 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) |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 165 | throws UniformInterfaceException, ClientHandlerException, |
| 166 | KustvaktException { |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 167 | ClientResponse response = resource().path(API_VERSION).path("oauth2") |
| 168 | .path("client").path("privilege") |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 169 | .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 | } |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 177 | |
| margaretha | c750cbb | 2018-12-11 12:47:02 +0100 | [diff] [blame] | 178 | protected ClientResponse searchWithAccessToken (String accessToken) { |
| margaretha | 230effb | 2018-11-29 17:28:18 +0100 | [diff] [blame] | 179 | return resource().path(API_VERSION).path("search") |
| 180 | .queryParam("q", "Wasser").queryParam("ql", "poliqarp") |
| margaretha | f008512 | 2018-08-16 16:19:53 +0200 | [diff] [blame] | 181 | .header(Attributes.AUTHORIZATION, "Bearer " + accessToken) |
| 182 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 183 | .get(ClientResponse.class); |
| 184 | } |
| 185 | |
| 186 | } |