hopefully fix test
This commit is contained in:
parent
155d7fddd7
commit
83bb2ce06b
1 changed files with 50 additions and 36 deletions
|
@ -16,17 +16,9 @@
|
||||||
*/
|
*/
|
||||||
package org.keycloak.testsuite.adapter.servlet;
|
package org.keycloak.testsuite.adapter.servlet;
|
||||||
|
|
||||||
import org.apache.http.NameValuePair;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.keycloak.KeycloakSecurityContext;
|
import org.keycloak.KeycloakSecurityContext;
|
||||||
import org.keycloak.OAuth2Constants;
|
import org.keycloak.OAuth2Constants;
|
||||||
import org.keycloak.common.util.Base64Url;
|
|
||||||
import org.keycloak.common.util.KeycloakUriBuilder;
|
import org.keycloak.common.util.KeycloakUriBuilder;
|
||||||
import org.keycloak.representations.AccessToken;
|
import org.keycloak.representations.AccessToken;
|
||||||
import org.keycloak.representations.AccessTokenResponse;
|
import org.keycloak.representations.AccessTokenResponse;
|
||||||
|
@ -38,15 +30,18 @@ import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.net.HttpURLConnection;
|
||||||
import java.security.MessageDigest;
|
import java.net.URL;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.net.URLEncoder;
|
||||||
import java.util.LinkedList;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
|
||||||
|
@ -54,42 +49,61 @@ import java.util.UUID;
|
||||||
*/
|
*/
|
||||||
@WebServlet("/client-linking")
|
@WebServlet("/client-linking")
|
||||||
public class LinkAndExchangeServlet extends HttpServlet {
|
public class LinkAndExchangeServlet extends HttpServlet {
|
||||||
|
|
||||||
|
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
boolean first = true;
|
||||||
|
for(Map.Entry<String, String> entry : params.entrySet()){
|
||||||
|
if (first)
|
||||||
|
first = false;
|
||||||
|
else
|
||||||
|
result.append("&");
|
||||||
|
|
||||||
|
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
|
||||||
|
result.append("=");
|
||||||
|
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public AccessTokenResponse doTokenExchange(String realm, String token, String requestedIssuer,
|
public AccessTokenResponse doTokenExchange(String realm, String token, String requestedIssuer,
|
||||||
String clientId, String clientSecret) throws Exception {
|
String clientId, String clientSecret) throws Exception {
|
||||||
CloseableHttpClient client = new DefaultHttpClient();
|
|
||||||
try {
|
try {
|
||||||
String exchangeUrl = KeycloakUriBuilder.fromUri(ServletTestUtils.getAuthServerUrlBase())
|
String exchangeUrl = KeycloakUriBuilder.fromUri(ServletTestUtils.getAuthServerUrlBase())
|
||||||
.path("/auth/realms/{realm}/protocol/openid-connect/token").build(realm).toString();
|
.path("/auth/realms/{realm}/protocol/openid-connect/token").build(realm).toString();
|
||||||
|
|
||||||
HttpPost post = new HttpPost(exchangeUrl);
|
URL url = new URL(exchangeUrl);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
List<NameValuePair> parameters = new LinkedList<NameValuePair>();
|
conn.setRequestMethod("POST");
|
||||||
parameters.add(new BasicNameValuePair(OAuth2Constants.GRANT_TYPE, OAuth2Constants.TOKEN_EXCHANGE_GRANT_TYPE));
|
conn.setDoInput(true);
|
||||||
parameters.add(new BasicNameValuePair(OAuth2Constants.SUBJECT_TOKEN, token));
|
conn.setDoOutput(true);
|
||||||
parameters.add(new BasicNameValuePair(OAuth2Constants.SUBJECT_TOKEN_TYPE, OAuth2Constants.ACCESS_TOKEN_TYPE));
|
HashMap<String, String> parameters = new HashMap<>();
|
||||||
parameters.add(new BasicNameValuePair(OAuth2Constants.REQUESTED_ISSUER, requestedIssuer));
|
|
||||||
|
|
||||||
if (clientSecret != null) {
|
if (clientSecret != null) {
|
||||||
String authorization = BasicAuthHelper.createHeader(clientId, clientSecret);
|
String authorization = BasicAuthHelper.createHeader(clientId, clientSecret);
|
||||||
post.setHeader("Authorization", authorization);
|
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, authorization);
|
||||||
} else {
|
} else {
|
||||||
parameters.add(new BasicNameValuePair("client_id", clientId));
|
parameters.put("client_id", clientId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UrlEncodedFormEntity formEntity;
|
parameters.put(OAuth2Constants.GRANT_TYPE, OAuth2Constants.TOKEN_EXCHANGE_GRANT_TYPE);
|
||||||
try {
|
parameters.put(OAuth2Constants.SUBJECT_TOKEN, token);
|
||||||
formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
|
parameters.put(OAuth2Constants.SUBJECT_TOKEN_TYPE, OAuth2Constants.ACCESS_TOKEN_TYPE);
|
||||||
} catch (UnsupportedEncodingException e) {
|
parameters.put(OAuth2Constants.REQUESTED_ISSUER, requestedIssuer);
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
OutputStream os = conn.getOutputStream();
|
||||||
post.setEntity(formEntity);
|
BufferedWriter writer = new BufferedWriter(
|
||||||
CloseableHttpResponse response = client.execute(post);
|
new OutputStreamWriter(os, "UTF-8"));
|
||||||
AccessTokenResponse tokenResponse = JsonSerialization.readValue(response.getEntity().getContent(), AccessTokenResponse.class);
|
writer.write(getPostDataString(parameters));
|
||||||
response.close();
|
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
os.close();
|
||||||
|
AccessTokenResponse tokenResponse = JsonSerialization.readValue(conn.getInputStream(), AccessTokenResponse.class);
|
||||||
|
conn.getInputStream().close();
|
||||||
return tokenResponse;
|
return tokenResponse;
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue