61 lines
2.1 KiB
Java
Executable File
61 lines
2.1 KiB
Java
Executable File
package com.plannaplan.security.cas;
|
|
|
|
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;
|
|
|
|
/**
|
|
* Cas Validator for localhost and not registered dopmains
|
|
*/
|
|
public class DefaultUAMCasValidator implements CasValidator {
|
|
private static String CAS_URL = "https://cas.amu.edu.pl/cas";
|
|
private final CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
private String service;
|
|
private String ticket;
|
|
|
|
/**
|
|
* craste new instance
|
|
*
|
|
* @param service string with url of our service from where request began
|
|
* @param ticket ticket returned after user provided creeds on cas page
|
|
*/
|
|
public DefaultUAMCasValidator(String service, String ticket) {
|
|
this.service = service;
|
|
this.ticket = ticket;
|
|
}
|
|
|
|
@Override
|
|
public CasUserIdentity validate() {
|
|
try {
|
|
HttpGet request = new HttpGet(DefaultUAMCasValidator.CAS_URL + "/validate?service="
|
|
+ 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);
|
|
if (result.replace("\n", "").trim().equals("no")) {
|
|
throw new CasValidationExcepiton("Validation failed");
|
|
}
|
|
}
|
|
|
|
String res = result.substring(result.indexOf('\n') + 1);
|
|
return new CasUserIdentity(null, res);
|
|
|
|
}
|
|
}
|
|
|
|
catch (Exception e) {
|
|
throw new CasValidationExcepiton("Cas Validation has failed.");
|
|
}
|
|
}
|
|
}
|