mirror of https://github.com/jacekkow/keycloak-protocol-cas

Jacek Kowalski
2023-11-24 8560e4a392962f6b1bdd40f444f091f6a36cdbd4
commit | author | age
57a6c1 1 package org.keycloak.protocol.cas.utils;
MP 2
fdb9f6 3 import jakarta.ws.rs.core.HttpHeaders;
57a6c1 4 import org.apache.http.HttpEntity;
MP 5 import org.apache.http.HttpResponse;
281a7e 6 import org.apache.http.NameValuePair;
57a6c1 7 import org.apache.http.client.HttpClient;
281a7e 8 import org.apache.http.client.entity.UrlEncodedFormEntity;
57a6c1 9 import org.apache.http.client.methods.HttpPost;
281a7e 10 import org.apache.http.message.BasicNameValuePair;
57a6c1 11 import org.keycloak.connections.httpclient.HttpClientProvider;
MP 12 import org.keycloak.models.KeycloakSession;
13
14 import java.io.IOException;
15 import java.io.InputStream;
1293d5 16 import java.text.SimpleDateFormat;
MP 17 import java.util.Date;
281a7e 18 import java.util.LinkedList;
MM 19 import java.util.List;
fdb9f6 20 import java.util.UUID;
57a6c1 21
MP 22 public class LogoutHelper {
23     //although it looks alike, the CAS SLO protocol has nothing to do with SAML; so we build the format
24     //required by the spec manually
25     private static final String TEMPLATE = "<samlp:LogoutRequest xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\" ID=\"$ID\" Version=\"2.0\" IssueInstant=\"$ISSUE_INSTANT\">\n" +
26             "  <saml:NameID xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">@NOT_USED@</saml:NameID>\n" +
27             "  <samlp:SessionIndex>$SESSION_IDENTIFIER</samlp:SessionIndex>\n" +
28             "</samlp:LogoutRequest>";
29
281a7e 30     public static HttpEntity buildSingleLogoutRequest(String serviceTicket) throws IOException {
1293d5 31         String id = "ID_" + UUID.randomUUID().toString();
MP 32         String issueInstant = new SimpleDateFormat("yyyy-MM-dd'T'H:mm:ss").format(new Date());
33         String document = TEMPLATE.replace("$ID", id).replace("$ISSUE_INSTANT", issueInstant)
57a6c1 34                 .replace("$SESSION_IDENTIFIER", serviceTicket);
281a7e 35         List<NameValuePair> parameters = new LinkedList<>();
MM 36         parameters.add(new BasicNameValuePair("logoutRequest", document));
37         return new UrlEncodedFormEntity(parameters);
57a6c1 38     }
MP 39
40     public static void postWithRedirect(KeycloakSession session, String url, HttpEntity postBody) throws IOException {
41         HttpClient httpClient = session.getProvider(HttpClientProvider.class).getHttpClient();
42         for (int i = 0; i < 2; i++) { // follow redirects once
43             HttpPost post = new HttpPost(url);
44             post.setEntity(postBody);
45             HttpResponse response = httpClient.execute(post);
46             try {
47                 int status = response.getStatusLine().getStatusCode();
48                 if (status == 302 && !url.endsWith("/")) {
49                     String redirect = response.getFirstHeader(HttpHeaders.LOCATION).getValue();
50                     String withSlash = url + "/";
51                     if (withSlash.equals(redirect)) {
52                         url = withSlash;
53                         continue;
54                     }
55                 }
56             } finally {
57                 HttpEntity entity = response.getEntity();
58                 if (entity != null) {
59                     InputStream is = entity.getContent();
60                     if (is != null)
61                         is.close();
62                 }
63
64             }
65             break;
66         }
67     }
68 }