/** Decrypt and verify the provided security token. */
 @Override
 public SecurityToken createToken(Map<String, String> tokenParameters)
     throws SecurityTokenException {
   String token = tokenParameters.get(SecurityTokenCodec.SECURITY_TOKEN_NAME);
   if (token == null || token.trim().length() == 0) {
     // No token is present, assume anonymous access
     return new AnonymousSecurityToken();
   }
   String[] fields = token.split(":");
   if (fields.length != 2) {
     throw new SecurityTokenException("Invalid security token " + token);
   }
   String container = fields[0];
   BlobCrypter crypter = crypters.get(container);
   if (crypter == null) {
     throw new SecurityTokenException("Unknown container " + token);
   }
   String domain = domains.get(container);
   String activeUrl = tokenParameters.get(SecurityTokenCodec.ACTIVE_URL_NAME);
   String crypted = fields[1];
   try {
     return ExoBlobCrypterSecurityToken.decrypt(crypter, container, domain, crypted, activeUrl);
   } catch (BlobCrypterException e) {
     throw new SecurityTokenException(e);
   }
 }
 protected String createToken(
     String gadgetURL, String owner, String viewer, Long moduleId, String container) {
   try {
     BlobCrypter crypter = getBlobCrypter(this.containerKey);
     ExoBlobCrypterSecurityToken t =
         new ExoBlobCrypterSecurityToken(crypter, container, (String) null);
     t.setAppUrl(gadgetURL);
     t.setModuleId(moduleId);
     t.setOwnerId(owner);
     t.setViewerId(viewer);
     t.setTrustedJson("trusted");
     String portalContainer = PortalContainer.getCurrentPortalContainerName();
     PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
     String url = portalRequestContext.getRequest().getRequestURL().toString();
     String hostName = url.substring(0, url.indexOf(portalRequestContext.getRequestContextPath()));
     t.setPortalContainer(portalContainer);
     t.setHostName(hostName);
     t.setPortalOwner(portalRequestContext.getPortalOwner());
     return t.encrypt();
   } catch (Exception e) {
     LOG.error("Failed to generate token for gadget " + gadgetURL + " for owner " + owner, e);
   }
   return null;
 }