001package com.nimbusds.common.config;
002
003
004import java.util.Properties;
005
006import org.apache.logging.log4j.LogManager;
007import org.apache.logging.log4j.Logger;
008
009import com.thetransactioncompany.util.PropertyRetriever;
010import com.thetransactioncompany.util.PropertyParseException;
011
012
013/**
014 * Details of a custom key store for client X.509 certificates to be presented 
015 * to a remote server.
016 *
017 * <p>Supports Log4j logging, see {@link #log}.
018 *
019 * <p>Property keys: [prefix]*
020 */
021public class CustomKeyStoreConfiguration
022        implements LoggableConfiguration {
023
024
025        /**
026         * If {@code true} a custom key store file must be used for the client 
027         * X.509 certificates to be presented to the remote server (if such 
028         * authentication is required).
029         *
030         * <p>If {@code false} the default key store will be used (if one has 
031         * been provided and correctly configured).
032         *
033         * <p>Property key: [prefix]enable
034         */
035        public final boolean enable;
036
037
038        /**
039         * The file system location of the custom key store file.
040         *
041         * <p>Property key: [prefix]file
042         */
043        public final String file;
044
045
046        /**
047         * The type of the custom key store file, typically "JKS" or "PKCS12". 
048         * An empty or {@code null} string indicates to use the system default 
049         * type.
050         *
051         * <p>Property key: [prefix]type
052         */
053        public final String type;
054
055
056        /**
057         * The password to unlock the custom key store file. An empty or 
058         * {@code null} string indicates that no password is required.
059         *
060         * <p>Property key: [prefix]password
061         */
062        public final String password;
063
064
065        /**
066         * The logger.
067         */
068        private final Logger log = LogManager.getLogger(LOG_CATEGORY);
069
070
071        /**
072         * Creates a new custom key store configuration from the specified 
073         * properties.
074         *
075         * <p>Mandatory properties:
076         *
077         * <ul>
078         *     <li>none
079         * </ul>
080         *
081         * <p>Conditionally mandatory properties:
082         *
083         * <ul>
084         *     <li>[prefix]file - if the key store is enabled
085         * </ul>
086         *
087         * <p>Optional properties, with defaults:
088         *
089         * <ul>
090         *     <li>[prefix]enable = false
091         *     <li>[prefix]type = null
092         *     <li>[prefix]password = null
093         * </ul>
094         *
095         * @param prefix The properties prefix. Must not be {@code null}.
096         * @param props  The properties. Must not be {@code null}.
097         *
098         * @throws PropertyParseException On a missing or invalid property.
099         */
100        public CustomKeyStoreConfiguration(final String prefix, final Properties props)
101                throws PropertyParseException {
102
103                PropertyRetriever pr = new PropertyRetriever(props);
104
105                enable = pr.getOptBoolean(prefix + "enable", false);
106
107                if (enable) {
108                        file = pr.getString(prefix + "file");
109                        type = pr.getOptString(prefix + "type", null);
110                        password = pr.getOptString(prefix + "password", null);
111                }
112                else {
113                        file = null;
114                        type = null;
115                        password = null;
116                }
117        }
118
119
120        /**
121         * Logs the configuration details at INFO level using Log4j.
122         */
123        @Override
124        public void log() {
125
126                log.info("Custom key store enabled: {}", enable);
127
128                if (enable) {
129                        log.info("Custom key store file: {}", file);
130                        log.info("Custom key store type: {}", type);
131                }
132        }
133}