Пример #1
0
        public async Task ParseTokenResponse_post_binding_sp_initiated()
        {
            //ARRANGE
            var inResponseTo = Guid.NewGuid().ToString();

            var response            = ResponseFactoryMock.GetTokenResponseSuccess(inResponseTo, StatusCodes.Success);
            var logger              = new LogProviderMock();
            var serialised          = ResponseFactoryMock.Serialize(response);
            var xmlSignatureManager = new XmlSignatureManager();
            var document            = new XmlDocument();

            document.LoadXml(serialised);
            var cert = AssertionFactroryMock.GetMockCertificate();

            xmlSignatureManager.SignXml(document, response.ID, cert.PrivateKey, null);
            var base64Encoded = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(document.DocumentElement.OuterXml));

            var compressor           = new DeflateCompressor();
            var encoder              = new MessageEncoding(compressor);
            var jsonSerialiser       = new NSJsonSerializer(new DefaultSettingsProvider());
            var relayStateSerialiser = new RelaystateSerialiser(jsonSerialiser, encoder, logger) as IRelayStateSerialiser;
            var relayState           = await relayStateSerialiser.Serialize(new Dictionary <string, object> {
                { "Key", "Value" }
            });

            var form = new SAMLForm();

            form.SetResponse(base64Encoded);
            form.SetRelatState(relayState);

            Func <Type, IMetadataHandler> metadataHandlerFactory = t => new MetadataEntitityDescriptorHandler();

            var certManager      = new CertificateManager(logger);
            var signatureManager = new XmlSignatureManager();
            Func <IEnumerable <ResponseValidationRule> > rulesResolver = () => new[] { new ResponseSignatureRule(logger, certManager, signatureManager) };
            var validator = new Federation.Protocols.Response.Validation.ResponseValidator(logger, new RuleFactory(rulesResolver));
            var configurationRetrieverMock        = new ConfigurationRetrieverMock();
            var federationPartyContextBuilderMock = new FederationPartyContextBuilderMock();
            var configurationManger = new ConfigurationManager <MetadataBase>(federationPartyContextBuilderMock, configurationRetrieverMock);
            var relayStateHandler   = new RelayStateHandler(relayStateSerialiser, logger);
            var responseParser      = new ResponseParser(metadataHandlerFactory, t => new SamlTokenResponseParser(logger),
                                                         configurationManger, relayStateHandler, logger, validator);
            var postBindingDecoder = new PostBindingDecoder(logger);
            var message            = await postBindingDecoder.Decode(form.HiddenControls.ToDictionary(k => k.Key, v => v.Value));

            var context = new SamlInboundContext
            {
                Message            = message,
                DescriptorResolver = m => metadataHandlerFactory(typeof(object)).GetIdentityProviderSingleSignOnDescriptor(m).Single().Roles.Single()
            };
            //ACT
            var result = await responseParser.Parse(context);

            //ASSERT
            Assert.IsTrue(result.IsValidated);
        }
Пример #2
0
        public static async Task <SAMLForm> BuildRequestBindingContext(RequestContext requestContext)
        {
            string url      = String.Empty;
            var    builders = new List <IPostClauseBuilder>();

            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);
            var xmlSinatureManager = new XmlSignatureManager();

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

            builders.Add(authnBuilder);

            //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, xmlSinatureManager);

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

            foreach (var b in builders)
            {
                await b.Build(bindingContext);
            }
            var form          = new SAMLForm();
            var request       = bindingContext.RequestParts[HttpRedirectBindingConstants.SamlRequest];
            var base64Encoded = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(request));

            var relyingStateSerialised = bindingContext.RequestParts[HttpRedirectBindingConstants.RelayState];

            form.ActionURL = bindingContext.DestinationUri.AbsoluteUri;
            form.SetRequest(base64Encoded);
            form.SetRelatState(relyingStateSerialised);

            return(form);
        }