Created
September 10, 2012 19:08
-
-
Save davybrion/3693029 to your computer and use it in GitHub Desktop.
code snippets for "Securing Your Agatha Service Layer" post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyProjectRequest : Request | |
| { | |
| public string UserName { get; set; } | |
| public byte[] PasswordHash { get; set; } | |
| } | |
| public class MyProjectResponse : Response {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| new ClientConfiguration(typeof(MyProjectRequest).Assembly, new Agatha.Castle.Container(myContainerWrapper)) | |
| { | |
| AsyncRequestDispatcherImplementation = typeof(MyProjectAsyncRequestDispatcher) | |
| } | |
| .Initialize(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(MyProjectRequest).Assembly, | |
| new Agatha.Castle.Container(containerWrapper)) | |
| { | |
| RequestProcessorImplementation = typeof(MyProjectRequestProcessor), | |
| SecurityExceptionType = typeof(MySecurityException) | |
| } | |
| .Initialize(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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