Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
21   99   10   3
4   58   0.48   7
7     1.43  
1    
 
 
  ADIntegration       Line # 22 21 10 0% 0.0
 
No Tests
 
1    /**
2    * Originally contributed by eMation (www.emation.pt)
3    */
4    package org.itracker.services.authentication.adsson;
5   
6    import java.io.IOException;
7    import java.io.InputStream;
8    import java.security.AccessControlException;
9    import java.util.Properties;
10   
11    import javax.security.auth.Subject;
12    import javax.security.auth.login.LoginContext;
13    import javax.security.auth.login.LoginException;
14   
15    import org.apache.log4j.Logger;
16   
17    /**
18    * Performs a kerberos authenticated search in AD
19    *
20    * @author ricardo
21    */
 
22    public class ADIntegration {
23   
24    private static final String AD_AUTH_PROPERTIES_FILE = "adauth.properties";
25    private static final String PASSWORD = "password";
26    private static final String USERNAME = "username";
27    private static final String BASE_BRANCH = "basebranch";
28    private static final String PROVIDER_URL = "url";
29   
30    private final Logger logger;
31    private LoginContext lc = null;
32    private Properties adAuth;
33   
 
34  0 toggle public ADIntegration() throws IOException {
35  0 this.logger = Logger.getLogger(getClass());
36  0 adAuth = new Properties();
37  0 InputStream is = getClass().getResourceAsStream( "/" + AD_AUTH_PROPERTIES_FILE);
38  0 if( is == null) {
39  0 String message = "Can't find " + AD_AUTH_PROPERTIES_FILE + " to get A.D. auth properties. This file should be in the root of your classpath or EAR file";
40  0 logger.error( message );
41  0 throw new IOException( message );
42    }
43  0 adAuth.load( is );
44    }
45   
 
46  0 toggle public void login() throws LoginException {
47  0 try {
48    // 1. Log in (to Kerberos)
49    // The login context should be configured in login-config.xml
50  0 lc = new LoginContext("Helpdesk", new SimpleCallbackHandler( getUsername(), getPassword() ) );
51    // Attempt authentication
52    // You might want to do this in a "for" loop to give
53    // user more than one chance to enter correct username/password
54  0 lc.login();
55    } catch (IOException e) {
56  0 throw new LoginException( e.getMessage() );
57    }
58    }
59   
 
60  0 toggle public Object getUserInfo(String login) throws AccessControlException {
61    // 2. Perform JNDI work as logged in subject
62  0 Object userInfo = Subject.doAs(lc.getSubject(), new GetUserModelFromADPrivilegedAction( login, getBaseBranch(), getProviderUrl() ));
63   
64  0 if( userInfo == null ) {
65  0 logger.error("Can't get info on " + login + " from A.D.");
66  0 throw new AccessControlException("Can't get info on " + login + " from A.D.");
67    }
68   
69  0 return( userInfo );
70    }
71   
72    /**
73    * @return
74    */
 
75  0 toggle private String getProviderUrl() {
76  0 return( adAuth.getProperty( PROVIDER_URL ) );
77    }
78   
79    /**
80    * @return
81    */
 
82  0 toggle private String getPassword() throws IOException {
83  0 return( adAuth.getProperty( PASSWORD ) );
84    }
85   
86    /**
87    * @return
88    */
 
89  0 toggle private String getUsername() throws IOException {
90  0 return( adAuth.getProperty( USERNAME ) );
91    }
92   
93    /**
94    * @return
95    */
 
96  0 toggle private String getBaseBranch() {
97  0 return( adAuth.getProperty( BASE_BRANCH ) );
98    }
99    }