/*
 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.ecm.automation.core.operations.users;

import java.security.Principal;

import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService;
import org.nuxeo.runtime.api.Framework;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 */
@Operation(id = LoginAs.ID, category = Constants.CAT_USERS_GROUPS, label = "Login As", description = "Login As the given user. If no user is given a system login is performed. This is a void operations - the input will be returned back as the output.")
public class LoginAs {

    public static final String ID = "Auth.LoginAs";

    @Context
    protected OperationContext ctx;

    @Param(name = "name", required = false)
    protected String name;

    @OperationMethod
    public void run() throws Exception {
        LoginContext lc = null;
        if (name == null) {
            Principal origPrincipal = ctx.getPrincipal();
            if (origPrincipal != null) {
                lc = Framework.loginAs(origPrincipal.getName());
            } else {
                lc = Framework.login();
            }
        } else {
            lc = loginAs(name);
        }
        if (lc != null) {
            ctx.getLoginStack().push(lc);
        }
    }

    public static LoginContext loginAs(String username) throws LoginException {
        // New code to test unrestricted session as somebody
        UserIdentificationInfo userIdent = new UserIdentificationInfo(username,
                username);

        userIdent.setLoginPluginName("Trusting_LM");
        PluggableAuthenticationService service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(
                PluggableAuthenticationService.NAME);
        CallbackHandler handler = service.getCallbackHandler(userIdent);
        LoginContext loginContext = new LoginContext("nuxeo-ecm-web", handler);
        loginContext.login();
        return loginContext;
    }

}

@@ -760,128 +760,26 @@
 uth.
-callback.CallbackHandler;%0Aimport javax.security.auth.login.LoginContext;%0Aimport javax.security.auth.login.LoginException
+login.LoginContext
 ;%0A%0Ai
@@ -1119,72 +1119,8 @@
 am;%0A
-import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;%0A
 impo
@@ -1161,25 +1161,13 @@
 uth.
-service.Pluggable
+Nuxeo
 Auth
@@ -1176,23 +1176,22 @@
 tication
-Service
+Filter
 ;%0Aimport
@@ -2143,16 +2143,42 @@
    lc = 
+NuxeoAuthenticationFilter.
 loginAs(
@@ -2262,16 +2262,16 @@
 sh(lc);%0A
+
         
@@ -2283,700 +2283,6 @@
  %7D%0A%0A
-    public static LoginContext loginAs(String username) throws LoginException %7B%0A        // New code to test unrestricted session as somebody%0A        UserIdentificationInfo userIdent = new UserIdentificationInfo(username,%0A                username);%0A%0A        userIdent.setLoginPluginName(%22Trusting_LM%22);%0A        PluggableAuthenticationService service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(%0A                PluggableAuthenticationService.NAME);%0A        CallbackHandler handler = service.getCallbackHandler(userIdent);%0A        LoginContext loginContext = new LoginContext(%22nuxeo-ecm-web%22, handler);%0A        loginContext.login();%0A        return loginContext;%0A    %7D%0A%0A
 %7D%0A

Use new NuxeoAuthenticationFilter.loginAs utility method