Advertisement
Guest User

hookldappam.groovy - Report Server 2.2.1-5602

a guest
Jan 27th, 2015
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 5.47 KB | None | 0 0
  1. package ldap
  2.  
  3. import javax.naming.AuthenticationException
  4. import javax.naming.Context
  5. import javax.naming.InvalidNameException
  6. import javax.naming.NamingException
  7. import javax.naming.directory.InitialDirContext
  8. import javax.persistence.NoResultException
  9.  
  10. import net.datenwerke.rs.authenticator.client.login.dto.UserPasswordAuthToken
  11. import net.datenwerke.rs.authenticator.client.login.pam.UserPasswordClientPAM
  12. import net.datenwerke.rs.utils.crypto.PasswordHasher;
  13. import net.datenwerke.security.client.login.AuthToken
  14. import net.datenwerke.security.service.authenticator.AuthenticationResult
  15. import net.datenwerke.security.service.authenticator.ReportServerPAM
  16. import net.datenwerke.security.service.authenticator.hooks.PAMHook
  17. import net.datenwerke.security.service.usermanager.UserManagerService
  18. import net.datenwerke.security.service.usermanager.entities.User
  19.  
  20. import com.google.inject.Inject
  21. import java.util.logging.Logger;
  22.  
  23. Logger logger = Logger.getLogger("LDAP");
  24. logger.severe("@@@@ LDAP INIT @@@@");
  25.  
  26. final LdapPAM ldapPam = GLOBALS.injector.getInstance(LdapPAM.class);
  27. GLOBALS.services.callbackRegistry.attachHook("LDAP_PAM", PAMHook.class, new PAMHook(){
  28.    
  29.     public void beforeStaticPamConfig(LinkedHashSet<ReportServerPAM> pams){
  30.         pams.add(ldapPam);
  31.     }
  32.     public void afterStaticPamConfig(LinkedHashSet<ReportServerPAM> pams){
  33.        
  34.     }
  35.    
  36. });
  37.  
  38.  
  39. public class LdapPAM implements ReportServerPAM {
  40.    
  41.     private static final String CLIENT_MODULE_NAME = UserPasswordClientPAM.class.getName();
  42.     private UserManagerService userManagerService;
  43.     private PasswordHasher passwordHasher;
  44.                  Logger logger = Logger.getLogger("LDAP");
  45.  
  46.     @Inject
  47.     public LdapPAM(UserManagerService userManagerService, PasswordHasher passwordHasher) {
  48.         this.userManagerService = userManagerService;
  49.         this.passwordHasher = passwordHasher;
  50.     }
  51.    
  52.    
  53.     public AuthenticationResult authenticate(AuthToken[] tokens) {
  54.         for(Object token : tokens){
  55.             if(token instanceof UserPasswordAuthToken){
  56.                 UserPasswordAuthToken credentials = (UserPasswordAuthToken) token;
  57.                 User u = authenticate(credentials.getUsername(), credentials.getPassword());
  58.                 if(null != u){
  59.                     logger.severe("####### LdapPAM: authenticate success (usr=" + u.getUsername() + ")")
  60.                     return new AuthenticationResult(true, u, true);
  61.                 }else{
  62.                     User usr = getUserOrNull(credentials.getUsername());
  63.                     boolean authoritive = (null == usr || (null != usr.getOrigin() && usr.getOrigin().toLowerCase().startsWith("ldap://")) || (null != usr.getPassword() && !usr.getPassword().isEmpty()));
  64.                     logger.severe("####### LdapPAM: authenticate failed (result=AuthenticationResult(false, " + u.getUsername() + ", "+authoritive+")")
  65.                     return new AuthenticationResult(false, usr, authoritive);
  66.                 }
  67.             }
  68.         }
  69.         logger.severe("####### LdapPAM: authenticate notoken (result=AuthenticationResult(false, null, false)")
  70.         return new AuthenticationResult(false, null, false);
  71.     }
  72.    
  73.    
  74.     protected User getUserOrNull(String username){
  75.         try{
  76.             return userManagerService.getUserByName(username);
  77.         }catch(NoResultException ex){
  78.             return null;
  79.         }
  80.     }
  81.    
  82.    
  83.     public User authenticate(String username, String cleartextPassword){
  84.         User user = getUserOrNull(username);
  85.         if(null == user)
  86.             return null;
  87.        
  88.         if(null != user.getPassword() && !user.getPassword().isEmpty() && passwordHasher.validatePassword(user.getPassword(), cleartextPassword)){
  89.             logger.severe("####### LdapPAM: authenticate with local password: success")
  90.             return user;
  91.         }else{
  92.             logger.severe("####### LdapPAM: authenticate with local password: fail")
  93.         }      
  94.            
  95.         LdapAuthenticator authenticator = new LdapAuthenticator(); 
  96.         if(authenticator.authenticate(user, cleartextPassword)){
  97.             logger.severe("####### LdapPAM: authenticate against directory server: success")
  98.             return user;
  99.         }else{
  100.             logger.severe("####### LdapPAM: authenticate against directory server: failed")
  101.             return null;
  102.         }
  103.     }
  104.    
  105.     public String getClientModuleName() {
  106.         return CLIENT_MODULE_NAME;
  107.     }
  108.  
  109. }
  110.  
  111.  
  112. public class LdapAuthenticator {
  113.    
  114.                  Logger logger = Logger.getLogger("LDAP");
  115.     public boolean authenticate(User user, String password){
  116.         if(null == user.getOrigin() || null == user.getGuid())
  117.             return false;
  118.        
  119.         Properties props = new Properties();
  120.  
  121.         props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  122.         props.setProperty(Context.PROVIDER_URL, getProvider(user));
  123.         props.setProperty(Context.URL_PKG_PREFIXES, "com.sun.jndi.url");
  124.         props.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
  125.  
  126.         props.setProperty(Context.SECURITY_PRINCIPAL, getPrincipal(user));
  127.         props.setProperty(Context.SECURITY_CREDENTIALS, password);
  128.        
  129.         try {
  130.             InitialDirContext ctx = new InitialDirContext(props);
  131.             ctx.getAttributes(getPrincipal(user));
  132.             return true;
  133.         } catch (AuthenticationException e) {
  134.                                                    logger.severe(e);
  135.             return false;
  136.         } catch (InvalidNameException e) {
  137.             throw new RuntimeException(e);
  138.         } catch (NamingException e) {
  139.             if(e.getMessage().contains("LdapErr: DSID-0C0906E8")){
  140.                 return false;
  141.             }
  142.            
  143.         }
  144.  
  145.     }
  146.  
  147.     private String getProvider(User user) {
  148.         String origin = user.getOrigin();
  149.         int i = origin.lastIndexOf("/");
  150.        
  151.         return origin.substring(0, i);
  152.     }
  153.  
  154.     private String getPrincipal(User user) {
  155.         String origin = user.getOrigin();
  156.            
  157.         int i = user.getOrigin().lastIndexOf("/");
  158.         return origin.substring(i + 1);
  159.     }
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement