com.networknt.basicauth.BasicAuthConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of basic-config Show documentation
Show all versions of basic-config Show documentation
A config module for basic auth handler shared with lambda-native.
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* 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 com.networknt.basicauth;
import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import com.networknt.config.JsonMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BasicAuthConfig {
public static final String CONFIG_NAME = "basic-auth";
private static final String ENABLED = "enabled";
private static final String ENABLE_AD = "enableAD";
private static final String ALLOW_ANONYMOUS = "allowAnonymous";
private static final String ALLOW_BEARER_TOKEN = "allowBearerToken";
private static final String USERS = "users";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String PATHS = "paths";
public static final String ANONYMOUS = "anonymous";
public static final String BEARER = "bearer";
boolean enabled;
boolean enableAD;
boolean allowAnonymous;
boolean allowBearerToken;
Map users; // the key is the username to locate the object
private final Config config;
private Map mappedConfig;
public BasicAuthConfig() {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigUser();
}
/**
* Please note that this constructor is only for testing to load different config files
* to test different configurations.
* @param configName String
*/
public BasicAuthConfig(String configName) {
config = Config.getInstance();
mappedConfig = config.getJsonMapConfigNoCache(configName);
setConfigData();
setConfigUser();
}
public static BasicAuthConfig load() {
return new BasicAuthConfig();
}
public static BasicAuthConfig load(String configName) {
return new BasicAuthConfig(configName);
}
void reload() {
mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME);
setConfigData();
setConfigUser();
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isEnableAD() {
return enabled;
}
public void setEnableAD(boolean enabled) {
this.enableAD = enabled;
}
public boolean isAllowAnonymous() {
return allowAnonymous;
}
public void setAllowAnonymous(boolean allowAnonymous) {
this.allowAnonymous = allowAnonymous;
}
public boolean isAllowBearerToken() {
return allowBearerToken;
}
public void setAllowBearerToken(boolean allowBearerToken) {
this.allowBearerToken = allowBearerToken;
}
public Map getUsers() { return users; }
private void setConfigData() {
Object object = mappedConfig.get(ENABLED);
if(object != null) enabled = Config.loadBooleanValue(ENABLED, object);
object = mappedConfig.get(ENABLE_AD);
if(object != null) enableAD = Config.loadBooleanValue(ENABLE_AD, object);
object = mappedConfig.get(ALLOW_ANONYMOUS);
if(object != null) allowAnonymous = Config.loadBooleanValue(ALLOW_ANONYMOUS, object);
object = mappedConfig.get(ALLOW_BEARER_TOKEN);
if(object != null) allowBearerToken = Config.loadBooleanValue(ALLOW_BEARER_TOKEN, object);
}
private void setConfigUser() {
if (mappedConfig.get(USERS) instanceof List) {
List