backend/restservice/src/main/java/com/plannaplan/security/cas/DefaultUAMCasValidator.java

46 lines
1.6 KiB
Java
Raw Normal View History

package com.plannaplan.security.cas;
2020-09-11 13:15:22 +02:00
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DefaultUAMCasValidator {
2020-09-11 13:15:22 +02:00
private static String CAS_URL = "https://cas.amu.edu.pl/cas";
private final CloseableHttpClient httpClient = HttpClients.createDefault();
private String service;
private String ticket;
public DefaultUAMCasValidator(String service, String ticket) {
2020-09-11 13:15:22 +02:00
this.service = service;
this.ticket = ticket;
}
2020-09-25 17:01:51 +02:00
public String validate() throws Exception, CasValidationExcepiton {
HttpGet request = new HttpGet(DefaultUAMCasValidator.CAS_URL + "/validate?service="
2020-09-11 13:15:22 +02:00
+ URLEncoder.encode(this.service, "UTF-8") + "&ticket=" + URLEncoder.encode(this.ticket, "UTF-8"));
try (CloseableHttpResponse response = httpClient.execute(request)) {
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
// return it as a String
result = EntityUtils.toString(entity);
2020-09-25 17:01:51 +02:00
if (result.replace("\n", "").trim().equals("no")) {
2020-09-11 14:12:13 +02:00
throw new CasValidationExcepiton("Validation failed");
}
2020-09-11 13:15:22 +02:00
}
2020-09-11 14:12:13 +02:00
2020-09-11 13:15:22 +02:00
String res = result.substring(result.indexOf('\n') + 1);
return res;
}
}
}