Skip to content

Commit

Permalink
convert crypto to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Aug 22, 2012
1 parent 3e9ab30 commit f4bcfe9
Show file tree
Hide file tree
Showing 17 changed files with 1,121 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ public void registerProviderInstance(Object provider, int bindingPriority, Class
}
clientRequestFilters.registerSingleton((ClientRequestFilter)provider, bindingPriority);
}
if (isA(provider, ClientRequestFilter.class, contracts))
if (isA(provider, ClientResponseFilter.class, contracts))
{
if (clientResponseFilters == null)
{
Expand Down
5 changes: 5 additions & 0 deletions jaxrs/security/resteasy-crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>resteasy-jaxrs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@

import org.jboss.resteasy.annotations.security.doseta.After;
import org.jboss.resteasy.annotations.security.doseta.Signed;
import org.jboss.resteasy.spi.interception.AcceptedByMethod;

import java.lang.reflect.Method;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public abstract class DigitalSigningHeaderDecorator implements AcceptedByMethod
public class AbstractDigitalSigningHeaderDecorator
{
protected Signed signed;

public boolean accept(Class declaring, Method method)
{
signed = method.getAnnotation(Signed.class);
if (signed == null)
{
signed = (Signed) declaring.getAnnotation(Signed.class);
}
return signed != null;
}

protected DKIMSignature createHeader(KeyRepository repository)
{
DKIMSignature header = new DKIMSignature();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,18 @@
package org.jboss.resteasy.security.doseta;

import org.jboss.resteasy.annotations.interception.HeaderDecoratorPrecedence;
import org.jboss.resteasy.annotations.security.doseta.After;
import org.jboss.resteasy.annotations.security.doseta.Verifications;
import org.jboss.resteasy.annotations.security.doseta.Verify;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.AcceptedByMethod;
import org.jboss.resteasy.spi.interception.ClientExecutionContext;
import org.jboss.resteasy.spi.interception.ClientExecutionInterceptor;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Method;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Provider
@HeaderDecoratorPrecedence
public class DigitalVerificationHeaderDecorator implements ClientExecutionInterceptor, PreProcessInterceptor, AcceptedByMethod
public class AbstractDigitalVerificationHeaderDecorator
{
protected Verify verify;
protected Verifications verifications;

public boolean accept(Class declaring, Method method)
{
verify = (Verify) method.getAnnotation(Verify.class);
verifications = (Verifications) method.getAnnotation(Verifications.class);

return verify != null || verifications != null;
}

@Override
public ClientResponse execute(ClientExecutionContext ctx) throws Exception
{
ClientResponse response = ctx.proceed();
response.getAttributes().put(Verifier.class.getName(), create());
return response;
}

@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException
{
request.setAttribute(Verifier.class.getName(), create());
return null;
}

public Verifier create()
{
// Currently we create verifier every time so that the verifications can hold state related to failures
Expand Down Expand Up @@ -101,5 +62,4 @@ protected Verification createVerification(Verify v)
verification.setBodyHashRequired(v.bodyHashRequired());
return verification;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.jboss.resteasy.security.doseta;

import org.jboss.resteasy.annotations.security.doseta.Signed;
import org.jboss.resteasy.spi.ResteasyProviderFactory;

import javax.ws.rs.BindingPriority;
import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Configurable;
import java.io.IOException;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@ConstrainedTo(ConstrainedTo.Type.CLIENT)
public class ClientDigitalSigningHeaderDecoratorFeature implements DynamicFeature
{
@Override
public void configure(ResourceInfo resourceInfo, Configurable configurable)
{
Signed signed = resourceInfo.getResourceMethod().getAnnotation(Signed.class);
if (signed == null)
{
signed = (Signed) resourceInfo.getResourceClass().getAnnotation(Signed.class);
}
if (signed == null) return;

configurable.register(new DigitalSigningHeaderDecorator(signed));
}

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@BindingPriority(BindingPriority.HEADER_DECORATOR)
public static class DigitalSigningHeaderDecorator extends AbstractDigitalSigningHeaderDecorator implements ClientRequestFilter
{
public DigitalSigningHeaderDecorator(Signed signed)
{
this.signed = signed;
}

@Override
public void filter(ClientRequestContext requestContext) throws IOException
{
KeyRepository repository = (KeyRepository) requestContext.getProperty(KeyRepository.class.getName());
if (repository == null)
{
repository = ResteasyProviderFactory.getContextData(KeyRepository.class);
}
DKIMSignature header = createHeader(repository);
requestContext.getHeaders().add(DKIMSignature.DKIM_SIGNATURE, header);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jboss.resteasy.security.doseta;

import org.jboss.resteasy.annotations.security.doseta.Verifications;
import org.jboss.resteasy.annotations.security.doseta.Verify;

import javax.ws.rs.BindingPriority;
import javax.ws.rs.ConstrainedTo;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Configurable;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Provider
@ConstrainedTo(ConstrainedTo.Type.CLIENT)
public class ClientDigitalVerificationHeaderDecoratorFeature implements DynamicFeature
{
@Override
public void configure(ResourceInfo resourceInfo, Configurable configurable)
{
Verify verify = resourceInfo.getResourceMethod().getAnnotation(Verify.class);
Verifications verifications = resourceInfo.getResourceClass().getAnnotation(Verifications.class);

if (verify != null || verifications != null)
{
configurable.register(new DigitalVerificationHeaderDecorator(verify, verifications));
}

}

@BindingPriority(BindingPriority.HEADER_DECORATOR)
public static class DigitalVerificationHeaderDecorator extends AbstractDigitalVerificationHeaderDecorator implements ClientResponseFilter
{
public DigitalVerificationHeaderDecorator(Verify verify, Verifications verifications)
{
this.verify = verify;
this.verifications = verifications;
}

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException
{
requestContext.setProperty(Verifier.class.getName(), create());
}

}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package org.jboss.resteasy.security.doseta;

import org.jboss.resteasy.annotations.interception.HeaderDecoratorPrecedence;
import org.jboss.resteasy.annotations.security.doseta.Signed;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.interception.AcceptedByMethod;
import org.jboss.resteasy.spi.interception.ClientExecutionContext;
import org.jboss.resteasy.spi.interception.ClientExecutionInterceptor;

import javax.ws.rs.ext.Provider;
import java.lang.reflect.Method;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
@Provider
@HeaderDecoratorPrecedence
public class ClientDigitalSigningHeaderDecorator extends DigitalSigningHeaderDecorator implements ClientExecutionInterceptor
public class DigitalSigningHeaderDecoratorClientExecutionInterceptor extends AbstractDigitalSigningHeaderDecorator implements ClientExecutionInterceptor, AcceptedByMethod
{

public boolean accept(Class declaring, Method method)
{
signed = method.getAnnotation(Signed.class);
if (signed == null)
{
signed = (Signed) declaring.getAnnotation(Signed.class);
}
return signed != null;
}

@Override
public ClientResponse execute(ClientExecutionContext ctx) throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
import org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;

import javax.ws.rs.BindingPriority;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.ClientException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -31,10 +40,9 @@
* @version $Revision: 1 $
*/
@Provider
@ServerInterceptor
@ClientInterceptor
@DecoderPrecedence
public class DigitalSigningInterceptor implements MessageBodyWriterInterceptor, ClientExecutionInterceptor, PostProcessInterceptor
@BindingPriority(BindingPriority.ENTITY_CODER)
public class DigitalSigningInterceptor implements WriterInterceptor, ClientExecutionInterceptor, ContainerResponseFilter, ClientRequestFilter
{

protected List<DKIMSignature> getHeaders(MultivaluedMap<String, Object> headers)
Expand All @@ -57,12 +65,39 @@ protected List<DKIMSignature> getHeaders(MultivaluedMap<String, Object> headers)
return list;
}

@Override
public void filter(ClientRequestContext requestContext) throws IOException
{
if (requestContext.hasEntity())
{
// let WriterInterceptor handle this
return;
}
MultivaluedMap<String, Object> headers = requestContext.getHeaders();
List<DKIMSignature> list = getHeaders(headers);

for (DKIMSignature dosetaSignature : list)
{
KeyRepository repository = (KeyRepository) requestContext.getProperty(KeyRepository.class.getName());
try
{
sign(repository, headers, null, dosetaSignature);
}
catch (Exception e)
{
throw new ClientException(e);
}
}

}

@Override
public ClientResponse execute(ClientExecutionContext context) throws Exception
{
if (context.getRequest().getBody() != null)
{
return context.proceed(); // let the MBW handle this
return context.proceed(); // let WriterInterceptor handle this

}

MultivaluedMap<String, Object> headers = context.getRequest().getHeadersAsObjects();
Expand All @@ -78,14 +113,14 @@ public ClientResponse execute(ClientExecutionContext context) throws Exception
}

@Override
public void postProcess(ServerResponse response)
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException
{
if (response.getEntity() != null)
if (responseContext.getEntity() != null)
{
return; // let the MBW handle this
return; // let WriterInterceptor handle this
}

MultivaluedMap<String, Object> headers = response.getMetadata();
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
List<DKIMSignature> list = getHeaders(headers);

for (DKIMSignature dosetaSignature : list)
Expand All @@ -101,7 +136,8 @@ public void postProcess(ServerResponse response)
}
}

public void write(MessageBodyWriterContext context) throws IOException, WebApplicationException
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException
{
MultivaluedMap<String, Object> headers = context.getHeaders();

Expand All @@ -125,7 +161,7 @@ public void write(MessageBodyWriterContext context) throws IOException, WebAppli

for (DKIMSignature dosetaSignature : list)
{
KeyRepository repository = (KeyRepository) context.getAttribute(KeyRepository.class.getName());
KeyRepository repository = (KeyRepository) context.getProperty(KeyRepository.class.getName());
sign(repository, headers, body, dosetaSignature);
}

Expand Down Expand Up @@ -153,14 +189,14 @@ protected void sign(KeyRepository repository, MultivaluedMap<String, Object> hea

if (repository == null)
{
throw new RuntimeException("Unable to locate a private key to sign message, repository is null.");
throw new InvalidKeyException("Unable to locate a private key to sign message, repository is null.");

}

privateKey = repository.findPrivateKey(dosetaSignature);
if (privateKey == null)
{
throw new RuntimeException("Unable to find key to sign message. Repository returned null. ");
throw new InvalidKeyException("Unable to find key to sign message. Repository returned null. ");
}
}
dosetaSignature.sign(headers, body, privateKey);
Expand Down
Loading

0 comments on commit f4bcfe9

Please sign in to comment.