Coverage Report - org.apache.myfaces.application.viewstate.SecureRandomCsrfSessionTokenFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
SecureRandomCsrfSessionTokenFactory
0%
0/19
0%
0/6
2
 
 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  
 package org.apache.myfaces.application.viewstate;
 20  
 
 21  
 import javax.faces.context.FacesContext;
 22  
 import org.apache.commons.codec.binary.Hex;
 23  
 import org.apache.myfaces.application.StateCache;
 24  
 import org.apache.myfaces.shared.util.WebConfigParamUtils;
 25  
 
 26  
 /**
 27  
  * This factory generate a key composed by a counter and a random number. The
 28  
  * counter ensures uniqueness, and the random number prevents guess the next
 29  
  * session token.
 30  
  * 
 31  
  * @since 2.2
 32  
  * @author Leonardo Uribe
 33  
  */
 34  
 class SecureRandomCsrfSessionTokenFactory extends CsrfSessionTokenFactory
 35  
 {
 36  
     private final SessionIdGenerator sessionIdGenerator;
 37  
     private final int length;
 38  
 
 39  
     public SecureRandomCsrfSessionTokenFactory(FacesContext facesContext)
 40  0
     {
 41  0
         length = WebConfigParamUtils.getIntegerInitParameter(
 42  
             facesContext.getExternalContext(), 
 43  
             StateCache.RANDOM_KEY_IN_CSRF_SESSION_TOKEN_LENGTH_PARAM, 
 44  
             StateCache.RANDOM_KEY_IN_CSRF_SESSION_TOKEN_LENGTH_PARAM_DEFAULT);
 45  0
         sessionIdGenerator = new SessionIdGenerator();
 46  0
         sessionIdGenerator.setSessionIdLength(length);
 47  0
         String secureRandomClass = WebConfigParamUtils.getStringInitParameter(
 48  
             facesContext.getExternalContext(), 
 49  
             StateCache.RANDOM_KEY_IN_CSRF_SESSION_TOKEN_SECURE_RANDOM_CLASS_PARAM);
 50  0
         if (secureRandomClass != null)
 51  
         {
 52  0
             sessionIdGenerator.setSecureRandomClass(secureRandomClass);
 53  
         }
 54  0
         String secureRandomProvider = WebConfigParamUtils.getStringInitParameter(
 55  
             facesContext.getExternalContext(), 
 56  
             StateCache.RANDOM_KEY_IN_CSRF_SESSION_TOKEN_SECURE_RANDOM_PROVIDER_PARAM);
 57  0
         if (secureRandomProvider != null)
 58  
         {
 59  0
             sessionIdGenerator.setSecureRandomProvider(secureRandomProvider);
 60  
         }
 61  0
         String secureRandomAlgorithm = WebConfigParamUtils.getStringInitParameter(
 62  
             facesContext.getExternalContext(), 
 63  
             StateCache.RANDOM_KEY_IN_CSRF_SESSION_TOKEN_SECURE_RANDOM_ALGORITM_PARAM);
 64  0
         if (secureRandomAlgorithm != null)
 65  
         {
 66  0
             sessionIdGenerator.setSecureRandomAlgorithm(secureRandomAlgorithm);
 67  
         }
 68  0
     }
 69  
 
 70  
     public byte[] generateKey(FacesContext facesContext)
 71  
     {
 72  0
         byte[] array = new byte[length];
 73  0
         sessionIdGenerator.getRandomBytes(array);
 74  0
         return array;
 75  
     }
 76  
 
 77  
     @Override
 78  
     public String createCryptographicallyStrongTokenFromSession(FacesContext context)
 79  
     {
 80  0
         byte[] key = generateKey(context);
 81  0
         return new String(Hex.encodeHex(key));
 82  
     }
 83  
 }