001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.net.URLConnection;
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.status.StatusLogger;
022import org.apache.logging.log4j.util.Base64Util;
023import org.apache.logging.log4j.util.LoaderUtil;
024import org.apache.logging.log4j.util.PropertiesUtil;
025
026/**
027 * Provides the Basic Authorization header to a request.
028 */
029public class BasicAuthorizationProvider implements AuthorizationProvider {
030    private static final String[] PREFIXES = {"log4j2.config.", "logging.auth."};
031    private static final String AUTH_USER_NAME = "username";
032    private static final String AUTH_PASSWORD = "password";
033    private static final String AUTH_PASSWORD_DECRYPTOR = "passwordDecryptor";
034    public static final String CONFIG_USER_NAME = "log4j2.configurationUserName";
035    public static final String CONFIG_PASSWORD = "log4j2.configurationPassword";
036    public static final String PASSWORD_DECRYPTOR = "log4j2.passwordDecryptor";
037
038    private static Logger LOGGER = StatusLogger.getLogger();
039
040    private String authString = null;
041
042    public BasicAuthorizationProvider(PropertiesUtil props) {
043        String userName = props.getStringProperty(PREFIXES,AUTH_USER_NAME,
044                () -> props.getStringProperty(CONFIG_USER_NAME));
045        String password = props.getStringProperty(PREFIXES, AUTH_PASSWORD,
046                () -> props.getStringProperty(CONFIG_PASSWORD));
047        String decryptor = props.getStringProperty(PREFIXES, AUTH_PASSWORD_DECRYPTOR,
048                () -> props.getStringProperty(PASSWORD_DECRYPTOR));
049        if (decryptor != null) {
050            try {
051                Object obj = LoaderUtil.newInstanceOf(decryptor);
052                if (obj instanceof PasswordDecryptor) {
053                    password = ((PasswordDecryptor) obj).decryptPassword(password);
054                }
055            } catch (Exception ex) {
056                LOGGER.warn("Unable to decrypt password.", ex);
057            }
058        }
059        if (userName != null && password != null) {
060            authString = "Basic " + Base64Util.encode(userName + ":" + password);
061        }
062    }
063
064    @Override
065    public void addAuthorization(URLConnection urlConnection) {
066        if (authString != null) {
067            urlConnection.setRequestProperty("Authorization", authString);
068        }
069    }
070}