org.sonar.plugins.preauth.PreAuthHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.preauth.PreAuthHelper.java

Source

/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar 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 3 of the License, or (at your option) any later version.
 *
 * Sonar 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 Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */

package org.sonar.plugins.preauth;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerExtension;
import org.sonar.api.config.Settings;

import com.google.common.annotations.VisibleForTesting;

public class PreAuthHelper implements ServerExtension {

    private static final Logger LOG = LoggerFactory.getLogger(PreAuthHelper.class);

    @VisibleForTesting
    static final String PRE_AUTH_HEADER_NAME_KEY = "preAuth.headerName";
    @VisibleForTesting
    static final String DEFAULT_PRE_AUTH_HEADER_NAME = "REMOTE_USER";

    private final String preAuthHeaderName;

    public PreAuthHelper(Settings settings) {
        this.preAuthHeaderName = StringUtils.defaultString(settings.getString(PRE_AUTH_HEADER_NAME_KEY),
                DEFAULT_PRE_AUTH_HEADER_NAME);
    }

    public String getPreAuthHeaderName() {
        return preAuthHeaderName;
    }

    public boolean isPreAuthRequired(HttpServletRequest request) {
        return request.getSession(false) == null;
    }

    /**
     * Finds the name of the preauthenticated user.
     *
     * @param request the {@link HttpServletRequest}
     * @return the name of the preauthenticated user or <code>null</code>
     */
    public String findPreAuthenticatedUser(HttpServletRequest request) {
        String userNameFromHeader = request.getHeader(preAuthHeaderName);
        if (userNameFromHeader == null) {
            LOG.info("Preauthentication Header '" + preAuthHeaderName + "' not found.");
            logAvailableHeaders(request);
        }
        return userNameFromHeader;
    }

    private void logAvailableHeaders(HttpServletRequest request) {
        if (!LOG.isDebugEnabled()) {
            return;
        }
        StringBuilder sb = new StringBuilder("Available Headers: ");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            sb.append(headerNames.nextElement());
            if (headerNames.hasMoreElements()) {
                sb.append(", ");
            }
        }
        LOG.debug(sb.toString());
    }

}