public void GetClaimsAsyncTest()
        {
            var routeDef = new PostRouteDefinition(typeof(TestingType))
            {
                ClaimsConfig = new RouteClaimsConfig()
                {
                    ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                    {
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathNo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.No").Build(),
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathYo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.Yo").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueHeaders", ClaimLocation.Headers)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "Authorization").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueQuery", ClaimLocation.QueryParameters)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "areYou").Build(),
                        new RegexClaimExtractionConfig("PityTheFoolRegex", ClaimLocation.Uri)
                        .ConfigureExtraction(ExtractionFunctions.RegexFunc, new Regex("/yolo/nolo/(.*)/bolo"))
                        .Build(),
                        new TypeClaimExtractionConfig <TestingType>("PityTheFoolType")
                        .ConfigureExtraction((testingType) => { return(Task.FromResult(testingType.No)); }).Build()
                    }
                }
            };
            var requestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new TestingType()
            {
                Yo = "Lo",
                No = "low"
            }));
            var request = new DefaultHttpRequest(new DefaultHttpContext())
            {
                Body    = new MemoryStream(requestBytes),
                Method  = HttpMethod.Post.ToString(),
                Headers =
                {
                    new KeyValuePair <string,                                                       StringValues>("Authorization",
                                                                                                                  new StringValues("Bearer hollaaaaaa")),
                    new KeyValuePair <string,                                                       StringValues>("x-access-token",
                                                                                                                  new StringValues("just kidding"))
                },
                Path  = "/yolo/nolo/12345/bolo",
                Query = new QueryCollection(
                    new Dictionary <string, StringValues>()
                {
                    { "areYou", new StringValues("YesIAm") },
                    { "spitItAllOut", new StringValues("YES") }
                }),
                ContentType   = "application/json",
                ContentLength = requestBytes.Length
            };
            var claims = ExtractionUtils.GetClaimsAsync(routeDef, request, new RouteValueDictionary(TemplateParser.Parse("yolo/nolo/{culpridId}/bolo"))).Result;

            Assert.Equal(6, claims.Count);
            Assert.Equal("low", claims.First(x => x.Type == "PityTheFoolJsonPathNo").Value);
            Assert.Equal("Lo", claims.First(x => x.Type == "PityTheFoolJsonPathYo").Value);
            Assert.Equal("Bearer hollaaaaaa", claims.First(x => x.Type == "PityTheFoolKeyValueHeaders").Value);
            Assert.Equal("YesIAm", claims.First(x => x.Type == "PityTheFoolKeyValueQuery").Value);
            Assert.Equal("12345", claims.First(x => x.Type == "PityTheFoolRegex").Value);
            Assert.Equal("low", claims.First(x => x.Type == "PityTheFoolType").Value);
        }
Example #2
0
        public void InternalRouteDefinition()
        {
            var routeDef = new PostRouteDefinition(typeof(TestingType));

            routeDef.ClaimsConfig = new RouteClaimsConfig()
            {
                ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                {
                    new JsonPathClaimExtractionConfig("YoloClaim")
                    .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.json").Build()
                }
            };
            routeDef.RouteTemplate = "who/letThe/Dogs/out/{culpritId}";
            var internalRouteDef = new InternalRouteDefinition(routeDef);

            Assert.Equal(HttpMethod.Post, internalRouteDef.Method);
            Assert.Equal(typeof(TestingType), internalRouteDef.RequestBody);
        }
Example #3
0
        public void InternalRouteDefinition_Post()
        {
            var routeDef     = new PostRouteDefinition(typeof(TestingType));
            var claimsConfig = new RouteClaimsConfig()
            {
                ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                {
                    new JsonPathClaimExtractionConfig("YoloClaim")
                    .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.json").Build()
                }
            };

            routeDef.ClaimsConfig = claimsConfig;
            var internalRouteDef = new InternalRouteDefinition(routeDef);

            Assert.Equal(HttpMethod.Post, internalRouteDef.Method);
            Assert.Equal(typeof(TestingType), internalRouteDef.RequestBody);
            Assert.Equal(claimsConfig, internalRouteDef.ClaimsConfig);
        }
