001 /* 002 * $Id: JAASLoginService.java,v 1.4 2005/11/11 23:05:15 rbair Exp $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 package org.jdesktop.swingx.auth; 022 023 import java.util.logging.Level; 024 import java.util.logging.Logger; 025 026 import javax.security.auth.callback.Callback; 027 import javax.security.auth.callback.CallbackHandler; 028 import javax.security.auth.callback.NameCallback; 029 import javax.security.auth.callback.PasswordCallback; 030 import javax.security.auth.login.AccountExpiredException; 031 import javax.security.auth.login.CredentialExpiredException; 032 import javax.security.auth.login.FailedLoginException; 033 import javax.security.auth.login.LoginContext; 034 import javax.security.auth.login.LoginException; 035 036 /** 037 * <b>JAASLoginService</b> implements a <b>LoginService</b> 038 * that uses JAAS for authentication. <b>JAASLoginService</b> uses the 039 * server name as name of the configuration for JAAS. 040 * 041 * @author Bino George 042 */ 043 public class JAASLoginService extends LoginService { 044 private static final Logger LOG = Logger.getLogger(JAASLoginService.class 045 .getName()); 046 047 /** 048 * Constructor for <b>JAASLoginService</b> 049 * @param server server name that is also used for the JAAS config name 050 */ 051 public JAASLoginService(String server) { 052 super(server); 053 } 054 055 056 /** 057 * @inheritDoc 058 * 059 */ 060 public boolean authenticate(String name, char[] password, String server) throws Exception { 061 try { 062 LoginContext loginContext = null; 063 064 loginContext = new LoginContext(getServer(), 065 new JAASCallbackHandler(name, password)); 066 loginContext.login(); 067 return true; 068 } catch (AccountExpiredException e) { 069 // TODO add explanation? 070 LOG.log(Level.WARNING, "", e); 071 return false; 072 } catch (CredentialExpiredException e) { 073 // TODO add explanation? 074 LOG.log(Level.WARNING, "", e); 075 return false; 076 } catch (FailedLoginException e) { 077 // TODO add explanation? 078 LOG.log(Level.WARNING, "", e); 079 return false; 080 } catch (LoginException e) { 081 // TODO add explanation? 082 LOG.log(Level.WARNING, "", e); 083 return false; 084 } catch (Throwable e) { 085 // TODO add explanation? 086 LOG.log(Level.WARNING, "", e); 087 return false; 088 } 089 } 090 091 class JAASCallbackHandler implements CallbackHandler { 092 093 private String name; 094 095 private char[] password; 096 097 public JAASCallbackHandler(String name, char[] passwd) { 098 this.name = name; 099 this.password = passwd; 100 } 101 102 public void handle(Callback[] callbacks) throws java.io.IOException { 103 for (int i = 0; i < callbacks.length; i++) { 104 if (callbacks[i] instanceof NameCallback) { 105 NameCallback cb = (NameCallback) callbacks[i]; 106 cb.setName(name); 107 } else if (callbacks[i] instanceof PasswordCallback) { 108 PasswordCallback cb = (PasswordCallback) callbacks[i]; 109 cb.setPassword(password); 110 } 111 } 112 } 113 114 } 115 116 117 }