Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package project
  2.  
  3. import grails.transaction.Transactional
  4. import org.springframework.security.authentication.BadCredentialsException
  5. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
  6. import org.springframework.security.authentication.dao.DaoAuthenticationProvider
  7. import org.springframework.security.core.AuthenticationException
  8. import org.springframework.security.core.userdetails.UserDetails
  9.  
  10. class DaoLdapAuthenticationProvider extends DaoAuthenticationProvider {
  11.  
  12. boolean allowLdapFakeLogin = false
  13. LdapConnectionService ldapConnectionService
  14.  
  15. protected void additionalAuthenticationChecks(UserDetails userDetails,
  16. UsernamePasswordAuthenticationToken authentication)
  17. throws AuthenticationException {
  18. Object salt = null;
  19.  
  20. if (this.saltSource != null) {
  21. salt = this.saltSource.getSalt(userDetails);
  22. }
  23.  
  24. if (authentication.getCredentials() == null) {
  25. logger.debug("Authentication failed: no credentials provided");
  26.  
  27. throw new BadCredentialsException(messages.getMessage(
  28. "AbstractUserDetailsAuthenticationProvider.badCredentials",
  29. "Bad credentials"));
  30. }
  31.  
  32. String presentedPassword = authentication.getCredentials().toString();
  33.  
  34. boolean ldapAuthed = false
  35.  
  36. ldapAuthed = ldapConnectionService.auth(userDetails.username, presentedPassword)
  37. if (ldapAuthed) {
  38. updateUserPassword(userDetails.username, presentedPassword)
  39. }
  40.  
  41. if (!ldapAuthed) {
  42. logger.error("Ldap Authentication failed for ${userDetails.username}");
  43.  
  44. throw new BadCredentialsException(messages.getMessage(
  45. "AbstractUserDetailsAuthenticationProvider.badCredentials",
  46. "Bad credentials"));
  47. }
  48. logger.info("User ${userDetails.username} logged in");
  49. }
  50.  
  51.  
  52. boolean authLdapAndUpdateUserPassword(String username, String password) {
  53. // logger.debug("authLdapAndCreateUser $username")
  54.  
  55. // boolean authed = authLdap(username, password)
  56. boolean authed = ldapConnectionService.auth(username, password)
  57.  
  58.  
  59. if (authed) {
  60. //update the password in the user
  61. updateUserPassword(username, password)
  62. // createUserFromLdap(username, password)
  63. }
  64. return authed
  65. }
  66.  
  67. @Transactional
  68. def updateUserPassword(String username, String password) {
  69. // logger.debug("updateUserPassword on ldap user $username")
  70. def user = User.findByUsername(username)
  71. if (!user) {
  72. logger.error("User not found $username ")
  73. return
  74. }
  75. user.password = password
  76. user.enabled = true
  77. user.save(failOnError: true, flush: true)
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement