org.apache.wicket.core.request.mapper
Class CryptoMapper

java.lang.Object
  extended by org.apache.wicket.core.request.mapper.CryptoMapper
All Implemented Interfaces:
IRequestMapper, IRequestMapperDelegate

public class CryptoMapper
extends Object
implements IRequestMapperDelegate

A request mapper that encrypts URLs generated by another mapper. This mapper encrypts the segments and query parameters of URLs starting with IMapperContext.getNamespace(), and just the PageComponentInfo parameter for mounted URLs.

Important: for better security it is recommended to use CryptoMapper(org.apache.wicket.request.IRequestMapper, org.apache.wicket.util.IProvider) constructor with ICrypt implementation that generates a separate key for each user. KeyInSessionSunJceCryptFactory provides such an implementation that stores the key in the HTTP session.

This mapper can be mounted before or after mounting other pages, but will only encrypt URLs for pages mounted before the CryptoMapper. If required, multiple CryptoMappers may be installed in an Application.

When encrypting URLs in the Wicket namespace (starting with IMapperContext.getNamespace()), the entire URL, including segments and parameters, is encrypted, with the encrypted form stored in the first segment of the encrypted URL.

To be able to handle relative URLs, like for image URLs in a CSS file, checksum segments are appended to the encrypted URL until the encrypted URL has the same number of segments as the original URL had. Each checksum segment has a precise 5 character value, calculated using a checksum. This helps in calculating the relative distance from the original URL. When a URL is returned by the browser, we iterate through these checksummed placeholder URL segments. If the segment matches the expected checksum, then the segment is deemed to be the corresponding segment in the original URL. If the segment does not match the expected checksum, then the segment is deemed a plain text sibling of the corresponding segment in the original URL, and all subsequent segments are considered plain text children of the current segment.

When encrypting mounted URLs, we look for the PageComponentInfo parameter, and encrypt only that parameter.

CryptoMapper can be configured to mark encrypted URLs as encrypted, and throw a PageExpiredException exception if a encrypted URL cannot be decrypted. This can occur when using KeyInSessionSunJceCryptFactory, and the session has expired.

Author:
igor.vaynberg, Jesse Long, svenmeier
See Also:
ISecuritySettings.setCryptFactory(org.apache.wicket.util.crypt.ICryptFactory), KeyInSessionSunJceCryptFactory, SunJceCrypt

Nested Class Summary
static class CryptoMapper.HashedSegmentGenerator
          A generator of hashed segments.
 
Constructor Summary
CryptoMapper(IRequestMapper wrappedMapper, Application application)
          Encrypt with ISecuritySettings.getCryptFactory().
CryptoMapper(IRequestMapper wrappedMapper, IProvider<ICrypt> cryptProvider)
          Construct.
 
Method Summary
protected  Url decryptEntireUrl(Request request, Url encryptedUrl)
          Decrypts an entire URL, which was previously encrypted by encryptEntireUrl(org.apache.wicket.request.Url).
protected  Url decryptRequestListenerParameter(Request request, Url encryptedUrl)
          Decrypts a URL which may contain an encrypted PageComponentInfo query parameter.
protected  Url decryptUrl(Request request, Url encryptedUrl)
          Decrypts a Url.
protected  Url encryptEntireUrl(Url url)
          Encrypts an entire URL, segments and query parameters.
protected  Url encryptRequestListenerParameter(Url url)
          Encrypts the PageComponentInfo query parameter in the URL, if any is found.
protected  Url encryptUrl(Url url)
          Encrypts a URL.
 int getCompatibilityScore(Request request)
          
protected  IMapperContext getContext()
          Returns the applications IMapperContext.
protected  ICrypt getCrypt()
           
 IRequestMapper getDelegateMapper()
           
 boolean getMarkEncryptedUrls()
          Whether or not to mark encrypted URLs as encrypted.
protected  IRequestMapper getWrappedMapper()
          Deprecated. Since 6.18.0, to be removed in 7.0.0. Use getDelegateMapper() instead.
 Url mapHandler(IRequestHandler requestHandler)
           
 IRequestHandler mapRequest(Request request)
           
 CryptoMapper setMarkEncryptedUrls(boolean markEncryptedUrls)
          Sets whether or not to mark encrypted URLs as encrypted.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CryptoMapper

public CryptoMapper(IRequestMapper wrappedMapper,
                    Application application)
Encrypt with ISecuritySettings.getCryptFactory().

Important: Encryption is done with ISecuritySettings.DEFAULT_ENCRYPTION_KEY if you haven't configured an alternative ICryptFactory. For better security it is recommended to use CryptoMapper(IRequestMapper, IProvider) with a specific ICrypt implementation that generates a separate key for each user. KeyInSessionSunJceCryptFactory provides such an implementation that stores the key in the HTTP session.

Parameters:
wrappedMapper - the non-crypted request mapper
application - the current application
See Also:
SunJceCrypt

CryptoMapper

public CryptoMapper(IRequestMapper wrappedMapper,
                    IProvider<ICrypt> cryptProvider)
Construct.

Parameters:
wrappedMapper - the non-crypted request mapper
cryptProvider - the custom crypt provider
Method Detail

getMarkEncryptedUrls

public boolean getMarkEncryptedUrls()
Whether or not to mark encrypted URLs as encrypted. If set, a PageExpiredException is thrown when a encrypted URL can no longer be decrypted.

Returns:
whether or not to mark encrypted URLs as encrypted.

setMarkEncryptedUrls

public CryptoMapper setMarkEncryptedUrls(boolean markEncryptedUrls)
Sets whether or not to mark encrypted URLs as encrypted. If set, a PageExpiredException is thrown when a encrypted URL can no longer be decrypted.

Parameters:
markEncryptedUrls - whether or not to mark encrypted URLs as encrypted.
Returns:
this, for chaining.

getCompatibilityScore

public int getCompatibilityScore(Request request)

This implementation decrypts the URL and passes the decrypted URL to the wrapped mapper.

Specified by:
getCompatibilityScore in interface IRequestMapper
Parameters:
request - The request for which to get a compatibility score.
Returns:
The compatibility score.

mapHandler

public Url mapHandler(IRequestHandler requestHandler)
Specified by:
mapHandler in interface IRequestMapper

mapRequest

public IRequestHandler mapRequest(Request request)
Specified by:
mapRequest in interface IRequestMapper

getCrypt

protected final ICrypt getCrypt()
Returns:
the ICrypt implementation that may be used to encrypt/decrypt Url's segments and/or query string

getWrappedMapper

@Deprecated
protected final IRequestMapper getWrappedMapper()
Deprecated. Since 6.18.0, to be removed in 7.0.0. Use getDelegateMapper() instead.

Returns:
the wrapped root request mapper

getDelegateMapper

public final IRequestMapper getDelegateMapper()
Specified by:
getDelegateMapper in interface IRequestMapperDelegate
Returns:
the wrapped root request mapper

getContext

protected IMapperContext getContext()
Returns the applications IMapperContext.

Returns:
The applications IMapperContext.

encryptUrl

protected Url encryptUrl(Url url)
Encrypts a URL. This method should return a new, encrypted instance of the URL. If the URL starts with /wicket/, the entire URL is encrypted.

Parameters:
url - The URL to encrypt.
Returns:
A new, encrypted version of the URL.

encryptEntireUrl

protected Url encryptEntireUrl(Url url)
Encrypts an entire URL, segments and query parameters.

Parameters:
url - The URL to encrypt.
Returns:
An encrypted form of the URL.

encryptRequestListenerParameter

protected Url encryptRequestListenerParameter(Url url)
Encrypts the PageComponentInfo query parameter in the URL, if any is found.

Parameters:
url - The URL to encrypt.
Returns:
An encrypted form of the URL.

decryptUrl

protected Url decryptUrl(Request request,
                         Url encryptedUrl)
Decrypts a Url. This method should return null if the URL is not decryptable, or if the URL should have been encrypted but was not. Returning null results in a 404 error.

Parameters:
request - The Request.
encryptedUrl - The encrypted Url.
Returns:
Returns a decrypted Url.

decryptEntireUrl

protected Url decryptEntireUrl(Request request,
                               Url encryptedUrl)
Decrypts an entire URL, which was previously encrypted by encryptEntireUrl(org.apache.wicket.request.Url). This method should return null if the URL is not decryptable.

Parameters:
request - The request that was made.
encryptedUrl - The encrypted URL.
Returns:
A decrypted form of the URL, or null if the URL is not decryptable.

decryptRequestListenerParameter

protected Url decryptRequestListenerParameter(Request request,
                                              Url encryptedUrl)
Decrypts a URL which may contain an encrypted PageComponentInfo query parameter.

Parameters:
request - The request that was made.
encryptedUrl - The (potentially) encrypted URL.
Returns:
A decrypted form of the URL.


Copyright © 2006–2016 Apache Software Foundation. All rights reserved.