001/*******************************************************************************
002 * Copyright 2018 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        private boolean allowCompleteDeviceCodeUri = false;
073
074        public ConfigurationPropertiesBean() {
075
076        }
077
078        /**
079         * Endpoints protected by TLS must have https scheme in the URI.
080         * @throws HttpsUrlRequiredException
081         */
082        @PostConstruct
083        public void checkConfigConsistency() {
084                if (!StringUtils.startsWithIgnoreCase(issuer, "https")) {
085                        if (this.forceHttps) {
086                                logger.error("Configured issuer url is not using https scheme. Server will be shut down!");
087                                throw new BeanCreationException("Issuer is not using https scheme as required: " + issuer);
088                        }
089                        else {
090                                logger.warn("\n\n**\n** WARNING: Configured issuer url is not using https scheme.\n**\n\n");
091                        }
092                }
093
094                if (languageNamespaces == null || languageNamespaces.isEmpty()) {
095                        logger.error("No configured language namespaces! Text rendering will fail!");
096                }
097        }
098
099        /**
100         * @return the issuer baseUrl
101         */
102        public String getIssuer() {
103                return issuer;
104        }
105
106        /**
107         * @param iss the issuer to set
108         */
109        public void setIssuer(String iss) {
110                issuer = iss;
111        }
112
113        /**
114         * @return the topbarTitle
115         */
116        public String getTopbarTitle() {
117                return topbarTitle;
118        }
119
120        /**
121         * @param topbarTitle the topbarTitle to set
122         */
123        public void setTopbarTitle(String topbarTitle) {
124                this.topbarTitle = topbarTitle;
125        }
126
127        /**
128         * @return If shortTopbarTitle is undefined, returns topbarTitle.
129         */
130        public String getShortTopbarTitle() {
131                return shortTopbarTitle == null ? topbarTitle : shortTopbarTitle;
132        }
133
134        public void setShortTopbarTitle(String shortTopbarTitle) {
135                this.shortTopbarTitle = shortTopbarTitle;
136        }
137
138        /**
139         * @return the logoImageUrl
140         */
141        public String getLogoImageUrl() {
142                return logoImageUrl;
143        }
144
145        /**
146         * @param logoImageUrl the logoImageUrl to set
147         */
148        public void setLogoImageUrl(String logoImageUrl) {
149                this.logoImageUrl = logoImageUrl;
150        }
151
152        /**
153         * @return the regTokenLifeTime
154         */
155        public Long getRegTokenLifeTime() {
156                return regTokenLifeTime;
157        }
158
159        /**
160         * @param regTokenLifeTime the registration token lifetime to set in seconds
161         */
162        public void setRegTokenLifeTime(Long regTokenLifeTime) {
163                this.regTokenLifeTime = regTokenLifeTime;
164        }
165
166        /**
167         * @return the rqpTokenLifeTime
168         */
169        public Long getRqpTokenLifeTime() {
170                return rqpTokenLifeTime;
171        }
172
173        /**
174         * @param rqpTokenLifeTime the rqpTokenLifeTime to set
175         */
176        public void setRqpTokenLifeTime(Long rqpTokenLifeTime) {
177                this.rqpTokenLifeTime = rqpTokenLifeTime;
178        }
179
180        public boolean isForceHttps() {
181                return forceHttps;
182        }
183
184        public void setForceHttps(boolean forceHttps) {
185                this.forceHttps = forceHttps;
186        }
187
188        /**
189         * @return the locale
190         */
191        public Locale getLocale() {
192                return locale;
193        }
194
195        /**
196         * @param locale the locale to set
197         */
198        public void setLocale(Locale locale) {
199                this.locale = locale;
200        }
201
202        /**
203         * @return the languageNamespaces
204         */
205        public List<String> getLanguageNamespaces() {
206                return languageNamespaces;
207        }
208
209        /**
210         * @param languageNamespaces the languageNamespaces to set
211         */
212        public void setLanguageNamespaces(List<String> languageNamespaces) {
213                this.languageNamespaces = languageNamespaces;
214        }
215
216        /**
217         * @return true if dual client is configured, otherwise false
218         */
219        public boolean isDualClient() {
220                if (isHeartMode()) {
221                        return false; // HEART mode is incompatible with dual client mode
222                } else {
223                        return dualClient;
224                }
225        }
226
227        /**
228         * @param dualClient the dual client configuration
229         */
230        public void setDualClient(boolean dualClient) {
231                this.dualClient = dualClient;
232        }
233
234        /**
235         * Get the list of namespaces as a JSON string, for injection into the JavaScript UI
236         * @return
237         */
238        public String getLanguageNamespacesString() {
239                return new Gson().toJson(getLanguageNamespaces());
240        }
241
242        /**
243         * Get the default namespace (first in the nonempty list)
244         */
245        public String getDefaultLanguageNamespace() {
246                return getLanguageNamespaces().get(0);
247        }
248
249        /**
250         * @return the heartMode
251         */
252        public boolean isHeartMode() {
253                return heartMode;
254        }
255
256        /**
257         * @param heartMode the heartMode to set
258         */
259        public void setHeartMode(boolean heartMode) {
260                this.heartMode = heartMode;
261        }
262
263        /**
264         * @return the allowCompleteDeviceCodeUri
265         */
266        public boolean isAllowCompleteDeviceCodeUri() {
267                return allowCompleteDeviceCodeUri;
268        }
269
270        /**
271         * @param allowCompleteDeviceCodeUri the allowCompleteDeviceCodeUri to set
272         */
273        public void setAllowCompleteDeviceCodeUri(boolean allowCompleteDeviceCodeUri) {
274                this.allowCompleteDeviceCodeUri = allowCompleteDeviceCodeUri;
275        }
276}