1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 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 /** Init LoginModule. 46 * Called once by JAAS after new instance created. 47 * @param subject 48 * @param callbackHandler 49 * @param sharedState 50 * @param options 51 */ 52 public void initialize(Subject subject, 53 CallbackHandler callbackHandler, 54 Map<String,?> sharedState, 55 Map<String,?> options) 56 { 57 try 58 { 59 super.initialize(subject, callbackHandler, sharedState, options); 60 61 //get the datasource jndi name 62 dbJNDIName = (String)options.get("dbJNDIName"); 63 64 InitialContext ic = new InitialContext(); 65 dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName); 66 } 67 catch (NamingException e) 68 { 69 throw new IllegalStateException (e.toString()); 70 } 71 } 72 73 74 /** 75 * Get a connection from the DataSource 76 * @see AbstractDatabaseLoginModule#getConnection() 77 * @return the connection for the datasource 78 * @throws Exception 79 */ 80 public Connection getConnection () 81 throws Exception 82 { 83 return dataSource.getConnection(); 84 } 85 86 87 88 89 90 }