001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016
017package org.forgerock.openam.selfservice.config.beans;
018
019import org.forgerock.openam.sm.config.ConfigAttribute;
020import org.forgerock.openam.sm.config.ConfigSource;
021import org.forgerock.util.Reject;
022
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Locale;
026import java.util.Map;
027import java.util.Set;
028
029/**
030 * Represents forgotten username console configuration.
031 *
032 * @supported.all.api
033 * @since 13.0.0
034 */
035public final class ForgottenUsernameConsoleConfig extends CommonConsoleConfig {
036
037    private final int minimumAnswersToVerify;
038    private final boolean showUsernameEnabled;
039    private final boolean enabled;
040    private final String configProviderClass;
041    private final long tokenExpiry;
042    private final boolean emailEnabled;
043    private final Map<Locale, String> subjectTranslations;
044    private final Map<Locale, String> messageTranslations;
045    private final boolean captchaEnabled;
046    private final boolean kbaEnabled;
047    private final Set<String> validQueryAttributes;
048
049    private ForgottenUsernameConsoleConfig(ForgottenUsernameBuilder builder) {
050        super(builder);
051        minimumAnswersToVerify = builder.minimumAnswersToVerify;
052        showUsernameEnabled = builder.showUsernameEnabled;
053        configProviderClass = builder.configProviderClass;
054        enabled = builder.enabled;
055        emailEnabled = builder.emailEnabled;
056        tokenExpiry = builder.tokenExpiry;
057        captchaEnabled = builder.captchaEnabled;
058        kbaEnabled = builder.kbaEnabled;
059        subjectTranslations = builder.subjectTranslations;
060        messageTranslations = builder.messageTranslations;
061        validQueryAttributes = builder.validQueryAttributes;
062    }
063
064    @Override
065    public String getConfigProviderClass() {
066        return configProviderClass;
067    }
068
069    /**
070     * Whether the service is enabled.
071     *
072     * @return whether the service is enabled
073     */
074    public boolean isEnabled() {
075        return enabled;
076    }
077
078    /**
079     * Whether email verification is enabled.
080     *
081     * @return whether email verification is enabled
082     */
083    public boolean isEmailEnabled() {
084        return emailEnabled;
085    }
086
087    /**
088     * Gets the token expiry time in seconds.
089     *
090     * @return the token expiry time
091     */
092    public long getTokenExpiry() {
093        return tokenExpiry;
094    }
095
096    /**
097     * Whether the captcha stage is enabled.
098     *
099     * @return whether the captcha stage is enabled
100     */
101    public boolean isCaptchaEnabled() {
102        return captchaEnabled;
103    }
104
105    /**
106     * Whether the KBA stage is enabled.
107     *
108     * @return whether the KBA stage is enabled
109     */
110    public boolean isKbaEnabled() {
111        return kbaEnabled;
112    }
113
114    /**
115     * Gets the map of locales to subject strings.
116     *
117     * @return the map of locales to subject text strings.
118     */
119    public Map<Locale, String> getSubjectTranslations() {
120        return subjectTranslations;
121    }
122
123    /**
124     * Gets the map of locales to email body text strings.
125     *
126     * @return the map of locales to email body text strings.
127     */
128    public Map<Locale, String> getMessageTranslations() {
129        return messageTranslations;
130    }
131
132    /**
133     * Get the minimum count of questions to verify.
134     *
135     * @return minimum count
136     */
137    public int getMinimumAnswersToVerify() {
138        return minimumAnswersToVerify;
139    }
140
141    /**
142     * Whether or the not the username should be displayed.
143     *
144     * @return whether username should be shown
145     */
146    public boolean isShowUsernameEnabled() {
147        return showUsernameEnabled;
148    }
149
150    /**
151     * Get set of valid query attributes.
152     *
153     * @return valid query attributes
154     */
155    public Set<String> getValidQueryAttributes() {
156        return validQueryAttributes;
157    }
158
159    /**
160     * Builder for {@link ForgottenUsernameConsoleConfig}.
161     */
162    @ConfigSource({"MailServer", "selfService"})
163    public static final class ForgottenUsernameBuilder
164            extends CommonConsoleConfigBuilder<ForgottenUsernameConsoleConfig> {
165
166        private int minimumAnswersToVerify;
167        private boolean showUsernameEnabled;
168        private boolean enabled;
169        private String configProviderClass;
170        private long tokenExpiry;
171        private boolean emailEnabled;
172        private final Map<Locale, String> subjectTranslations;
173        private final Map<Locale, String> messageTranslations;
174        private boolean captchaEnabled;
175        private boolean kbaEnabled;
176        private final Set<String> validQueryAttributes;
177
178        /**
179         * Constructs a new forgotten username builder.
180         */
181        public ForgottenUsernameBuilder() {
182            subjectTranslations = new HashMap<>();
183            messageTranslations = new HashMap<>();
184            validQueryAttributes = new HashSet<>();
185        }
186
187        /**
188         * Sets whether the service is enabled.
189         *
190         * @param enabled
191         *         whether the service is enabled
192         */
193        @ConfigAttribute("selfServiceForgottenUsernameEnabled")
194        public void setEnabled(boolean enabled) {
195            this.enabled = enabled;
196        }
197
198        /**
199         * Sets the config provider class.
200         *
201         * @param configProviderClass
202         *         config provider class
203         */
204        @ConfigAttribute("selfServiceForgottenUsernameServiceConfigClass")
205        public void setConfigProviderClass(String configProviderClass) {
206            this.configProviderClass = configProviderClass;
207        }
208
209        /**
210         * Sets the token expiry time.
211         *
212         * @param tokenExpiry
213         *         token expiry time
214         */
215        @ConfigAttribute("selfServiceForgottenUsernameTokenTTL")
216        public void setTokenExpiry(long tokenExpiry) {
217            this.tokenExpiry = tokenExpiry;
218        }
219
220        /**
221         * Sets whether email is enabled.
222         *
223         * @param emailEnabled
224         *         whether email is enabled
225         */
226        @ConfigAttribute("selfServiceForgottenUsernameEmailUsernameEnabled")
227        public void setEmailEnabled(boolean emailEnabled) {
228            this.emailEnabled = emailEnabled;
229        }
230
231        /**
232         * Sets the email subject translations.
233         *
234         * @param subjectTranslations
235         *         email subject translations
236         */
237        @ConfigAttribute(value = "selfServiceForgottenUsernameEmailSubject",
238                transformer = LocaleMessageTransformer.class)
239        public void setSubjectTranslations(Map<Locale, String> subjectTranslations) {
240            this.subjectTranslations.putAll(subjectTranslations);
241        }
242
243        /**
244         * Sets the email body translations.
245         *
246         * @param messageTranslations
247         *         email body translations
248         */
249        @ConfigAttribute(value = "selfServiceForgottenUsernameEmailBody",
250                transformer = LocaleMessageTransformer.class)
251        public void setMessageTranslations(Map<Locale, String> messageTranslations) {
252            this.messageTranslations.putAll(messageTranslations);
253        }
254
255        /**
256         * Sets whether captcha is enabled.
257         *
258         * @param captchaEnabled
259         *         whether captcha is enabled
260         */
261        @ConfigAttribute("selfServiceForgottenUsernameCaptchaEnabled")
262        public void setCaptchaEnabled(boolean captchaEnabled) {
263            this.captchaEnabled = captchaEnabled;
264        }
265
266        /**
267         * Sets whether KBA is enabled.
268         *
269         * @param kbaEnabled
270         *         whether KBA is enabled
271         */
272        @ConfigAttribute("selfServiceForgottenUsernameKbaEnabled")
273        public void setKbaEnabled(boolean kbaEnabled) {
274            this.kbaEnabled = kbaEnabled;
275        }
276
277        /**
278         * Sets the minimum number of answers to be verified.
279         *
280         * @param minimumAnswersToVerify
281         *         minimum number of answers to be verified
282         */
283        @ConfigAttribute("selfServiceMinimumAnswersToVerify")
284        public void setMinimumAnswersToVerify(int minimumAnswersToVerify) {
285            this.minimumAnswersToVerify = minimumAnswersToVerify;
286        }
287
288        /**
289         * Sets whether show username is enabled.
290         *
291         * @param showUsernameEnabled
292         *         whether show username is enabled
293         */
294        @ConfigAttribute("selfServiceForgottenUsernameShowUsernameEnabled")
295        public void setShowUsernameEnabled(boolean showUsernameEnabled) {
296            this.showUsernameEnabled = showUsernameEnabled;
297        }
298
299        /**
300         * Sets the set of valid query attributes.
301         *
302         * @param validQueryAttributes
303         *         valid query attributes
304         */
305        @ConfigAttribute("selfServiceValidQueryAttributes")
306        public void setValidQueryAttributes(Set<String> validQueryAttributes) {
307            this.validQueryAttributes.addAll(validQueryAttributes);
308        }
309
310        @Override
311        boolean isCaptchaEnabled() {
312            return captchaEnabled;
313        }
314
315        @Override
316        boolean isKbaEnabled() {
317            return kbaEnabled;
318        }
319
320        @Override
321        ForgottenUsernameConsoleConfig internalBuild() {
322            Reject.ifNull(configProviderClass, "Config provider class name required");
323            Reject.ifFalse(tokenExpiry > 0, "Token expiry must be greater than zero");
324
325            if (emailEnabled) {
326                Reject.ifTrue(subjectTranslations.isEmpty(), "Subject translations are required");
327                Reject.ifTrue(messageTranslations.isEmpty(), "Message translations are required");
328            }
329
330            if (kbaEnabled) {
331                Reject.ifFalse(minimumAnswersToVerify > 0, "Minimum questions to be verified must be greater than 0");
332            }
333
334            return new ForgottenUsernameConsoleConfig(this);
335        }
336
337    }
338
339}