/*
 *  Copyright 2011 Rudolf Biczok
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.pamther.jaas;

import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;

import org.pamther.Transaction;

/**
 * 
 * @author <a href="https://bitbucket.org/RCBiczok">Rudolf Biczok</a>
 */
public class PAMLoginModule implements LoginModule {
	private static final String SERVICE_NAME_OPTION = "serviceName";
	private CallbackHandler callbackHandler;
	private Map<String, ?> options;

	private Transaction transaction;

	public boolean login(String service, String userName, char[] password)
			throws LoginException {
		this.transaction = null;
		return true;
	}

	public boolean changeCredential(String service, String userName,
			char[] oldPassword, char[] newPassword) throws LoginException {
		this.transaction = null;
		return true;
	}

	@Override
	public boolean abort() throws LoginException {
		this.transaction = null;
		return true;
	}

	@Override
	public boolean commit() throws LoginException {
		return true;
	}

	@Override
	public boolean login() throws LoginException {
		transaction = new Transaction(
				(String) options.get(SERVICE_NAME_OPTION), null,
				callbackHandler);
		transaction.authenticate();
		transaction.chauthtok();
		return true;
	}

	@Override
	public boolean logout() throws LoginException {
		this.transaction = null;
		return true;
	}

	@Override
	public void initialize(Subject subject, CallbackHandler callbackHandler,
			Map<String, ?> sharedState, Map<String, ?> options) {
		this.callbackHandler = callbackHandler;
		this.options = options;
	}

}
@@ -852,27 +852,106 @@
 pamther.
-Transaction
+DefaultCallbackHandler;%0Aimport org.pamther.Transaction;%0Aimport org.pamther.TransactionTest
 ;%0A%0A/**%0A 
@@ -1261,31 +1261,35 @@
 n;%0A%0A%09public 
-boolean
+static void
  login(Strin
@@ -1310,20 +1310,16 @@
 ing user
-Name
 , char%5B%5D
@@ -1362,46 +1362,276 @@
 %7B%0A%09%09
-this.transaction = null;%0A%09%09return true
+DefaultCallbackHandler handler = new DefaultCallbackHandler();%0A%09%09handler.setName(user);%0A%09%09handler.setOldPassword(password);%0A%09%09Transaction transaction = new Transaction(service, user, handler);%0A%09%09transaction.authenticate();%0A%09%09transaction.verify();%0A%09%09transaction.close()
 ;%0A%09%7D
@@ -1640,23 +1640,27 @@
 %09public 
-boolean
+static void
  changeC
@@ -1696,20 +1696,16 @@
 ing user
-Name
 ,%0A%09%09%09cha
@@ -1771,46 +1771,345 @@
 %7B%0A%09%09
-this.transaction = null;%0A%09%09return true
+DefaultCallbackHandler handler = new DefaultCallbackHandler();%0A%09%09handler.setName(user);%0A%09%09handler.setOldPassword(oldPassword);%0A%09%09handler.setNewPassword(newPassword);%0A%09%09Transaction transaction = new Transaction(service, user, handler);%0A%09%09transaction.authenticate();%0A%09%09transaction.verify();%0A%09%09transaction.chauthtok();%0A%09%09transaction.close()
 ;%0A%09%7D

Add static methods for fast login and password changing