Last active
December 17, 2015 12:29
-
-
Save akimboyko/5610447 to your computer and use it in GitHub Desktop.
PostSharp custom architecture constraint ("HmacSignatureRequiredAttribute.cs" here) requires that every specified method of WebAPIcontroller ("SomeController.cs" here) contains both User Identity and HMAC (keyed-hash message authentication code ) signature. Otherwise compile-time error will happens
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
| namespace CodeSmells.FakeWebApplication.Aspect | |
| { | |
| [Serializable] | |
| [MulticastAttributeUsage(MulticastTargets.Class)] | |
| public class HmacSignatureRequiredAttribute : ScalarConstraint | |
| { | |
| private readonly string[] _httpVerbs; | |
| public HmacSignatureRequiredAttribute(string[] httpVerbs) | |
| { | |
| _httpVerbs = httpVerbs; | |
| } | |
| public override void ValidateCode(object target) | |
| { | |
| var targetType = (Type)target; | |
| var httpVerbsMethods = targetType | |
| .GetMethods(BindingFlags.Public | BindingFlags.Instance) | |
| .Where(methodInfo => _httpVerbs.Contains(methodInfo.Name)) | |
| .Where( | |
| methodInfo => | |
| !(methodInfo.GetParameters().First().ParameterType == typeof (int) | |
| && methodInfo.GetParameters().First().Name == "userId") | |
| || | |
| !(methodInfo.GetParameters().Last().ParameterType == typeof (string) | |
| && methodInfo.GetParameters().Last().Name == "hmacSignature")); | |
| foreach (var methodInfo in httpVerbsMethods) | |
| { | |
| Message.Write(MessageLocation.Of(targetType), | |
| SeverityType.Error, | |
| "997", | |
| "Method '{0}' in ApiController class {1} required to have both userId/hmacSignature", | |
| methodInfo.Name, targetType.FullName); | |
| } | |
| } | |
| } | |
| } |
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
| namespace CodeSmells.FakeWebApplication.Controllers | |
| { | |
| // Require following HTTP verbs to have identity and signature | |
| [HmacSignatureRequired(new[] { "Post", "Put", "Delete" })] | |
| public class SomeController : ApiController | |
| { | |
| public IEnumerable<string> Get() | |
| { | |
| return new string[] { "value1", "value2" }; | |
| } | |
| public string Get(int id) | |
| { | |
| return "value"; | |
| } | |
| // ok | |
| public void Post([FromBody]int userId, [FromBody]string value, [FromBody]string hmacSignature) | |
| { | |
| } | |
| // ok | |
| public void Put([FromBody]int userId, int id, [FromBody]string value, [FromBody]string hmacSignature) | |
| { | |
| } | |
| // without both `int userId` and `string hmacSignature` parameters | |
| // PostSharp will generate compile-time error message | |
| public void Delete(int userId, int id, decimal hmacSignature) | |
| { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment