001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018package org.mitre.openid.connect.config;
019
020import java.util.List;
021import java.util.Locale;
022
023import javax.annotation.PostConstruct;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.springframework.beans.factory.BeanCreationException;
028import org.springframework.util.StringUtils;
029
030import com.google.common.collect.Lists;
031import com.google.gson.Gson;
032
033
034
035/**
036 * Bean to hold configuration information that must be injected into various parts
037 * of our application. Set all of the properties here, and autowire a reference
038 * to this bean if you need access to any configuration properties.
039 *
040 * @author AANGANES
041 *
042 */
043public class ConfigurationPropertiesBean {
044
045        /**
046         * Logger for this class
047         */
048        private static final Logger logger = LoggerFactory.getLogger(ConfigurationPropertiesBean.class);
049
050        private String issuer;
051
052        private String topbarTitle;
053
054        private String shortTopbarTitle;
055
056        private String logoImageUrl;
057
058        private Long regTokenLifeTime;
059
060        private Long rqpTokenLifeTime;
061
062        private boolean forceHttps = false; // by default we just log a warning for HTTPS deployment
063
064        private Locale locale = Locale.ENGLISH; // we default to the english translation
065
066        private List<String> languageNamespaces = Lists.newArrayList("messages");
067
068        private boolean dualClient = false;
069
070        private boolean heartMode = false;
071
072        public ConfigurationPropertiesBean() {
073
074        }
075
076        /**
077         * Endpoints protected by TLS must have https scheme in the URI.
078         * @throws HttpsUrlRequiredException
079         */
080        @PostConstruct
081        public void checkConfigConsistency() {
082                if (!StringUtils.startsWithIgnoreCase(issuer, "https")) {
083                        if (this.forceHttps) {
084                                logger.error("Configured issuer url is not using https scheme. Server will be shut down!");
085                                throw new BeanCreationException("Issuer is not using https scheme as required: " + issuer);
086                        }
087                        else {
088                                logger.warn("\n\n**\n** WARNING: Configured issuer url is not using https scheme.\n**\n\n");
089                        }
090                }
091
092                if (languageNamespaces == null || languageNamespaces.isEmpty()) {
093                        logger.error("No configured language namespaces! Text rendering will fail!");
094                }
095        }
096
097        /**
098         * @return the issuer baseUrl
099         */
100        public String getIssuer() {
101                return issuer;
102        }
103
104        /**
105         * @param iss the issuer to set
106         */
107        public void setIssuer(String iss) {
108                issuer = iss;
109        }
110
111        /**
112         * @return the topbarTitle
113         */
114        public String getTopbarTitle() {
115                return topbarTitle;
116        }
117
118        /**
119         * @param topbarTitle the topbarTitle to set
120         */
121        public void setTopbarTitle(String topbarTitle) {
122                this.topbarTitle = topbarTitle;
123        }
124
125        /**
126         * @return If shortTopbarTitle is undefined, returns topbarTitle.
127         */
128        public String getShortTopbarTitle() {
129                return shortTopbarTitle == null ? topbarTitle : shortTopbarTitle;
130        }
131
132        public void setShortTopbarTitle(String shortTopbarTitle) {
133                this.shortTopbarTitle = shortTopbarTitle;
134        }
135
136        /**
137         * @return the logoImageUrl
138         */
139        public String getLogoImageUrl() {
140                return logoImageUrl;
141        }
142
143        /**
144         * @param logoImageUrl the logoImageUrl to set
145         */
146        public void setLogoImageUrl(String logoImageUrl) {
147                this.logoImageUrl = logoImageUrl;
148        }
149
150        /**
151         * @return the regTokenLifeTime
152         */
153        public Long getRegTokenLifeTime() {
154                return regTokenLifeTime;
155        }
156
157        /**
158         * @param regTokenLifeTime the registration token lifetime to set in seconds
159         */
160        public void setRegTokenLifeTime(Long regTokenLifeTime) {
161                this.regTokenLifeTime = regTokenLifeTime;
162        }
163
164        /**
165         * @return the rqpTokenLifeTime
166         */
167        public Long getRqpTokenLifeTime() {
168                return rqpTokenLifeTime;
169        }
170
171        /**
172         * @param rqpTokenLifeTime the rqpTokenLifeTime to set
173         */
174        public void setRqpTokenLifeTime(Long rqpTokenLifeTime) {
175                this.rqpTokenLifeTime = rqpTokenLifeTime;
176        }
177
178        public boolean isForceHttps() {
179                return forceHttps;
180        }
181
182        public void setForceHttps(boolean forceHttps) {
183                this.forceHttps = forceHttps;
184        }
185
186        /**
187         * @return the locale
188         */
189        public Locale getLocale() {
190                return locale;
191        }
192
193        /**
194         * @param locale the locale to set
195         */
196        public void setLocale(Locale locale) {
197                this.locale = locale;
198        }
199
200        /**
201         * @return the languageNamespaces
202         */
203        public List<String> getLanguageNamespaces() {
204                return languageNamespaces;
205        }
206
207        /**
208         * @param languageNamespaces the languageNamespaces to set
209         */
210        public void setLanguageNamespaces(List<String> languageNamespaces) {
211                this.languageNamespaces = languageNamespaces;
212        }
213
214        /**
215         * @return true if dual client is configured, otherwise false
216         */
217        public boolean isDualClient() {
218                if (isHeartMode()) {
219                        return false; // HEART mode is incompatible with dual client mode
220                } else {
221                        return dualClient;
222                }
223        }
224
225        /**
226         * @param dualClient the dual client configuration
227         */
228        public void setDualClient(boolean dualClient) {
229                this.dualClient = dualClient;
230        }
231
232        /**
233         * Get the list of namespaces as a JSON string, for injection into the JavaScript UI
234         * @return
235         */
236        public String getLanguageNamespacesString() {
237                return new Gson().toJson(getLanguageNamespaces());
238        }
239
240        /**
241         * Get the default namespace (first in the nonempty list)
242         */
243        public String getDefaultLanguageNamespace() {
244                return getLanguageNamespaces().get(0);
245        }
246
247        /**
248         * @return the heartMode
249         */
250        public boolean isHeartMode() {
251                return heartMode;
252        }
253
254        /**
255         * @param heartMode the heartMode to set
256         */
257        public void setHeartMode(boolean heartMode) {
258                this.heartMode = heartMode;
259        }
260}