Added CasValidator.java; Update DefaultUAMCasValidator

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-02 13:48:28 +01:00
parent cd96e74c29
commit 1e3cccc991
Signed by: y0rune
GPG Key ID: F204C385F57EB348
2 changed files with 29 additions and 16 deletions

View File

@ -0,0 +1,7 @@
package com.plannaplan.security.cas;
public interface CasValidator {
String validate();
}

View File

@ -9,7 +9,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DefaultUAMCasValidator {
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;
@ -20,26 +20,32 @@ public class DefaultUAMCasValidator {
this.ticket = ticket;
}
public String validate() throws Exception, CasValidationExcepiton {
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)) {
@Override
public String 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();
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 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 res;
}
String res = result.substring(result.indexOf('\n') + 1);
return res;
}
catch (Exception e) {
throw new CasValidationExcepiton("Cas Validation has failed.");
}
}
}