Cas update

This commit is contained in:
Filip Izydorczyk 2020-09-11 14:12:13 +02:00
parent 3deebe9248
commit c9f50c5b16
4 changed files with 59 additions and 3 deletions

View File

@ -0,0 +1,21 @@
package com.plannaplan.controllers;
import com.plannaplan.security.CasValidator;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class TokenController {
public static String SERVICE_URL = "http://localhost:3000";
// @GetMapping("/token")
// public ResponseEntity<String> getToken(@RequestParam("ticket") final String ticket) {
// CasValidator validator = new CasValidator(SERVICE_URL,ticket);
// }
}

View File

@ -0,0 +1,13 @@
package com.plannaplan.security;
public class CasValidationExcepiton extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public CasValidationExcepiton(String s) {
super(s);
}
}

View File

@ -20,7 +20,7 @@ public class CasValidator {
this.ticket = ticket;
}
public String validate() throws Exception {
public String validate() throws Exception, CasValidationExcepiton{
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)) {
@ -31,8 +31,11 @@ public class CasValidator {
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;

View File

@ -1,5 +1,7 @@
package com.plannaplan.security;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Ignore;
import org.junit.Test;
@ -14,8 +16,25 @@ public class CasValidatorTest {
try {
System.out.println(validator.validate());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
assertTrue(false);
}
}
@Test
public void shouldNotValidateTicket() {
//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",
"notticket");
try {
assertTrue(validator.validate().trim().equals(""));
}
catch (CasValidationExcepiton e){
assertTrue(true);
}
catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}
}