Added cas module + add customuamcasvalidator + test passed

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-03 15:11:23 +01:00
parent 6298bb4765
commit b6c2e43975
Signed by: y0rune
GPG Key ID: F204C385F57EB348
7 changed files with 42 additions and 13 deletions

View File

@ -57,6 +57,13 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jasig.cas.client/cas-client-core -->
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

View File

@ -1,6 +1,7 @@
package com.plannaplan.controllers;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.security.cas.CasUserIdentity;
import com.plannaplan.security.cas.CasValidationExcepiton;
import com.plannaplan.security.cas.DefaultUAMCasValidator;
import com.plannaplan.services.UserService;
@ -34,7 +35,8 @@ public class TokenController {
final DefaultUAMCasValidator validator = new DefaultUAMCasValidator(SERVICE_URL, ticket);
try {
String authority = validator.validate();
CasUserIdentity casUserIdentity = validator.validate();
String authority = casUserIdentity.getEmail();
String token = this.userService.login(authority);
return new ResponseEntity<>(token, HttpStatus.OK);
} catch (CasValidationExcepiton e) {

View File

@ -0,0 +1,19 @@
package com.plannaplan.security.cas;
public class CasUserIdentity {
private String usosId;
private String email;
public CasUserIdentity(String usosId, String email){
this.usosId = usosId;
this.email = email;
}
public String getUsosId() {
return usosId;
}
public String getEmail() {
return email;
}
}

View File

@ -2,6 +2,5 @@ package com.plannaplan.security.cas;
public interface CasValidator {
String validate();
CasUserIdentity validate();
}

View File

@ -17,23 +17,25 @@ public class CustomUAMCasValidator implements CasValidator {
}
@Override
public String validate() {
public CasUserIdentity validate() {
/*
* TO DO
* Dodać case z CAS10/CAS20/CAS30
*/
Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL);
final Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL);
try {
Assertion assertion = validator.validate(this.ticket, this.service);
final Assertion assertion = validator.validate(this.ticket, this.service);
if (assertion == null) {
throw new CasValidationExcepiton("Validation failed. Assertion could not be retrieved for ticket " + "");
}
String usosid = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.USOS_ID).toString();
String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD).toString();
System.out.println(usosid);
System.out.println(mail);
final String usosid = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.USOS_ID).toString();
final String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD).toString();
return new CasUserIdentity(usosid,mail);
} catch (TicketValidationException e) {
e.printStackTrace();

View File

@ -21,7 +21,7 @@ public class DefaultUAMCasValidator implements CasValidator {
}
@Override
public String validate() {
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"));
@ -39,7 +39,7 @@ public class DefaultUAMCasValidator implements CasValidator {
}
String res = result.substring(result.indexOf('\n') + 1);
return res;
return new CasUserIdentity(null,res);
}
}

View File

@ -26,7 +26,7 @@ public class DefaultUAMCasValidatorTest {
public void shouldNotValidateTicket() {
final DefaultUAMCasValidator validator = new DefaultUAMCasValidator("http://localhost:3000", "notticket");
try {
assertTrue(validator.validate().trim().equals(""));
assertTrue(validator.validate().getEmail().trim().equals(""));
} catch (CasValidationExcepiton e) {
assertTrue(true);
} catch (Exception e) {