buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/taskdefs/ValidateUserLoginTask.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15 *
    15 *
    16 */
    16 */
    17  
    17  
    18 package com.nokia.helium.core.ant.taskdefs;
    18 package com.nokia.helium.core.ant.taskdefs;
    19 
    19 
       
    20 import java.util.Hashtable;
       
    21 
       
    22 import javax.naming.Context;
       
    23 import javax.naming.NamingEnumeration;
       
    24 import javax.naming.NamingException;
       
    25 import javax.naming.directory.Attribute;
       
    26 import javax.naming.directory.Attributes;
       
    27 import javax.naming.directory.DirContext;
       
    28 import javax.naming.directory.InitialDirContext;
       
    29 import javax.naming.directory.SearchControls;
       
    30 import javax.naming.directory.SearchResult;
       
    31 
       
    32 import org.apache.tools.ant.BuildException;
       
    33 import org.apache.tools.ant.Project;
    20 import org.apache.tools.ant.Task;
    34 import org.apache.tools.ant.Task;
    21 import org.apache.tools.ant.Project;
       
    22 import org.apache.tools.ant.BuildException;
       
    23 import javax.naming.*;
       
    24 import javax.naming.directory.*;
       
    25 import java.util.Hashtable;
       
    26 import org.apache.tools.ant.taskdefs.condition.Condition;
    35 import org.apache.tools.ant.taskdefs.condition.Condition;
    27 
    36 
    28 /**
    37 /**
    29  * Task is to validate noe user with LDAP server.
    38  * Task is to validate noe user with LDAP server.
    30  * <pre>
    39  * <pre>
    59     private String password;
    68     private String password;
    60         
    69         
    61     public void execute()
    70     public void execute()
    62     {
    71     {
    63         
    72         
    64         if (property == null)
    73         if (property == null) {
    65             throw new BuildException("'property' attribute is not defined");
    74             throw new BuildException("'property' attribute is not defined");
       
    75         }
    66         validateParameters(url, rootdn, filter, key, searchdn, password);
    76         validateParameters(url, rootdn, filter, key, searchdn, password);
    67         log("Authenticating the user...");
    77         log("Authenticating the user...");
    68         if (authenticateUser(url, searchUser(url, rootdn, filter, key, searchdn))) {
    78         if (authenticateUser(url, searchUser(url, rootdn, filter, key, searchdn))) {
    69             getProject().setProperty(property, "true");
    79             getProject().setProperty(property, "true");
    70         }
    80         }
   111         return userSearchDN;
   121         return userSearchDN;
   112     }
   122     }
   113     
   123     
   114     public void validateParameters(String url, String rootdn, String filter, String key, String searchdn, String password) {
   124     public void validateParameters(String url, String rootdn, String filter, String key, String searchdn, String password) {
   115         
   125         
   116         if (url == null)
   126         if (url == null) {
   117             throw new BuildException("'url' attribute is not defined");
   127             throw new BuildException("'url' attribute is not defined");
   118         if (rootdn == null)
   128         }
       
   129         if (rootdn == null) {
   119             throw new BuildException("'rootdn' attribute is not defined");
   130             throw new BuildException("'rootdn' attribute is not defined");
   120         if (filter == null)
   131         }
       
   132         if (filter == null) {
   121             throw new BuildException("'filter' attribute is not defined");
   133             throw new BuildException("'filter' attribute is not defined");
   122         if (key == null)
   134         }
       
   135         if (key == null) {
   123             throw new BuildException("'key' attribute is not defined");
   136             throw new BuildException("'key' attribute is not defined");
   124         if (searchdn == null)
   137         }
       
   138         if (searchdn == null) {
   125             throw new BuildException("'searchdn' attribute is not defined");
   139             throw new BuildException("'searchdn' attribute is not defined");
   126         if (password == null)
   140         }
       
   141         if (password == null) {
   127             throw new BuildException("'password' attribute is not defined");
   142             throw new BuildException("'password' attribute is not defined");
       
   143         }
   128     }
   144     }
   129     
   145     
   130     public boolean authenticateUser(String ldapurl, String rooTdn) {
   146     public boolean authenticateUser(String ldapurl, String rooTdn) {
   131     
   147     
   132         Hashtable<String, String> env = new Hashtable<String, String>(11);
   148         Hashtable<String, String> env = new Hashtable<String, String>(11);
   134         env.put(Context.PROVIDER_URL, ldapurl);
   150         env.put(Context.PROVIDER_URL, ldapurl);
   135         env.put(Context.SECURITY_AUTHENTICATION, "simple");
   151         env.put(Context.SECURITY_AUTHENTICATION, "simple");
   136         env.put(Context.SECURITY_PRINCIPAL, rooTdn);
   152         env.put(Context.SECURITY_PRINCIPAL, rooTdn);
   137         env.put(Context.SECURITY_CREDENTIALS, password);
   153         env.put(Context.SECURITY_CREDENTIALS, password);
   138         try {
   154         try {
   139             DirContext authContext = new InitialDirContext(env);
   155             DirContext authContext = new InitialDirContext(env); //NOPMD
   140             return true;
   156             return true;
   141         } catch (NamingException e) {
   157         } catch (NamingException e) {
   142             // We are Ignoring the errors as no need to fail the build.
   158             // We are Ignoring the errors as no need to fail the build.
   143             log("Not able to validate the user. " + e.getMessage(), Project.MSG_DEBUG);
   159             log("Not able to validate the user. " + e.getMessage(), Project.MSG_DEBUG);
   144             return false;  
   160             return false;