001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.security.jaas;
018
019 import java.util.Map;
020 import java.util.Set;
021 import javax.security.auth.spi.LoginModule;
022 import javax.security.auth.Subject;
023 import javax.security.auth.DestroyFailedException;
024 import javax.security.auth.login.LoginException;
025 import javax.security.auth.callback.CallbackHandler;
026
027 /**
028 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
029 */
030 public class ConfiguredIdentityNamedUsernamePasswordLoginModule implements LoginModule {
031 public static final String CREDENTIAL_NAME = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Name";
032 public static final String USER_NAME = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Username";
033 public static final String PASSWORD = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Password";
034
035 private Subject subject;
036 private NamedUsernamePasswordCredential namedUsernamePasswordCredential;
037
038 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
039 this.subject = subject;
040 String name = (String) options.get(CREDENTIAL_NAME);
041 String username = (String) options.get(USER_NAME);
042 String password = (String) options.get(PASSWORD);
043 namedUsernamePasswordCredential = new NamedUsernamePasswordCredential(username, password.toCharArray(), name);
044 }
045
046 public boolean login() throws LoginException {
047 return true;
048 }
049
050 public boolean commit() throws LoginException {
051 if (subject.isReadOnly()) {
052 throw new LoginException("Subject is ReadOnly");
053 }
054
055 Set pvtCreds = subject.getPrivateCredentials();
056 if (namedUsernamePasswordCredential != null && !pvtCreds.contains(namedUsernamePasswordCredential)) {
057 pvtCreds.add(namedUsernamePasswordCredential);
058 }
059 return true;
060 }
061
062 public boolean abort() throws LoginException {
063 return logout();
064 }
065
066 public boolean logout() throws LoginException {
067 if (namedUsernamePasswordCredential == null) {
068 return true;
069 }
070
071 Set pvtCreds = subject.getPrivateCredentials(UsernamePasswordCredential.class);
072 if (pvtCreds.contains(namedUsernamePasswordCredential)) {
073 pvtCreds.remove(namedUsernamePasswordCredential);
074 }
075
076 try {
077 namedUsernamePasswordCredential.destroy();
078 } catch (DestroyFailedException e) {
079 // do nothing
080 }
081 namedUsernamePasswordCredential = null;
082
083 return true;
084 }
085 }