Added cas module + add customuamcasvalidator + test passed
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
		| @@ -57,6 +57,13 @@ | |||||||
|       <scope>test</scope> |       <scope>test</scope> | ||||||
|     </dependency> |     </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> |     <dependency> | ||||||
|       <groupId>org.springframework.boot</groupId> |       <groupId>org.springframework.boot</groupId> | ||||||
|       <artifactId>spring-boot-starter-security</artifactId> |       <artifactId>spring-boot-starter-security</artifactId> | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| package com.plannaplan.controllers; | package com.plannaplan.controllers; | ||||||
|  |  | ||||||
| import com.plannaplan.exceptions.UserNotFoundException; | import com.plannaplan.exceptions.UserNotFoundException; | ||||||
|  | import com.plannaplan.security.cas.CasUserIdentity; | ||||||
| import com.plannaplan.security.cas.CasValidationExcepiton; | import com.plannaplan.security.cas.CasValidationExcepiton; | ||||||
| import com.plannaplan.security.cas.DefaultUAMCasValidator; | import com.plannaplan.security.cas.DefaultUAMCasValidator; | ||||||
| import com.plannaplan.services.UserService; | import com.plannaplan.services.UserService; | ||||||
| @@ -34,7 +35,8 @@ public class TokenController { | |||||||
|         final DefaultUAMCasValidator validator = new DefaultUAMCasValidator(SERVICE_URL, ticket); |         final DefaultUAMCasValidator validator = new DefaultUAMCasValidator(SERVICE_URL, ticket); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             String authority = validator.validate(); |             CasUserIdentity casUserIdentity = validator.validate(); | ||||||
|  |             String authority = casUserIdentity.getEmail();  | ||||||
|             String token = this.userService.login(authority); |             String token = this.userService.login(authority); | ||||||
|             return new ResponseEntity<>(token, HttpStatus.OK); |             return new ResponseEntity<>(token, HttpStatus.OK); | ||||||
|         } catch (CasValidationExcepiton e) { |         } catch (CasValidationExcepiton e) { | ||||||
|   | |||||||
| @@ -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; | ||||||
|  |     }  | ||||||
|  | } | ||||||
| @@ -2,6 +2,5 @@ package com.plannaplan.security.cas; | |||||||
|  |  | ||||||
| public interface CasValidator { | public interface CasValidator { | ||||||
|   |   | ||||||
|     String validate(); |     CasUserIdentity validate(); | ||||||
|      |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -17,23 +17,25 @@ public class CustomUAMCasValidator implements CasValidator { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public String validate() { |     public CasUserIdentity validate() { | ||||||
|         /*  |         /*  | ||||||
|         * TO DO  |         * TO DO  | ||||||
|         * Dodać case z CAS10/CAS20/CAS30 |         * Dodać case z CAS10/CAS20/CAS30 | ||||||
|         */ |         */ | ||||||
|         Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL); |         final Cas20ServiceTicketValidator validator = new Cas20ServiceTicketValidator(CustomUAMCasValidator.CAS_URL); | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             Assertion assertion = validator.validate(this.ticket, this.service); |             final Assertion assertion = validator.validate(this.ticket, this.service); | ||||||
|  |              | ||||||
|             if (assertion == null) { |             if (assertion == null) { | ||||||
|                 throw new CasValidationExcepiton("Validation failed. Assertion could not be retrieved for ticket " + ""); |                 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); |             final String usosid = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.USOS_ID).toString(); | ||||||
|             System.out.println(mail); |              | ||||||
|  |             final String mail = assertion.getPrincipal().getAttributes().get(CustomUAMCasValidator.EMAIL_FIELD).toString(); | ||||||
|  |  | ||||||
|  |             return new CasUserIdentity(usosid,mail); | ||||||
|  |  | ||||||
|         } catch (TicketValidationException e) { |         } catch (TicketValidationException e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|   | |||||||
| @@ -21,7 +21,7 @@ public class DefaultUAMCasValidator implements CasValidator { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public String validate() { |     public CasUserIdentity validate() { | ||||||
|         try { |         try { | ||||||
|             HttpGet request = new HttpGet(DefaultUAMCasValidator.CAS_URL + "/validate?service=" |             HttpGet request = new HttpGet(DefaultUAMCasValidator.CAS_URL + "/validate?service=" | ||||||
|                     + URLEncoder.encode(this.service, "UTF-8") + "&ticket=" + URLEncoder.encode(this.ticket, "UTF-8")); |                     + 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); |                 String res = result.substring(result.indexOf('\n') + 1); | ||||||
|                 return res; |                 return new CasUserIdentity(null,res); | ||||||
|  |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ public class DefaultUAMCasValidatorTest { | |||||||
|     public void shouldNotValidateTicket() { |     public void shouldNotValidateTicket() { | ||||||
|         final DefaultUAMCasValidator validator = new DefaultUAMCasValidator("http://localhost:3000", "notticket"); |         final DefaultUAMCasValidator validator = new DefaultUAMCasValidator("http://localhost:3000", "notticket"); | ||||||
|         try { |         try { | ||||||
|             assertTrue(validator.validate().trim().equals("")); |             assertTrue(validator.validate().getEmail().trim().equals("")); | ||||||
|         } catch (CasValidationExcepiton e) { |         } catch (CasValidationExcepiton e) { | ||||||
|             assertTrue(true); |             assertTrue(true); | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user