Added CasValidator
This commit is contained in:
parent
824c137141
commit
3deebe9248
@ -31,6 +31,12 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.10</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.plannaplan.security;
|
||||
|
||||
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 CasValidator {
|
||||
private static String CAS_URL = "https://cas.amu.edu.pl/cas";
|
||||
private final CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
private String service;
|
||||
private String ticket;
|
||||
|
||||
public CasValidator(String service, String ticket) {
|
||||
this.service = service;
|
||||
this.ticket = ticket;
|
||||
}
|
||||
|
||||
public String validate() throws Exception {
|
||||
HttpGet request = new HttpGet(CasValidator.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);
|
||||
|
||||
}
|
||||
String res = result.substring(result.indexOf('\n') + 1);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.plannaplan.security;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CasValidatorTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void shouldValidateTicket() {
|
||||
//you need to privide fresh ticket to make this test pass that's why it is marked as ignored
|
||||
CasValidator validator = new CasValidator("http://localhost:3000",
|
||||
"ST-572267-cbgKrcJLd0tdCubeLqdW-cas.amu.edu.pl");
|
||||
try {
|
||||
System.out.println(validator.validate());
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user