Exemplo n.º 1
0
        private static bool TrySetPropertyFromRequestUrl(PropertyBindingContext pbc, RequestBindingContext context)
        {
            if (!context.Request.RouteValues.TryGetValue(pbc.Name, out var paramValue))
            {
                return(false);
            }

            var propertyValue = Convert.ChangeType(paramValue, pbc.CommandProperty.PropertyType);

            return(context.TrySetCommandProperty(pbc, propertyValue));
        }
Exemplo n.º 2
0
        private static bool TrySetPropertyFromForm(PropertyBindingContext pbc, RequestBindingContext context)
        {
            var formValues = context.FormData[pbc.Name];

            if (formValues.Count < 1)
            {
                return(false);
            }

            return(context.TrySetCommandProperty(pbc, formValues[0]));
        }
        public static async Task <RequestBindingContext> BuildRequestBindingContext(RequestContext requestContext)
        {
            string url      = String.Empty;
            var    builders = new List <IRedirectClauseBuilder>();

            requestContext.RelyingState.Add("relayState", "Test state");
            var xmlSerialiser = new XMLSerialiser();
            var compressor    = new DeflateCompressor();
            var encoder       = new MessageEncoding(compressor);
            var logger        = new LogProviderMock();
            var serialiser    = new RequestSerialiser(xmlSerialiser, encoder, logger);

            RequestHelper.GetAuthnRequestBuilders = AuthnRequestBuildersFactoryMock.GetAuthnRequestBuildersFactory();
            var authnBuilder = new SamlRequestBuilder(serialiser);

            builders.Add(authnBuilder);

            //request compression builder
            var encodingBuilder = new RequestEncoderBuilder(encoder);

            builders.Add(encodingBuilder);

            //relay state builder
            var jsonSerialiser       = new NSJsonSerializer(new DefaultSettingsProvider());
            var relayStateSerialiser = new RelaystateSerialiser(jsonSerialiser, encoder, logger) as IRelayStateSerialiser;
            var relayStateBuilder    = new RelayStateBuilder(relayStateSerialiser);

            builders.Add(relayStateBuilder);

            //signature builder
            var certificateManager = new CertificateManager(logger);
            var signatureBuilder   = new SignatureBuilder(certificateManager, logger);

            builders.Add(signatureBuilder);
            var bindingContext = new RequestBindingContext(requestContext);

            foreach (var b in builders)
            {
                await b.Build(bindingContext);
            }

            return(bindingContext);
        }
Exemplo n.º 4
0
        private static void TrySetProperty(PropertyBindingContext bi, RequestBindingContext context)
        {
            // Try from the request body first
            if (TrySetPropertyFromRequestBody(bi, context))
            {
                return;
            }

            if (TrySetPropertyFromRequestUrl(bi, context))
            {
                return;
            }

            if (TrySetPropertyFromQueryString(bi, context))
            {
                return;
            }

            TrySetPropertyFromForm(bi, context);
        }
