/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.web.security.jaspi;

import org.jboss.as.web.WebLogger;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.message.callback.CallerPrincipalCallback;
import javax.security.auth.message.callback.GroupPrincipalCallback;
import javax.security.auth.message.callback.PasswordValidationCallback;
import java.io.IOException;

/**
 * <p>
 * This class implements a {@code CallbackHandler} for the JASPI Web Profile.
 * </p>
 *
 * @author <a href="mailto:Anil.Saldhana@redhat.com">Anil Saldhana</a>
 * @author <a href="mailto:sguilhen@redhat.com">Stefan Guilhen</a>
 */
@SuppressWarnings("unused")
public class WebJASPICallbackHandler implements CallbackHandler {

    private CallerPrincipalCallback callerPrincipalCallback;

    private PasswordValidationCallback passwordValidationCallback;

    private GroupPrincipalCallback groupPrincipalCallback;

    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

        if (callbacks.length > 0) {
            for (Callback callback : callbacks) {
                if (callback instanceof CallerPrincipalCallback) {
                    CallerPrincipalCallback callerCallback = (CallerPrincipalCallback) callback;
                    if (callerCallback.getPrincipal()  != null)
                        this.callerPrincipalCallback = new CallerPrincipalCallback(callerCallback.getSubject(),
                                callerCallback.getPrincipal());
                    else
                        this.callerPrincipalCallback = new CallerPrincipalCallback(callerCallback.getSubject(),
                                callerCallback.getName());
                } else if (callback instanceof PasswordValidationCallback) {
                    PasswordValidationCallback passCallback = (PasswordValidationCallback) callback;
                    this.passwordValidationCallback = new PasswordValidationCallback(passCallback.getSubject(),
                            passCallback.getUsername(), passCallback.getPassword());
                } else if (callback instanceof GroupPrincipalCallback) {
                    GroupPrincipalCallback groupCallback = (GroupPrincipalCallback) callback;
                    this.groupPrincipalCallback = new GroupPrincipalCallback(groupCallback.getSubject(),
                            groupCallback.getGroups());
                } else
                    WebLogger.WEB_SECURITY_LOGGER.tracef("Callback %s not supported", callback.getClass().getCanonicalName());
            }
        }
    }

    public CallerPrincipalCallback getCallerPrincipalCallback() {
        return callerPrincipalCallback;
    }

    public PasswordValidationCallback getPasswordValidationCallback() {
        return passwordValidationCallback;
    }

    public GroupPrincipalCallback getGroupPrincipalCallback() {
        return groupPrincipalCallback;
    }
}
@@ -1070,34 +1070,27 @@
 ort 
-org.jboss.as.web.WebLogger
+java.io.IOException
 ;%0A%0Ai
@@ -1466,34 +1466,42 @@
 ck;%0A
+%0A
 import 
-java.io.IOException
+org.jboss.as.web.WebLogger
 ;%0A%0A/
@@ -2268,32 +2268,100 @@
 ipalCallback) %7B%0A
+                    if (this.callerPrincipalCallback == null) %7B%0A    
                 
@@ -2453,32 +2453,36 @@
                 
+ 
+   
 if (callerCallba
@@ -2509,16 +2509,20 @@
 = null)%0A
+    
         
@@ -2649,32 +2649,36 @@
                 
+  
+  
 callerCallback.g
@@ -2713,21 +2713,29 @@
         
+    
 else%0A
+    
         
@@ -2862,32 +2862,36 @@
                 
+    
 callerCallback.g
@@ -2893,32 +2893,54 @@
 ack.getName());%0A
+                    %7D%0A
                 
@@ -2992,32 +2992,103 @@
 tionCallback) %7B%0A
+                    if (this.passwordValidationCallback == null) %7B%0A    
                 
@@ -3188,24 +3188,28 @@
             
+ 
+   
 this.passwor
@@ -3316,16 +3316,20 @@
         
+    
 passCall
@@ -3373,24 +3373,46 @@
 assword());%0A
+                    %7D%0A
             
@@ -3464,32 +3464,99 @@
 ipalCallback) %7B%0A
+                    if (this.groupPrincipalCallback == null) %7B%0A    
                 
@@ -3625,32 +3625,36 @@
 back) callback;%0A
+    
                 
@@ -3770,16 +3770,20 @@
         
+    
 groupCal
@@ -3798,24 +3798,46 @@
 tGroups());%0A
+                    %7D%0A
             

Fix for TCK JASPI tests - prevents established callbacks from being overriden in subsequent calls to handle.