1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 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.jaas.spi;
20
21 import java.sql.Connection;
22 import java.util.Map;
23
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.security.auth.Subject;
27 import javax.security.auth.callback.CallbackHandler;
28 import javax.sql.DataSource;
29
30 /**
31 * DataSourceLoginModule
32 *
33 * A LoginModule that uses a DataSource to retrieve user authentication
34 * and authorisation information.
35 *
36 * @see JDBCLoginModule
37 */
38 public class DataSourceLoginModule extends AbstractDatabaseLoginModule
39 {
40
41 private String dbJNDIName;
42 private DataSource dataSource;
43
44 /* ------------------------------------------------ */
45 /**
46 * Init LoginModule.
47 * <p>
48 * Called once by JAAS after new instance created.
49 *
50 * @param subject the subject
51 * @param callbackHandler the callback handler
52 * @param sharedState the shared state map
53 * @param options the option map
54 */
55 public void initialize(Subject subject,
56 CallbackHandler callbackHandler,
57 Map<String,?> sharedState,
58 Map<String,?> options)
59 {
60 try
61 {
62 super.initialize(subject, callbackHandler, sharedState, options);
63
64 //get the datasource jndi name
65 dbJNDIName = (String)options.get("dbJNDIName");
66
67 InitialContext ic = new InitialContext();
68 dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName);
69 }
70 catch (NamingException e)
71 {
72 throw new IllegalStateException (e.toString());
73 }
74 }
75
76 /**
77 * Get a connection from the DataSource
78 * @see AbstractDatabaseLoginModule#getConnection()
79 * @return the connection for the datasource
80 * @throws Exception if unable to get the connection
81 */
82 public Connection getConnection ()
83 throws Exception
84 {
85 return dataSource.getConnection();
86 }
87 }