Example #4
0
        public void InternalRouteDefinition_BadRouteTemplate()
        {
            var routeDef = new PostRouteDefinition(typeof(TestingType));

            routeDef.ClaimsConfig = new RouteClaimsConfig()
            {
                ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                {
                    new JsonPathClaimExtractionConfig("YoloClaim")
                    .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.json").Build()
                }
            };
            routeDef.RouteTemplate = "slkdfjlsdkjf?&lksdflkjsdflk";
            try
            {
                var internalRouteDef = new InternalRouteDefinition(routeDef);
                Assert.False(internalRouteDef == null);
            }
            catch (Exception)
            {
                // ignored
            }
        }
        public void ValidateClaimsAsyncTest_Invalid()
        {
            var routeDef = new PostRouteDefinition(typeof(TestingType))
            {
                ClaimsConfig = new RouteClaimsConfig()
                {
                    ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                    {
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathNo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.No").Build(),
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathYo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.Yo").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueHeaders", ClaimLocation.Headers)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "Authorization").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueQuery", ClaimLocation.QueryParameters)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "areYou").Build(),
                        new RegexClaimExtractionConfig("PityTheFoolRegex", ClaimLocation.Uri)
                        .ConfigureExtraction(ExtractionFunctions.RegexFunc, new Regex("/yolo/nolo/(.*)/bolo"))
                        .Build(),
                        new TypeClaimExtractionConfig <TestingType>("PityTheFoolType")
                        .ConfigureExtraction((testingType) => { return(Task.FromResult(testingType.No)); }).Build()
                    },
                    ValidationConfigs = new List <ClaimValidationConfig>()
                    {
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolJsonPathNo",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolJsonPathYo",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolKeyValueHeaders",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolKeyValueQuery",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolRegex",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolType",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "Wildcard",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        }
                    },
                    BadRequestResponse = new BadRequestResponse()
                    {
                        Response = new ExpandoObject(),
                        Headers  = new HeaderDictionary()
                        {
                            { "worked", "true" }
                        },
                        HttpStatusCode = HttpStatusCode.Forbidden
                    }
                },
                RouteTemplate = "/yolo/nolo/{id:int}/bolo"
            };
            var requestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new TestingType()
            {
                Yo = "Lo",
                No = "low"
            }));
            var httpContext = new DefaultHttpContext();

            httpContext.Response.Body = new MemoryStream();
            var request = new DefaultHttpRequest(httpContext)
            {
                Body    = new MemoryStream(requestBytes),
                Method  = HttpMethod.Post.ToString(),
                Headers =
                {
                    new KeyValuePair <string,                                                       StringValues>("Authorization",
                                                                                                                  new StringValues("Bearer hollaaaaaa")),
                    new KeyValuePair <string,                                                       StringValues>("x-access-token",
                                                                                                                  new StringValues("just kidding"))
                },
                Path  = "/yolo/nolo/12345/bolo",
                Query = new QueryCollection(
                    new Dictionary <string, StringValues>()
                {
                    { "areYou", new StringValues("YesIAm") },
                    { "spitItAllOut", new StringValues("YES") }
                }),
                ContentType   = "application/json",
                ContentLength = requestBytes.Length
            };

            httpContext.Items.Add("ExpectedClaims", new List <Claim>()
            {
                new Claim("PityTheFoolJsonPathNo", "low"),
                new Claim("PityTheFoolJsonPathYo", "Lo"),
                new Claim("PityTheFoolKeyValueHeaders", "Bearer hollaaaaaa"),
                new Claim("PityTheFoolKeyValueQuery", "YesIAm"),
                new Claim("PityTheFoolRegex", "12345"),
                new Claim("PityTheFoolType", "low"),
                new Claim("Wildcard", "whoooooooooo")
            });
            Assert.NotNull(request);
            Assert.False(ValidationUtils.ValidateClaimsAsync(routeDef, httpContext, new RouteValueDictionary(TemplateParser.Parse("yolo/nolo/{id:int}/bolo"))).Result);
            Assert.Equal((int)HttpStatusCode.Forbidden, httpContext.Response.StatusCode);
            Assert.Single(httpContext.Response.Headers.Where(x => x.Key == "worked"));
            httpContext.Response.Body.Position = 0;
            var responseString       = new StreamReader(httpContext.Response.Body).ReadToEnd();
            var responseDeserialized = JsonConvert.DeserializeObject <dynamic>(responseString);

            Assert.Equal("Wildcard", ((JArray)responseDeserialized.MissingClaims)[0].Value <string>());
        }
        public void ValidateClaimsAsyncTest()
        {
            var routeDef = new PostRouteDefinition(typeof(TestingType))
            {
                ClaimsConfig = new RouteClaimsConfig()
                {
                    ExtractionConfigs = new List <IValidClaimsExtractionConfig>()
                    {
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathNo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.No").Build(),
                        new JsonPathClaimExtractionConfig("PityTheFoolJsonPathYo")
                        .ConfigureExtraction(ExtractionFunctions.JsonPathFunc, "$.Yo").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueHeaders", ClaimLocation.Headers)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "Authorization").Build(),
                        new KeyValueClaimExtractionConfig("PityTheFoolKeyValueQuery", ClaimLocation.QueryParameters)
                        .ConfigureExtraction(ExtractionFunctions.KeyValueFunc, "areYou").Build(),
                        new RegexClaimExtractionConfig("PityTheFoolRegex", ClaimLocation.Uri)
                        .ConfigureExtraction(ExtractionFunctions.RegexFunc, new Regex("/yolo/nolo/(.*)/bolo"))
                        .Build(),
                        new TypeClaimExtractionConfig <TestingType>("PityTheFoolType")
                        .ConfigureExtraction((testingType) => { return(Task.FromResult(testingType.No)); }).Build()
                    },
                    ValidationConfigs = new List <ClaimValidationConfig>()
                    {
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolJsonPathNo",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolJsonPathYo",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolKeyValueHeaders",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolKeyValueQuery",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolRegex",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        },
                        new ClaimValidationConfig()
                        {
                            AllowNullOrEmpty      = false,
                            ClaimName             = "PityTheFoolType",
                            IsRequired            = true,
                            ValueMustBeExactMatch = true
                        }
                    }
                },
                RouteTemplate = "/yolo/nolo/{id:int}/bolo"
            };
            var requestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new TestingType()
            {
                Yo = "Lo",
                No = "low"
            }));
            var httpContext = new DefaultHttpContext();
            var request     = new DefaultHttpRequest(httpContext)
            {
                Body    = new MemoryStream(requestBytes),
                Method  = HttpMethod.Post.ToString(),
                Headers =
                {
                    new KeyValuePair <string,                                                       StringValues>("Authorization",
                                                                                                                  new StringValues("Bearer hollaaaaaa")),
                    new KeyValuePair <string,                                                       StringValues>("x-access-token",
                                                                                                                  new StringValues("just kidding"))
                },
                Path  = "/yolo/nolo/12345/bolo",
                Query = new QueryCollection(
                    new Dictionary <string, StringValues>()
                {
                    { "areYou", new StringValues("YesIAm") },
                    { "spitItAllOut", new StringValues("YES") }
                }),
                ContentType   = "application/json",
                ContentLength = requestBytes.Length
            };

            httpContext.Items.Add("ExpectedClaims", new List <Claim>()
            {
                new Claim("PityTheFoolJsonPathNo", "low"),
                new Claim("PityTheFoolJsonPathYo", "Lo"),
                new Claim("PityTheFoolKeyValueHeaders", "Bearer hollaaaaaa"),
                new Claim("PityTheFoolKeyValueQuery", "YesIAm"),
                new Claim("PityTheFoolRegex", "12345"),
                new Claim("PityTheFoolType", "low"),
            });
            Assert.NotNull(request);
            Assert.True(ValidationUtils.ValidateClaimsAsync(routeDef, httpContext, new RouteValueDictionary(TemplateParser.Parse("yolo/nolo/{id:int}/bolo"))).Result);
        }