View Javadoc

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