Debian Codegraph @

src/main/org/h2/security/auth/H2AuthConfigXml.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: Alessandro Ventura
5 */
6 package org.h2.security.auth;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.StringReader;
11 import java.net.URL;
12
13 import javax.xml.XMLConstants;
14 import javax.xml.parsers.ParserConfigurationException;
15 import javax.xml.parsers.SAXParser;
16 import javax.xml.parsers.SAXParserFactory;
17
18 import org.xml.sax.Attributes;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.helpers.DefaultHandler;
22
23 /**
24 * Parser of external authentication XML configuration file
25 */
26 public class H2AuthConfigXml extends DefaultHandler{
27
28 private H2AuthConfig result;
29 private HasConfigProperties lastConfigProperties;
30
31 @Override
32 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
33 switch (qName) {
34 case "h2Auth":
35 result = new H2AuthConfig();
36 result.setAllowUserRegistration("true".equals(
37 getAttributeValueOr("allowUserRegistration",attributes,"false")));
38 result.setCreateMissingRoles("true".equals(
39 getAttributeValueOr("createMissingRoles",attributes, "true")));
40 break;
41 case "realm":
42 RealmConfig realmConfig = new RealmConfig();
43 realmConfig.setName(getMandatoryAttributeValue("name", attributes));
44 realmConfig.setValidatorClass(getMandatoryAttributeValue("validatorClass", attributes));
45 result.getRealms().add(realmConfig);
46 lastConfigProperties=realmConfig;
47 break;
48 case "userToRolesMapper":
49 UserToRolesMapperConfig userToRolesMapperConfig = new UserToRolesMapperConfig();
50 userToRolesMapperConfig.setClassName(getMandatoryAttributeValue("className", attributes));
51 result.getUserToRolesMappers().add(userToRolesMapperConfig);
52 lastConfigProperties=userToRolesMapperConfig;
53 break;
54 case "property":
55 if (lastConfigProperties==null) {
56 throw new SAXException("property element in the wrong place");
57 }
58 lastConfigProperties.getProperties().add(new PropertyConfig(
59 getMandatoryAttributeValue("name", attributes),
60 getMandatoryAttributeValue("value", attributes)));
61 break;
62 default:
63 throw new SAXException("unexpected element "+qName);
64 }
65
66 }
67
68 @Override
69 public void endElement(String uri, String localName, String qName) throws SAXException {
70 if (lastConfigProperties!=null && qName.equals("property")==false) {
71 lastConfigProperties=null;
72 }
73 }
74
75 @Override
76 public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
77 return new InputSource(new StringReader(""));
78 }
79
80 private static String getMandatoryAttributeValue(String attributeName, Attributes attributes) throws SAXException {
81 String attributeValue=attributes.getValue(attributeName);
82 if (attributeValue==null || attributeValue.trim().equals("")) {
83 throw new SAXException("missing attribute "+attributeName);
84 }
85 return attributeValue;
86
87 }
88
89 private static String getAttributeValueOr(String attributeName, Attributes attributes, String defaultValue) {
90 String attributeValue=attributes.getValue(attributeName);
91 if (attributeValue==null || attributeValue.trim().equals("")) {
92 return defaultValue;
93 }
94 return attributeValue;
95 }
96
97 /**
98 * Returns parsed authenticator configuration.
99 *
100 * @return Authenticator configuration.
101 */
102 public H2AuthConfig getResult() {
103 return result;
104 }
105
106 /**
107 * Parse the xml.
108 *
109 * @param url the source of the xml configuration.
110 * @return Authenticator configuration.
111 * @throws ParserConfigurationException if a parser cannot be created.
112 * @throws SAXException for SAX errors.
113 * @throws IOException If an I/O error occurs
114 */
115 public static H2AuthConfig parseFrom(URL url)
116 throws SAXException, IOException, ParserConfigurationException {
117 try (InputStream inputStream = url.openStream()) {
118 return parseFrom(inputStream);
119 }
120 }
121
122 /**
123 * Parse the xml.
124 *
125 * @param inputStream the source of the xml configuration.
126 * @return Authenticator configuration.
127 * @throws ParserConfigurationException if a parser cannot be created.
128 * @throws SAXException for SAX errors.
129 * @throws IOException If an I/O error occurs
130 */
131 public static H2AuthConfig parseFrom(InputStream inputStream)
132 throws SAXException, IOException, ParserConfigurationException {
133 SAXParserFactory spf = SAXParserFactory.newInstance();
134 spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
135 spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
136 spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
137 spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
138 spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
139 SAXParser saxParser = spf.newSAXParser();
140 H2AuthConfigXml xmlHandler = new H2AuthConfigXml();
141 saxParser.parse(inputStream, xmlHandler);
142 return xmlHandler.getResult();
143 }
144 }