View Javadoc

1   // ========================================================================
2   // Copyright (c) 1999-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.jaas.spi;
15  
16  import java.sql.Connection;
17  import java.sql.PreparedStatement;
18  import java.sql.ResultSet;
19  import java.sql.SQLException;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.security.auth.Subject;
25  import javax.security.auth.callback.CallbackHandler;
26  
27  import org.eclipse.jetty.http.security.Credential;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  /**
32   * AbstractDatabaseLoginModule
33   *
34   * Abstract base class for LoginModules that interact with a 
35   * database to retrieve authentication and authorization information.
36   * Used by the JDBCLoginModule and DataSourceLoginModule.
37   *
38   */
39  public abstract class AbstractDatabaseLoginModule extends AbstractLoginModule
40  {
41      private static final Logger LOG = Log.getLogger(AbstractDatabaseLoginModule.class);
42  
43      private String userQuery;
44      private String rolesQuery;
45      private String dbUserTable;
46      private String dbUserTableUserField;
47      private String dbUserTableCredentialField;
48      private String dbUserRoleTable;
49      private String dbUserRoleTableUserField;
50      private String dbUserRoleTableRoleField;
51      
52      
53      
54      
55      /**
56       * @return a java.sql.Connection from the database
57       * @throws Exception
58       */
59      public abstract Connection getConnection () throws Exception;
60      
61     
62      
63      /* ------------------------------------------------ */
64      /** Load info from database
65       * @param userName user info to load
66       * @exception SQLException 
67       */
68      public UserInfo getUserInfo (String userName)
69          throws Exception
70      {
71          Connection connection = null;
72          
73          try
74          {
75              connection = getConnection();
76              
77              //query for credential
78              PreparedStatement statement = connection.prepareStatement (userQuery);
79              statement.setString (1, userName);
80              ResultSet results = statement.executeQuery();
81              String dbCredential = null;
82              if (results.next())
83              {
84                  dbCredential = results.getString(1);
85              }
86              results.close();
87              statement.close();
88              
89              //query for role names
90              statement = connection.prepareStatement (rolesQuery);
91              statement.setString (1, userName);
92              results = statement.executeQuery();
93              List<String> roles = new ArrayList<String>();
94              
95              while (results.next())
96              {
97                  String roleName = results.getString (1);
98                  roles.add (roleName);
99              }
100             
101             results.close();
102             statement.close();
103             
104             return dbCredential==null ? null : new UserInfo (userName, 
105                     Credential.getCredential(dbCredential), roles);
106         }
107         finally
108         {
109             if (connection != null) connection.close();
110         }
111     }
112     
113 
114     public void initialize(Subject subject,
115             CallbackHandler callbackHandler,
116             Map<String,?> sharedState,
117             Map<String,?> options)
118     {
119         super.initialize(subject, callbackHandler, sharedState, options);
120         
121         //get the user credential query out of the options
122         dbUserTable = (String)options.get("userTable");
123         dbUserTableUserField = (String)options.get("userField");
124         dbUserTableCredentialField = (String)options.get("credentialField");
125         
126         userQuery = "select "+dbUserTableCredentialField+" from "+dbUserTable+" where "+dbUserTableUserField+"=?";
127         
128         
129         //get the user roles query out of the options
130         dbUserRoleTable = (String)options.get("userRoleTable");
131         dbUserRoleTableUserField = (String)options.get("userRoleUserField");
132         dbUserRoleTableRoleField = (String)options.get("userRoleRoleField");
133         
134         rolesQuery = "select "+dbUserRoleTableRoleField+" from "+dbUserRoleTable+" where "+dbUserRoleTableUserField+"=?";
135         
136         if(LOG.isDebugEnabled())LOG.debug("userQuery = "+userQuery);
137         if(LOG.isDebugEnabled())LOG.debug("rolesQuery = "+rolesQuery);
138     }
139 }