/*
* This file is part of ReadonlyREST.
*
* ReadonlyREST is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ReadonlyREST 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ReadonlyREST. If not, see http://www.gnu.org/licenses/
*/
package org.elasticsearch.plugin.readonlyrest.settings.rules;
import org.elasticsearch.plugin.readonlyrest.settings.AuthKeyProviderSettings;
import org.elasticsearch.plugin.readonlyrest.settings.RawSettings;
import org.elasticsearch.plugin.readonlyrest.settings.RuleSettings;
import org.elasticsearch.plugin.readonlyrest.settings.definitions.ProxyAuthDefinitionSettingsCollection;
import java.util.List;
public class ProxyAuthRuleSettings implements RuleSettings, AuthKeyProviderSettings {
public static final String ATTRIBUTE_NAME = "proxy_auth";
private static final String PROXY_AUTH_CONFIG = "proxy_auth_config";
private static final String USERS = "users";
private static final String DEFAULT_HEADER_NAME = "X-Forwarded-User";
private final List<String> users;
private final String userIdHeader;
@SuppressWarnings("unchecked")
public static ProxyAuthRuleSettings from(RawSettings settings,
ProxyAuthDefinitionSettingsCollection proxyAuthDefinitionSettingsCollection) {
String providerName = settings.stringReq(PROXY_AUTH_CONFIG);
return new ProxyAuthRuleSettings(
(List<String>) settings.notEmptyListReq(USERS),
proxyAuthDefinitionSettingsCollection.get(providerName).getUserIdHeader()
);
}
public static ProxyAuthRuleSettings from(List<String> users) {
return new ProxyAuthRuleSettings(users, DEFAULT_HEADER_NAME);
}
private ProxyAuthRuleSettings(List<String> users, String userIdHeader) {
this.users = users;
this.userIdHeader = userIdHeader;
}
public List<String> getUsers() {
return users;
}
public String getUserIdHeader() {
return userIdHeader;
}
@Override
public String getName() {
return ATTRIBUTE_NAME;
}
}