All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.security.XMACallbackHandler Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * Created on 13.06.2003
 *
 *
 *
 */
package at.spardat.xma.security;

import java.io.InputStream;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.security.auth.callback.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import at.spardat.xma.boot.logger.LogLevel;
import at.spardat.xma.session.XMASessionClient;

/**
 * Generic callback handler.
 * This callback handler is passed to every LoginModule which is used as client side authentication plugin.
 * It supports NameCallback, PasswordCallback, TextInputCallback, TextOutputCallback, {@link ChallengeCallback}.
 * From the callbacks defined in JAAS only LanguageCallback is not supported.
 * For every CallengeCallback a server communication is established and the server side
 * authentication plugin is asked for a challenge by calling {@link LoginModuleServer#getPreLoginInfo} via the
 * {@link at.spardat.xma.session.LoginServlet}.
 * All other callbacks are handled with user interface dialog which contains an input field for every callback.
 *
 * @author s2877
 */
public class XMACallbackHandler implements CallbackHandler, SelectionListener {
    private XMASessionClient session;
    Callback[] callbacks;
    Control[] controls;
    Display display;
    boolean displayIsNew;
    Shell shell;
    Button ok,cancel;
    Control lastControl;
    Image icon;
    boolean exitStatus;

    /**
     * Constructor
     * This method is intended to be called only be the XMA runtime.
     * @param session the XMA session it belongs to.
     */
    public XMACallbackHandler(XMASessionClient session) {
        this.session=session;
    }

    /**
     * Handle the callbacks.
     * This includes user interaction if callbacks contains any
     * NameCallback, PasswordCallback, TextInputCallback or TextOutputCallback.
     * It includes server communication if callbacks contains a {@link ChallengeCallback}.
     * Other callbacks are not supported.
     * @param callbacks the callbacks to handle.
     * @throws UnsupportedCallbackException if callbacks contains any unsupported Callback.
     */
    public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
        if(callbacks.length<=0) return;
        this.callbacks=callbacks;
        controls = new Control[callbacks.length];

        readIn();
        if(lastControl!=null) {
            exitStatus=false;
            openShell();
            lastControl=null;
        } else {
            exitStatus=true;
            readBack();
        }
        if(displayIsNew) {
            display.dispose();
        }
    }

    /**
     * Creates and initializes the SWT-Shell
     */
    private void initShell() {
        //todo default locale
        //todo migrate to new layout
        Locale locale = Locale.getDefault();
        ResourceBundle messages = ResourceBundle.getBundle("at.spardat.xma.security.XMACallbackHandler",locale);
        display = Display.getCurrent();
        if(display==null) {
            displayIsNew=true;
            display = Display.getDefault();
        }
        shell = new Shell(display,SWT.DIALOG_TRIM|SWT.RESIZE);
        shell.setText(messages.getString("handler"));
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("at/spardat/xma/page/imc16x16.gif");
        if(in!=null) {
            icon = new Image(display, in);
            shell.setImage(icon);
            shell.addDisposeListener(new DisposeListener(){
                public void widgetDisposed(DisposeEvent e) {
                    icon.dispose();
                }
            });
        }
        FormLayout layout = new FormLayout();
        layout.marginHeight=10;
        layout.marginWidth=10;
        shell.setLayout(layout);
    }

    /**
     * Creates and layouts the widgets for all callbacks that need user interaction.
     * Calls {@link initShell} if any user interaction in necessary.
     * @throws UnsupportedCallbackException if callbacks contains any unknown Callback.
     */
    private void readIn() throws UnsupportedCallbackException {
        Label label;
        Control control=null;
        for(int i=0;i-1) {
                    ((Combo)control).select(call.getDefaultChoice());
                }
            } else if(callbacks[i] instanceof TextOutputCallback) {
                if(lastControl==null) { initShell(); }
                label=null;
                control = new Label(shell,SWT.NONE);
                ((Label)control).setText(((TextOutputCallback)callbacks[i]).getMessage());
//            } else if(callbacks[i] instanceof LanguageCallback) {
//                label=null;
//                control=null;
            } else if(callbacks[i] instanceof ChallengeCallback) {
                label=null;
                control=null;
            } else {
                throw new UnsupportedCallbackException(callbacks[i]);
            }

            if(label!=null) {
                FormData data = new FormData();
                data.left = new FormAttachment(0,0);
                data.top = new FormAttachment(control,0,SWT.CENTER);
                label.setLayoutData(data);
            }
            if(control!=null) {
                FormData data = new FormData();
//                data.left = new FormAttachment(sep,5,SWT.RIGHT);
                data.left = new FormAttachment(35,0);
                if(lastControl==null) {
                    data.top = new FormAttachment(0,0);
                } else {
                    data.top = new FormAttachment(lastControl,5,SWT.BOTTOM);
                }
                data.right = new FormAttachment(100,0);
                data.width=125;
                control.setLayoutData(data);
            }

            controls[i]=control;
            if(control!=null) lastControl=control;
        }
    }

    /**
     * Open and runs the dialog. This method blocks until the
     * dialog is closed by the user.
     */
    private void openShell() {
        //todo default locale
        Locale locale = Locale.getDefault();
        ResourceBundle messages = ResourceBundle.getBundle("at.spardat.xma.security.XMACallbackHandler",locale);

        ok = new Button (shell, SWT.PUSH);
        ok.setText(messages.getString("handler.okW"));
        ok.addSelectionListener(this);

        cancel = new Button (shell, SWT.PUSH);
        cancel.setText(messages.getString("handler.cancelW"));
        cancel.addSelectionListener(this);

        FormData data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(cancel,-5,SWT.LEFT);
        data.top = new FormAttachment(lastControl,10,SWT.BOTTOM);
        ok.setLayoutData(data);

        data = new FormData();
        data.width = 60;
        data.right = new FormAttachment(100,100,0);
        data.top = new FormAttachment(lastControl,10,SWT.BOTTOM);
        cancel.setLayoutData(data);

        shell.setDefaultButton(ok);
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Writes all the gathered information into the callback-objects.
     * Does the server communication for any ChallengeCallback if necessary.
     * @throws UnsupportedCallbackException if callbacks contains any unknown Callback.
     */
    private void readBack() throws UnsupportedCallbackException {
        for(int i=0;i
     * true means ok pressed 
* false means cancel pressesd
*/ public boolean getExitstatus() { return exitStatus; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy