Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 10, 2012 19:08
Show Gist options
  • Save davybrion/3693029 to your computer and use it in GitHub Desktop.
Save davybrion/3693029 to your computer and use it in GitHub Desktop.
code snippets for "Securing Your Agatha Service Layer" post
public class MyProjectRequest : Request
{
public string UserName { get; set; }
public byte[] PasswordHash { get; set; }
}
public class MyProjectResponse : Response {}
public class MyProjectAsyncRequestDispatcher : AsyncRequestDispatcher
{
private readonly IUserContext userContext;
public MyProjectAsyncRequestDispatcher(IAsyncRequestProcessor requestProcessor, IUserContext userContext) : base(requestProcessor)
{
this.userContext = userContext;
}
protected override void BeforeSendingRequests(IEnumerable<Request> requestsToProcess)
{
base.BeforeSendingRequests(requestsToProcess);
foreach (var myProjectRequest in requestsToProcess.OfType<MyProjectRequest>())
{
myProjectRequest.UserName = userContext.UserName;
myProjectRequest.PasswordHash = userContext.PasswordHash;
}
}
}
new ClientConfiguration(typeof(MyProjectRequest).Assembly, new Agatha.Castle.Container(myContainerWrapper))
{
AsyncRequestDispatcherImplementation = typeof(MyProjectAsyncRequestDispatcher)
}
.Initialize();
public class MyProjectRequestProcessor : RequestProcessor
{
private readonly IAuthenticator authenticator;
public MyProjectRequestProcessor(IAuthenticator authenticator, ServiceLayerConfiguration serviceLayerConfiguration, ICacheManager cacheManager)
: base(serviceLayerConfiguration, cacheManager)
{
this.authenticator = authenticator;
}
protected override void BeforeHandle(Request request)
{
var myProjectRequest = request as MyProjectRequest;
if (myProjectRequest != null)
{
if (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))
{
throw new MySecurityException();
}
}
}
}
public class MyProjectRequestProcessor : RequestProcessor
{
private readonly IAuthenticator authenticator;
public MyProjectRequestProcessor(IAuthenticator authenticator, ServiceLayerConfiguration serviceLayerConfiguration, ICacheManager cacheManager)
: base(serviceLayerConfiguration, cacheManager)
{
this.authenticator = authenticator;
}
protected override void BeforeProcessing(IEnumerable<Request> requests)
{
var myProjectRequest = (MyProjectRequest)requests.ElementAt(0);
if (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))
{
throw new MySecurityException();
}
}
}
new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(MyProjectRequest).Assembly,
new Agatha.Castle.Container(containerWrapper))
{
RequestProcessorImplementation = typeof(MyProjectRequestProcessor),
SecurityExceptionType = typeof(MySecurityException)
}
.Initialize();
public abstract class MyProjectRequestHandler<TRequest, TResponse> : RequestHandler<TRequest, TResponse>
where TRequest : MyProjectRequest
{
public IAuthenticator Authenticator { get; set; }
public override void BeforeHandle(TRequest request)
{
base.BeforeHandle(request);
if (!Authenticator.AreValidCredentials(request.UserName, request.PasswordHash))
{
throw new MySecurityException();
}
Authorize(request);
}
protected virtual void Authorize(TRequest request) {}
}
new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(MyProjectRequest).Assembly,
new Agatha.Castle.Container(containerWrapper))
{
SecurityExceptionType = typeof(MySecurityException)
}
.Initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment