|
| 1 | +package com.networknt.openapi; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import com.networknt.config.Config; |
| 5 | +import com.networknt.config.ConfigException; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class UnifiedSecurityConfig { |
| 15 | + private static final Logger logger = LoggerFactory.getLogger(UnifiedSecurityConfig.class); |
| 16 | + public static final String CONFIG_NAME = "unified-security"; |
| 17 | + public static final String ENABLED = "enabled"; |
| 18 | + public static final String ANONYMOUS_PREFIXES = "anonymousPrefixes"; |
| 19 | + public static final String PATH_PREFIX_AUTHS = "pathPrefixAuths"; |
| 20 | + public static final String PREFIX = "prefix"; |
| 21 | + public static final String BASIC = "basic"; |
| 22 | + public static final String JWT = "jwt"; |
| 23 | + public static final String APIKEY = "apikey"; |
| 24 | + public static final String JWK_SERVICE_IDS = "jwkServiceIds"; |
| 25 | + |
| 26 | + boolean enabled; |
| 27 | + List<String> anonymousPrefixes; |
| 28 | + List<UnifiedPathPrefixAuth> pathPrefixAuths; |
| 29 | + |
| 30 | + private Config config; |
| 31 | + private Map<String, Object> mappedConfig; |
| 32 | + |
| 33 | + private UnifiedSecurityConfig() { |
| 34 | + this(CONFIG_NAME); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Please note that this constructor is only for testing to load different config files |
| 39 | + * to test different configurations. |
| 40 | + * @param configName String |
| 41 | + */ |
| 42 | + private UnifiedSecurityConfig(String configName) { |
| 43 | + config = Config.getInstance(); |
| 44 | + mappedConfig = config.getJsonMapConfigNoCache(configName); |
| 45 | + setConfigData(); |
| 46 | + setConfigList(); |
| 47 | + } |
| 48 | + public static UnifiedSecurityConfig load() { |
| 49 | + return new UnifiedSecurityConfig(); |
| 50 | + } |
| 51 | + |
| 52 | + public static UnifiedSecurityConfig load(String configName) { |
| 53 | + return new UnifiedSecurityConfig(configName); |
| 54 | + } |
| 55 | + |
| 56 | + void reload() { |
| 57 | + mappedConfig = config.getJsonMapConfigNoCache(CONFIG_NAME); |
| 58 | + setConfigData(); |
| 59 | + setConfigList(); |
| 60 | + } |
| 61 | + |
| 62 | + public boolean isEnabled() { |
| 63 | + return enabled; |
| 64 | + } |
| 65 | + |
| 66 | + public void setEnabled(boolean enabled) { |
| 67 | + this.enabled = enabled; |
| 68 | + } |
| 69 | + |
| 70 | + public List<String> getAnonymousPrefixes() { |
| 71 | + return anonymousPrefixes; |
| 72 | + } |
| 73 | + |
| 74 | + public void setAnonymousPrefixes(List<String> anonymousPrefixes) { |
| 75 | + this.anonymousPrefixes = anonymousPrefixes; |
| 76 | + } |
| 77 | + |
| 78 | + public List<UnifiedPathPrefixAuth> getPathPrefixAuths() { |
| 79 | + return pathPrefixAuths; |
| 80 | + } |
| 81 | + |
| 82 | + public void setPathPrefixAuths(List<UnifiedPathPrefixAuth> pathPrefixAuths) { |
| 83 | + this.pathPrefixAuths = pathPrefixAuths; |
| 84 | + } |
| 85 | + |
| 86 | + private void setConfigData() { |
| 87 | + Object object = mappedConfig.get(ENABLED); |
| 88 | + if(object != null && (Boolean) object) { |
| 89 | + setEnabled(true); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void setConfigList() { |
| 94 | + // anonymous prefixes |
| 95 | + if (mappedConfig != null && mappedConfig.get(ANONYMOUS_PREFIXES) != null) { |
| 96 | + Object object = mappedConfig.get(ANONYMOUS_PREFIXES); |
| 97 | + anonymousPrefixes = new ArrayList<>(); |
| 98 | + if(object instanceof String) { |
| 99 | + String s = (String)object; |
| 100 | + s = s.trim(); |
| 101 | + if(logger.isTraceEnabled()) logger.trace("s = " + s); |
| 102 | + if(s.startsWith("[")) { |
| 103 | + // json format |
| 104 | + try { |
| 105 | + anonymousPrefixes = Config.getInstance().getMapper().readValue(s, new TypeReference<List<String>>() {}); |
| 106 | + } catch (Exception e) { |
| 107 | + throw new ConfigException("could not parse the anonymousPrefixes json with a list of strings."); |
| 108 | + } |
| 109 | + } else { |
| 110 | + // comma separated |
| 111 | + anonymousPrefixes = Arrays.asList(s.split("\\s*,\\s*")); |
| 112 | + } |
| 113 | + } else if (object instanceof List) { |
| 114 | + List prefixes = (List)object; |
| 115 | + prefixes.forEach(item -> { |
| 116 | + anonymousPrefixes.add((String)item); |
| 117 | + }); |
| 118 | + } else { |
| 119 | + throw new ConfigException("anonymousPrefixes must be a string or a list of strings."); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // path prefix auth mapping |
| 124 | + if (mappedConfig.get(PATH_PREFIX_AUTHS) != null) { |
| 125 | + Object object = mappedConfig.get(PATH_PREFIX_AUTHS); |
| 126 | + pathPrefixAuths = new ArrayList<>(); |
| 127 | + if(object instanceof String) { |
| 128 | + String s = (String)object; |
| 129 | + s = s.trim(); |
| 130 | + if(logger.isTraceEnabled()) logger.trace("pathPrefixAuth s = " + s); |
| 131 | + if(s.startsWith("[")) { |
| 132 | + // json format |
| 133 | + try { |
| 134 | + pathPrefixAuths = Config.getInstance().getMapper().readValue(s, new TypeReference<List<UnifiedPathPrefixAuth>>() {}); |
| 135 | + } catch (Exception e) { |
| 136 | + throw new ConfigException("could not parse the pathPrefixAuths json with a list of string and object."); |
| 137 | + } |
| 138 | + } else { |
| 139 | + throw new ConfigException("pathPrefixAuths must be a list of string object map."); |
| 140 | + } |
| 141 | + } else if (object instanceof List) { |
| 142 | + // the object is a list of map, we need convert it to PathPrefixAuth object. |
| 143 | + List<Map<String, Object>> values = (List<Map<String, Object>>)object; |
| 144 | + for(Map<String, Object> value: values) { |
| 145 | + UnifiedPathPrefixAuth unifiedPathPrefixAuth = new UnifiedPathPrefixAuth(); |
| 146 | + |
| 147 | + unifiedPathPrefixAuth.setPathPrefix((String)value.get(PREFIX)); |
| 148 | + unifiedPathPrefixAuth.setBasic(value.get(BASIC) == null ? false : (Boolean)value.get(BASIC)); |
| 149 | + unifiedPathPrefixAuth.setJwt(value.get(JWT) == null ? false : (Boolean)value.get(JWT)); |
| 150 | + unifiedPathPrefixAuth.setApikey(value.get(APIKEY) == null ? false : (Boolean)value.get(APIKEY)); |
| 151 | + Object ids = value.get(JWK_SERVICE_IDS); |
| 152 | + if(ids instanceof String) { |
| 153 | + String s = (String)value.get(JWK_SERVICE_IDS); |
| 154 | + if(s.startsWith("[")) { |
| 155 | + // json format |
| 156 | + try { |
| 157 | + unifiedPathPrefixAuth.setJwkServiceIds(Config.getInstance().getMapper().readValue(s, new TypeReference<List<String>>() {})); |
| 158 | + } catch (Exception e) { |
| 159 | + throw new ConfigException("could not parse the jwkServiceIds json with a list of strings."); |
| 160 | + } |
| 161 | + } else { |
| 162 | + // comma separated |
| 163 | + unifiedPathPrefixAuth.setJwkServiceIds(Arrays.asList(s.split("\\s*,\\s*"))); |
| 164 | + } |
| 165 | + } else if(ids instanceof List ) { |
| 166 | + // it must be a json array. |
| 167 | + unifiedPathPrefixAuth.setJwkServiceIds((List)ids); |
| 168 | + } |
| 169 | + pathPrefixAuths.add(unifiedPathPrefixAuth); |
| 170 | + } |
| 171 | + } else { |
| 172 | + throw new ConfigException("pathPrefixAuth must be a list of string object map."); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments