<commit_before>/*
 * Copyright 2013 SimplifyOps
 *
 * 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.rundeck.jaas.jetty;

import org.eclipse.jetty.jaas.spi.AbstractLoginModule;
import org.eclipse.jetty.jaas.spi.PropertyFileLoginModule;
import org.eclipse.jetty.jaas.spi.UserInfo;
import org.rundeck.jaas.AbstractSharedLoginModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.security.Principal;
import java.util.List;
import java.util.Map;


/**
 * Augments Jetty property file login module {@link PropertyFileLoginModule}, to only perform authentication
 * via property file login, handles shared credentials logic, and does not use property file roles.
 */
public class JettyAuthPropertyFileLoginModule extends AbstractSharedLoginModule {
    public static final Logger logger = LoggerFactory.getLogger(JettyAuthPropertyFileLoginModule.class.getName());
    AbstractLoginModule module;
    UserInfo            userInfo;

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler, Map shared, Map options) {
        super.initialize(subject, callbackHandler, shared, options);
        if(options.containsKey("hotReload") && options.get("hotReload").equals("true")) {
            logger.debug("using reloadable realm property file reader");
            module = new ReloadablePropertyFileLoginModule();
        } else {
            logger.error("using static realm property file reader");
            module = new PropertyFileLoginModule();
        }
        module.initialize(subject, callbackHandler, shared, options);
    }

    /**
     * Uses jetty callbacks to retrieve auth credentials
     * @return
     * @throws IOException
     * @throws UnsupportedCallbackException
     * @throws LoginException
     */
    protected Object[] getCallBackAuth() throws IOException, UnsupportedCallbackException, LoginException {
        return JettySupport.performCallbacks(getCallbackHandler());
    }

    @Override
    protected Principal createUserPrincipal() {
        return JettySupport.createUserPrincipal(userInfo.getUserName());
    }

    /**
     * Does not generate role principals for the user.
     * @return
     */
    @Override<commit_msg>Remove unneeded log statement.<commit_after>/*
 * Copyright 2013 SimplifyOps
 *
 * 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.rundeck.jaas.jetty;

import org.eclipse.jetty.jaas.spi.AbstractLoginModule;
import org.eclipse.jetty.jaas.spi.PropertyFileLoginModule;
import org.eclipse.jetty.jaas.spi.UserInfo;
import org.rundeck.jaas.AbstractSharedLoginModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.security.Principal;
import java.util.List;
import java.util.Map;


/**
 * Augments Jetty property file login module {@link PropertyFileLoginModule}, to only perform authentication
 * via property file login, handles shared credentials logic, and does not use property file roles.
 */
public class JettyAuthPropertyFileLoginModule extends AbstractSharedLoginModule {
    public static final Logger logger = LoggerFactory.getLogger(JettyAuthPropertyFileLoginModule.class.getName());
    AbstractLoginModule module;
    UserInfo            userInfo;

    @Override
    public void initialize(Subject subject, CallbackHandler callbackHandler, Map shared, Map options) {
        super.initialize(subject, callbackHandler, shared, options);
        if(options.containsKey("hotReload") && options.get("hotReload").equals("true")) {
            logger.debug("using reloadable realm property file reader");
            module = new ReloadablePropertyFileLoginModule();
        } else {
            module = new PropertyFileLoginModule();
        }
        module.initialize(subject, callbackHandler, shared, options);
    }

    /**
     * Uses jetty callbacks to retrieve auth credentials
     * @return
     * @throws IOException
     * @throws UnsupportedCallbackException
     * @throws LoginException
     */
    protected Object[] getCallBackAuth() throws IOException, UnsupportedCallbackException, LoginException {
        return JettySupport.performCallbacks(getCallbackHandler());
    }

    @Override
    protected Principal createUserPrincipal() {
        return JettySupport.createUserPrincipal(userInfo.getUserName());
    }

    /**
     * Does not generate role principals for the user.
     * @return
     */
    @Override
{'fraction_non_alphanumeric': 0.08356024506466984, 'fraction_numerical': 0.0034036759700476512, 'mean_word_length': 3.922110552763819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637945', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1461, 'n_words': 462}