AuthController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package fr.triplea.demovote.web.controller;
  2. import java.util.List;
  3. import java.util.Locale;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.MessageSource;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  10. import org.springframework.security.core.Authentication;
  11. import org.springframework.security.core.context.SecurityContextHolder;
  12. import org.springframework.security.core.userdetails.UserDetails;
  13. import org.springframework.security.crypto.password.PasswordEncoder;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.servlet.LocaleResolver;
  20. import fr.triplea.demovote.dao.ParticipantRepository;
  21. import fr.triplea.demovote.dto.JourneesTransfer;
  22. import fr.triplea.demovote.dto.RefreshTokenTransfer;
  23. import fr.triplea.demovote.dto.UserCredentials;
  24. import fr.triplea.demovote.model.Participant;
  25. import fr.triplea.demovote.model.RefreshToken;
  26. import fr.triplea.demovote.model.Role;
  27. import fr.triplea.demovote.security.MyUserDetailsService;
  28. import fr.triplea.demovote.security.jwt.JwtTokenUtil;
  29. import fr.triplea.demovote.security.jwt.RefreshTokenException;
  30. import fr.triplea.demovote.security.jwt.RefreshTokenService;
  31. import jakarta.servlet.http.HttpServletRequest;
  32. import jakarta.validation.Valid;
  33. @RestController
  34. @RequestMapping("/sign")
  35. public class AuthController
  36. {
  37. // TODO : liste des participants arrivés sur le formulaire de login pour aider le participant ?
  38. // TODO : captcha ?
  39. @SuppressWarnings("unused")
  40. private static final Logger LOG = LoggerFactory.getLogger(AuthController.class);
  41. @Autowired
  42. public PasswordEncoder passwordEncoder;
  43. @Autowired
  44. public MyUserDetailsService myUserDetailsService;
  45. @Autowired
  46. private JwtTokenUtil jwtTokenUtil;
  47. @Autowired
  48. RefreshTokenService refreshTokenService;
  49. @Autowired
  50. private ParticipantRepository participantRepository;
  51. @Autowired
  52. private LocaleResolver localeResolver;
  53. @Autowired
  54. private MessageSource messageSource;
  55. @GetMapping(value = "/hello")
  56. public ResponseEntity<JourneesTransfer> getDaysLabels()
  57. {
  58. JourneesTransfer jt = new JourneesTransfer();
  59. jt.setJour1Court("Hello");
  60. return ResponseEntity.ok(jt);
  61. }
  62. @PostMapping(value = "/in")
  63. public ResponseEntity<UserCredentials> signIn(@RequestBody UserCredentials uc, HttpServletRequest request)
  64. {
  65. Locale locale = localeResolver.resolveLocale(request);
  66. String usrn = uc.getUsername(); if (usrn == null) { usrn = ""; } else { usrn = usrn.trim(); }
  67. String pass = uc.getPassword(); if (pass == null) { pass = ""; } else { pass = pass.trim(); }
  68. if (usrn.isEmpty() || pass.isEmpty()) { return ResponseEntity.notFound().build(); }
  69. Participant found = participantRepository.findByPseudonyme(usrn);
  70. if (found != null)
  71. {
  72. UserDetails userDetails = myUserDetailsService.loadUserByUsername(usrn);
  73. Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()) ;
  74. if (passwordEncoder.matches(pass, userDetails.getPassword()))
  75. {
  76. // TODO : restreindre la connexion aux participants avec flag 'arrivés' à true ?
  77. SecurityContextHolder.getContext().setAuthentication(authentication);
  78. String token = jwtTokenUtil.generateJwtToken(authentication);
  79. refreshTokenService.deleteByNumeroParticipant(found.getNumeroParticipant());
  80. RefreshToken refreshToken = refreshTokenService.createRefreshToken(found.getNumeroParticipant());
  81. uc = new UserCredentials();
  82. uc.setNumeroParticipant(found.getNumeroParticipant());
  83. uc.setUsername(usrn);
  84. uc.setPassword("<success@auth>");
  85. uc.setNom(found.getNom());
  86. uc.setPrenom(found.getPrenom());
  87. uc.setDelaiAvantDeconnexion(found.getDelaiDeconnexion());
  88. uc.setAccessToken(token);
  89. uc.setRefreshToken(refreshToken.getToken());
  90. uc.setErreur("");
  91. List<Role> roles = found.getRoles();
  92. if (!(uc.hasRole())) { for (Role role : roles) { if (role.isRole("ADMIN")) { uc.setRole("ADMIN"); } } }
  93. if (!(uc.hasRole())) { for (Role role : roles) { if (role.isRole("ORGA")) { uc.setRole("ORGA"); } } }
  94. if (!(uc.hasRole())) { uc.setRole("USER"); }
  95. return ResponseEntity.ok(uc);
  96. }
  97. else
  98. {
  99. uc = new UserCredentials();
  100. uc.setNumeroParticipant(0);
  101. uc.setUsername("");
  102. uc.setPassword("");
  103. uc.setNom("");
  104. uc.setPrenom("");
  105. uc.setDelaiAvantDeconnexion(15);
  106. uc.setAccessToken("");
  107. uc.setRefreshToken("");
  108. uc.setRole("");
  109. uc.setErreur(messageSource.getMessage("auth.password.mismatches", null, locale));
  110. return ResponseEntity.ok(uc);
  111. }
  112. }
  113. uc = new UserCredentials();
  114. uc.setNumeroParticipant(0);
  115. uc.setUsername("");
  116. uc.setPassword("");
  117. uc.setNom("");
  118. uc.setPrenom("");
  119. uc.setDelaiAvantDeconnexion(15);
  120. uc.setAccessToken("");
  121. uc.setRefreshToken("");
  122. uc.setRole("");
  123. uc.setErreur(messageSource.getMessage("auth.user.notfound", null, locale));
  124. return ResponseEntity.ok(uc);
  125. }
  126. @PostMapping("/refresh")
  127. public ResponseEntity<?> refreshtoken(@Valid @RequestBody RefreshTokenTransfer rtt, HttpServletRequest request)
  128. {
  129. Locale locale = localeResolver.resolveLocale(request);
  130. String refreshTokenActif = rtt.getRefreshToken();
  131. RefreshToken found = refreshTokenService.findByToken(refreshTokenActif);
  132. if (found == null) { throw new RefreshTokenException(refreshTokenActif, messageSource.getMessage("refreshtoken.notfound", null, locale)); }
  133. found = refreshTokenService.verifyExpiration(found);
  134. if (found == null) { throw new RefreshTokenException(refreshTokenActif, messageSource.getMessage("refreshtoken.expired", null, locale)); }
  135. Participant participant = found.getParticipant();
  136. rtt.setAccessToken(jwtTokenUtil.generateTokenFromPseudonyme(participant.getPseudonyme()));
  137. return ResponseEntity.ok(rtt);
  138. }
  139. @PostMapping("/out")
  140. public ResponseEntity<UserCredentials> signOut(final Authentication authentication)
  141. {
  142. if (authentication != null)
  143. {
  144. Participant found = participantRepository.findByPseudonyme(authentication.getName());
  145. if (found != null) { refreshTokenService.deleteByNumeroParticipant(found.getNumeroParticipant()); }
  146. }
  147. SecurityContextHolder.clearContext();
  148. UserCredentials uc = new UserCredentials();
  149. uc.setNumeroParticipant(0);
  150. uc.setUsername("");
  151. uc.setPassword("");
  152. uc.setNom("");
  153. uc.setPrenom("");
  154. uc.setDelaiAvantDeconnexion(15);
  155. uc.setAccessToken("");
  156. uc.setRefreshToken("");
  157. uc.setRole("");
  158. return ResponseEntity.ok(uc);
  159. }
  160. }