001package com.nimbusds.common.config;
002
003
004import java.util.Properties;
005
006import com.thetransactioncompany.util.PropertyParseException;
007import com.thetransactioncompany.util.PropertyRetriever;
008import com.unboundid.ldap.sdk.DN;
009import com.unboundid.ldap.sdk.LDAPException;
010import org.apache.logging.log4j.LogManager;
011import org.apache.logging.log4j.Logger;
012
013
014/**
015 * Directory user, with DN and password.
016 *
017 * <p>The configuration is stored as public fields which become immutable 
018 * (final) after their initialisation.
019 *
020 * <p>Property keys: [prefix]*
021 */
022public class DirectoryUser implements LoggableConfiguration {
023
024
025        /**
026         * The distinguished name (DN) of the directory user DN. A 
027         * {@code DN.NULL_DN} value represents an anonymous user.
028         *
029         * <p>Property key: [prefix]dn
030         */
031        public final DN dn;
032
033
034        /**
035         * The directory user password. An empty string represents an anonymous 
036         * user.
037         *
038         * <p>Property key: [prefix]password
039         */
040        public final String password;
041
042
043        /**
044         * Creates a new directory user from the specified properties.
045         *
046         * <p>Mandatory properties:
047         *
048         * <ul>
049         *     <li>[prefix]dn
050         *     <li>[prefix]password
051         * </ul>
052         *
053         * @param prefix The properties prefix. Must not be {@code null}.
054         * @param props  The properties. Must not be {@code null}.
055         *
056         * @throws PropertyParseException On a missing or invalid property.
057         */
058        public DirectoryUser(final String prefix, final Properties props)
059                throws PropertyParseException {
060
061                PropertyRetriever pr = new PropertyRetriever(props);
062                
063                String dnString = pr.getString(prefix + "dn");
064
065                if (dnString.isEmpty()) {
066
067                        dn = DN.NULL_DN;
068                }
069                else {
070                        try {
071                                dn = new DN(dnString);
072                                
073                        } catch (LDAPException e) {
074
075                                throw new PropertyParseException("Invalid DN", prefix + "dn", dnString);
076                        }
077                }
078
079                password = pr.getString(prefix + "password");
080        }
081
082
083        /** 
084         * Creates a new directory user.
085         *
086         * @param dn       The distinguished name (DN) of the directory user 
087         *                 DN. A {@code DN.NULL_DN} value represents an 
088         *                 anonymous user. Must not be {@code null}.
089         * @param password The directory user password. An empty string 
090         *                 represents an anonymous user.
091         */
092        public DirectoryUser(final DN dn, final String password) {
093
094                if (dn == null)
095                        throw new IllegalArgumentException("The directory user DN must not be null");
096
097                this.dn = dn;
098
099                if (password == null)
100                        throw new IllegalArgumentException("The directory user password must not be null");
101
102                this.password = password;
103        }
104
105
106        /**
107         * Logs the configuration details at INFO level.
108         */
109        @Override
110        public void log() {
111
112                Logger log = LogManager.getLogger(LOG_CATEGORY);
113
114                if (dn.equals(DN.NULL_DN))
115                        log.info("[CM1050] Directory user DN: [anonymous]");
116                else
117                        log.info("[CM1050] Directory user DN: {}", dn);
118        }
119}