Exemplo n.º 5
0
        public async Task <object> GenerateCommandAsync(HttpRequest request)
        {
            if (BindingContext.DirectFromBody)
            {
                return(await DeserializeCommandFromBody(request));
            }

            var command        = Activator.CreateInstance(_commandType);
            var requestContext = new RequestBindingContext(command, request);

            foreach (var bi in BindingContext.Properties)
            {
                switch (bi.BindingLocation)
                {
                case RequestBindingLocation.Any:
                    TrySetProperty(bi, requestContext);
                    break;

                case RequestBindingLocation.Url:
                    TrySetPropertyFromRequestUrl(bi, requestContext);
                    break;

                case RequestBindingLocation.QueryParameter:
                    break;

                case RequestBindingLocation.Body:
                    TrySetPropertyFromRequestBody(bi, requestContext);
                    break;

                case RequestBindingLocation.FormField:
                    TrySetPropertyFromForm(bi, requestContext);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(command);
        }
Exemplo n.º 6
0
        public async Task DecodeTest()
        {
            string url      = String.Empty;
            var    builders = new List <IRedirectClauseBuilder>();

            var requestUri = new Uri("http://localhost:59611/");
            var federationPartyContextBuilder = new FederationPartyContextBuilderMock();
            var federationContex = federationPartyContextBuilder.BuildContext("local");
            var spDescriptor     = federationContex.MetadataContext.EntityDesriptorConfiguration.SPSSODescriptors.First();
            var certContext      = spDescriptor.KeyDescriptors.Where(x => x.Use == KeyUsage.Signing && x.IsDefault)
                                   .Select(x => x.CertificateContext)
                                   .First();
            var supportedNameIdentifierFormats = new List <Uri> {
                new Uri(NameIdentifierFormats.Transient)
            };
            var authnRequestContext = new AuthnRequestContext(requestUri, new Uri("http://localhost"), federationContex, supportedNameIdentifierFormats);

            authnRequestContext.RelyingState.Add("relayState", "Test state");
            var xmlSerialiser = new XMLSerialiser();
            var compressor    = new DeflateCompressor();
            var encoder       = new MessageEncoding(compressor);
            var logger        = new LogProviderMock();
            var serialiser    = new RequestSerialiser(xmlSerialiser, encoder, logger);

            RequestHelper.GetAuthnRequestBuilders = AuthnRequestBuildersFactoryMock.GetAuthnRequestBuildersFactory();
            var authnBuilder = new SamlRequestBuilder(serialiser);

            builders.Add(authnBuilder);

            //request compression builder
            var encodingBuilder = new RequestEncoderBuilder(encoder);

            builders.Add(encodingBuilder);

            //relay state builder
            var jsonSerialiser       = new NSJsonSerializer(new DefaultSettingsProvider());
            var relayStateSerialiser = new RelaystateSerialiser(jsonSerialiser, encoder, logger) as IRelayStateSerialiser;
            var relayStateBuilder    = new RelayStateBuilder(relayStateSerialiser);

            builders.Add(relayStateBuilder);

            //signature builder
            var certificateManager = new CertificateManager(logger);
            var signatureBuilder   = new SignatureBuilder(certificateManager, logger);

            builders.Add(signatureBuilder);
            var bindingContext = new RequestBindingContext(authnRequestContext);

            foreach (var b in builders)
            {
                await b.Build(bindingContext);
            }
            var decoder = new RedirectBindingDecoder(logger, encoder);
            //ACT
            var message = await decoder.Decode(bindingContext.GetDestinationUrl());

            var stateFromResult    = message.Elements[HttpRedirectBindingConstants.RelayState];
            var requestFromContext = bindingContext.RequestParts[HttpRedirectBindingConstants.SamlRequest];
            var decoded            = await encoder.DecodeMessage(requestFromContext);

            //ASSERT
            Assert.IsNotNull(stateFromResult);
            Assert.AreEqual(bindingContext.RequestParts[HttpRedirectBindingConstants.RelayState], message.Elements[HttpRedirectBindingConstants.RelayState]);
            Assert.AreEqual(decoded, message.Elements[HttpRedirectBindingConstants.SamlRequest]);
        }
Exemplo n.º 7
0
        private static bool TrySetPropertyFromRequestBody(PropertyBindingContext pbc, RequestBindingContext context)
        {
            var bodyProperty = context.BodyObject?.Property(pbc.Name, StringComparison.OrdinalIgnoreCase);

            if (bodyProperty == null)
            {
                return(false);
            }

            var commandPropertyType = pbc.CommandProperty.PropertyType;

            object propertyValue;

            try
            {
                propertyValue = bodyProperty.Value.ToObject(commandPropertyType);
            }
            catch
            {
                return(false);
            }

            return(context.TrySetCommandProperty(pbc, propertyValue));
        }