1    /*
2     * Copyright 2006 :torweg free software group
3     * 
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.accesscontrol.authentication;
19   
20   import java.util.Collection;
21   import java.util.Collections;
22   import java.util.HashMap;
23   import java.util.HashSet;
24   import java.util.List;
25   import java.util.Map;
26   import java.util.Set;
27   
28   import org.jdom.Element;
29   import org.torweg.pulse.configuration.AbstractConfigBean;
30   import org.torweg.pulse.configuration.ConfigurationException;
31   
32   /**
33    * The configuration of the {@code Authentication}.
34    * 
35    * @author Christian Schatt
36    * @version $Revision: 1380 $
37    * 
38    */
39   public class AuthenticationConfig extends AbstractConfigBean {
40   
41       /**
42        * The serialVersionUID of this {@code AuthenticationConfig}.
43        */
44       private static final long serialVersionUID = -1086986185169763274L;
45   
46       /**
47        * The name-mappings for the httpParameters.
48        */
49       private final Map<String, String> parameterMappings = new HashMap<String, String>();
50   
51       /**
52        * The login-modes provided by the {@code Authentication}.
53        */
54       private final Set<String> loginModes = new HashSet<String>();
55   
56       /**
57        * The names of actions which will not redirect to last view on logout.
58        */
59       private final Set<String> redirectToHompageActions = new HashSet<String>();
60   
61       /**
62        * The error-codes of the {@code Authentication}.
63        */
64       private final Map<Integer, String> errorCodes = new HashMap<Integer, String>();
65   
66       /**
67        * the single sign on tasks.
68        */
69       private final Set<Class<ISingleSignOnTask>> singleSignOnTasks = new HashSet<Class<ISingleSignOnTask>>();
70   
71       /**
72        * checks if the passed action-name is a redirect-to-homepage-action.
73        * 
74        * @param actionName
75        *            the name to check
76        * 
77        * @return true if the passed actionName is a redirect-to-homepage-action,
78        *         false otherwise
79        */
80       public final boolean isRedirectToHompageAction(final String actionName) {
81           return this.redirectToHompageActions.contains(actionName);
82       }
83   
84       /**
85        * Initializes the configuration of the {@code Authentication}.
86        * 
87        * @param conf
88        *            The JDOM-{@code Element} containing the configuration of
89        *            the {@code Authentication}.
90        * 
91        * @see org.torweg.pulse.configuration.ConfigBean#init(org.jdom.Element)
92        */
93       @SuppressWarnings("unchecked")
94       public final void init(final Element conf) {
95           List<Element> list = (List<Element>) conf
96                   .getChild("parameter-mappings").getChildren();
97           for (Element mapping : list) {
98               this.parameterMappings.put(mapping.getName(), mapping
99                       .getAttributeValue("name"));
100          }
101          list = (List<Element>) conf.getChild("login-modes").getChildren(
102                  "login-mode");
103          for (Element mode : list) {
104              this.loginModes.add(mode.getAttributeValue("name"));
105          }
106          list = (List<Element>) conf.getChild("error-codes").getChildren(
107                  "error-code");
108          for (Element code : list) {
109              this.errorCodes.put(Integer
110                      .valueOf(code.getAttributeValue("value")), code
111                      .getAttributeValue("description"));
112          }
113          list = (List<Element>) conf.getChild("redirect-to-homepage-on-logout")
114                  .getChildren("action");
115          for (Element action : list) {
116              this.redirectToHompageActions.add(action.getAttributeValue("name"));
117          }
118          /* process single sign on tasks */
119          Element ssoEl = conf.getChild("single-sign-on");
120          if (ssoEl != null) {
121              list = (List<Element>) ssoEl.getChildren("task");
122              for (Element t : list) {
123                  try {
124                      Class<ISingleSignOnTask> clazz = (Class<ISingleSignOnTask>) Class
125                              .forName(t.getAttributeValue("class"));
126                      this.singleSignOnTasks
127                              .add((Class<ISingleSignOnTask>) clazz);
128                  } catch (ClassNotFoundException e) {
129                      throw new ConfigurationException(e.getLocalizedMessage(), e);
130                  } catch (ClassCastException e) {
131                      throw new ConfigurationException(e.getLocalizedMessage(), e);
132                  }
133              }
134          }
135      }
136  
137      /**
138       * Returns the name-mappings for the httpParameters provided by the
139       * {@code Authentication}.
140       * 
141       * @return the name-mappings for the httpParameters provided by the
142       *         {@code Authentication}
143       */
144      protected final Map<String, String> getParameterMappings() {
145          return this.parameterMappings;
146      }
147  
148      /**
149       * Returns the login-modes provided by the {@code Authentication}.
150       * 
151       * @return the login-modes provided by the {@code Authentication}
152       */
153      protected final Set<String> getLoginModes() {
154          return this.loginModes;
155      }
156  
157      /**
158       * Returns the error-codes provided by the {@code Authentication}.
159       * 
160       * @return the error-codes provided by the {@code Authentication}
161       */
162      protected final Map<Integer, String> getErrorCodes() {
163          return this.errorCodes;
164      }
165  
166      /**
167       * indicates whether single-sign-on is activated.
168       * 
169       * @return {@code true}, if the authentication is configured for
170       *         single-sign-on. Otherwise {@code false}.
171       */
172      protected final boolean isSingleSignOn() {
173          return this.singleSignOnTasks.isEmpty() ^ true;
174      }
175  
176      /**
177       * returns an unmodifiable collection of all {@code ISingleSignOnTask}s
178       * configured.
179       * 
180       * @return an unmodifiable view of all {@code ISingleSignOnTask}s
181       */
182      public final Collection<Class<ISingleSignOnTask>> getSingleSignOnTasks() {
183          return Collections.unmodifiableCollection(this.singleSignOnTasks);
184      }
185  
186  }
187