Package org.javaee7.jaspic.lifecycle.sam

Source Code of org.javaee7.jaspic.lifecycle.sam.TestLifecycleAuthModule

package org.javaee7.jaspic.lifecycle.sam;

import static javax.security.auth.message.AuthStatus.SEND_SUCCESS;
import static javax.security.auth.message.AuthStatus.SUCCESS;

import java.io.IOException;
import java.util.Map;

import javax.security.auth.Subject;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.message.AuthException;
import javax.security.auth.message.AuthStatus;
import javax.security.auth.message.MessageInfo;
import javax.security.auth.message.MessagePolicy;
import javax.security.auth.message.callback.CallerPrincipalCallback;
import javax.security.auth.message.callback.GroupPrincipalCallback;
import javax.security.auth.message.module.ServerAuthModule;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* A test SAM that always authenticates a hard-coded user "test" with role "architect" for every request.
*
* @author Arjan Tijms
*
*/
public class TestLifecycleAuthModule implements ServerAuthModule {

    private CallbackHandler handler;
    private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, HttpServletResponse.class };

    @Override
    public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler,
            @SuppressWarnings("rawtypes") Map options) throws AuthException {
        this.handler = handler;
    }

    @Override
    public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject)
            throws AuthException {

        HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();

        try {
            response.getWriter().write("validateRequest invoked\n");

            handler.handle(new Callback[] {
                new CallerPrincipalCallback(clientSubject, "test"),
                new GroupPrincipalCallback(clientSubject, new String[] { "architect" }) });
        } catch (IOException | UnsupportedCallbackException e) {
            throw (AuthException) new AuthException().initCause(e);
        }

        return SUCCESS;
    }

    @Override
    public Class<?>[] getSupportedMessageTypes() {
        return supportedMessageTypes;
    }

    @Override
    public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {

        HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();

        try {
            response.getWriter().write("secureResponse invoked\n");
        } catch (IOException e) {
            throw (AuthException) new AuthException().initCause(e);
        }

        return SEND_SUCCESS;
    }

    @Override
    public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {
        HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();

        try {
            response.getWriter().write("cleanSubject invoked\n");
        } catch (IOException e) {
            throw (AuthException) new AuthException().initCause(e);
        }
    }
}
TOP

Related Classes of org.javaee7.jaspic.lifecycle.sam.TestLifecycleAuthModule

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.