View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.apache.myfaces.tobago.internal.util.RandomUtils;
23  import org.apache.myfaces.tobago.portlet.PortletUtils;
24  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
27  
28  import javax.faces.context.FacesContext;
29  import javax.portlet.PortletSession;
30  import javax.servlet.http.HttpSession;
31  import java.io.IOException;
32  import java.io.Serializable;
33  import java.util.Map;
34  
35  public final class Secret implements Serializable {
36  
37    private static final long serialVersionUID = 1L;
38  
39    private static final String KEY = Secret.class.getName();
40  
41    private String secret;
42  
43    private Secret() {
44      secret = RandomUtils.nextString();
45    }
46  
47    /**
48     * Checks that the request contains a parameter {@link org.apache.myfaces.tobago.webapp.Secret#KEY}
49     * which is equals to a secret value in the session.
50     */
51    public static boolean check(final FacesContext facesContext) {
52      final Map requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
53      final String fromRequest = (String) requestParameterMap.get(Secret.KEY);
54      final Object session = facesContext.getExternalContext().getSession(false);
55      Secret secret = getSecret(session);
56      return secret != null && secret.secret.equals(fromRequest);
57    }
58  
59    private static Secret getSecret(Object session) {
60      Secret secret = null;
61      if (session!=null) {
62        if (session instanceof HttpSession) {
63          secret = (Secret) ((HttpSession) session).getAttribute(Secret.KEY);
64        } else if (PortletUtils.isPortletApiAvailable() && session instanceof PortletSession) {
65          secret = (Secret) ((PortletSession) session).getAttribute(Secret.KEY, PortletSession.APPLICATION_SCOPE);
66        } else {
67          throw new IllegalArgumentException("Unknown session type: " + session);
68        }
69      }
70      return secret;
71    }
72  
73    /**
74     * Encode a hidden field with the secret value from the session.
75     */
76    public static void encode(final FacesContext facesContext, final TobagoResponseWriter writer) throws IOException {
77      writer.startElement(HtmlElements.INPUT);
78      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
79      writer.writeAttribute(HtmlAttributes.NAME, Secret.KEY, false);
80      writer.writeAttribute(HtmlAttributes.ID, Secret.KEY, false);
81      final Object session = facesContext.getExternalContext().getSession(true);
82      final Secret secret = getSecret(session);
83      if (secret != null) {
84        writer.writeAttribute(HtmlAttributes.VALUE, secret.secret, false);
85      }
86      writer.endElement(HtmlElements.INPUT);
87    }
88  
89    /**
90     * Create a secret attribute in the session.
91     * Should usually be called in a {@link javax.servlet.http.HttpSessionListener}.
92     */
93    public static void create(final HttpSession session) {
94      session.setAttribute(Secret.KEY, new Secret());
95    }
96  }