Example #1
0
        public async Task NotAuthenticatedIntegrationTest()
        {
            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground
                    (new WampCraUserDbAuthenticationFactory
                        (new MyAuthenticationProvider(),
                        new MyUserDb()));

            SetupHost(playground);

            IWampClientAuthenticator authenticator =
                new WampCraClientAuthenticator(authenticationId: "peter", secret: "SECRET");

            IWampChannel channel =
                playground.CreateNewChannel("realm1", authenticator);

            IWampRealmProxy realmProxy = channel.RealmProxy;

            WampConnectionBrokenException openException = null;

            try
            {
                await channel.Open().ConfigureAwait(false);
            }
            catch (WampConnectionBrokenException ex)
            {
                openException = ex;
            }

            Assert.That(openException, Is.Not.Null);
            Assert.That(openException.CloseType, Is.EqualTo(SessionCloseType.Abort));
            Assert.That(openException.Reason, Is.EqualTo(WampErrors.NotAuthorized));
        }
        public void HelloParametersArePassedToAuthenticationFactory()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory = new MockSessionAuthenticationFactory();

            WampPendingClientDetails authenticatorFactoryParameters = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                authenticatorFactoryParameters = clientDetails;
                IWampSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                return(mockSessionAuthenticator);
            });

            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(new Mock <IWampClient <JToken> >().Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            Assert.That(authenticatorFactoryParameters.Realm, Is.EqualTo("realm1"));
            Assert.That(authenticatorFactoryParameters.HelloDetails.AuthenticationMethods, Is.EquivalentTo(new[] { "wampcra", "ticket" }));
            Assert.That(authenticatorFactoryParameters.HelloDetails.AuthenticationId, Is.EqualTo("joe"));
        }
Example #3
0
        public async Task KillByAuthIdTest(int killingChannel, int killedChannel, IEnumerable <int> expectedDeadChannels)
        {
            WampAuthenticationPlayground playground = SetupAuthenticationHost();

            List <IWampChannel> channels = new List <IWampChannel>();

            for (int i = 0; i < 30; i++)
            {
                IWampChannel channel =
                    playground.CreateNewChannel("realm1", new WampCraClientAuthenticator("user" + i % 15, "secret"));

                channels.Add(channel);
            }

            Dictionary <int, (string reason, string message)> disconnectionDetails =
                await OpenChannels(channels);

            Assert.That(disconnectionDetails.Count, Is.EqualTo(0));

            IWampSessionManagementServiceProxy proxy =
                channels[killingChannel].RealmProxy.Services.GetCalleeProxy <IWampSessionManagementServiceProxy>();

            const string expectedReason  = "wamp.myreason";
            const string expectedMessage = "Bye bye bye";

            await proxy.KillByAuthIdAsync("user" + killedChannel, expectedReason, expectedMessage).ConfigureAwait(false);

            AssertClosedChannels(expectedDeadChannels, disconnectionDetails, expectedMessage, expectedReason);
        }
Example #4
0
        public void ExceptionOnAuthenticateRaisesAbort()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
                new MockSessionAuthenticationFactory();

            WampPendingClientDetails authenticatorFactoryParameters = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                authenticatorFactoryParameters = clientDetails;
                MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                mockSessionAuthenticator.SetAuthenticationMethod("ticket");

                mockSessionAuthenticator.SetAuthenticate((signature, extraData) => throw new WampAuthenticationException(new MyAbortDetails()
                {
                    Message = "aborted!", Year = 2015
                }, "com.myapp.abortreason"));

                return(mockSessionAuthenticator);
            });

            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            string       clientReason       = null;
            AbortDetails clientAbortDetails = null;

            Mock <IWampClient <JToken> > clientMock = new Mock <IWampClient <JToken> >();

            clientMock.Setup(x => x.Abort(It.IsAny <AbortDetails>(), It.IsAny <string>()))
            .Callback((AbortDetails details, string reason) =>
            {
                clientReason       = reason;
                clientAbortDetails = details;
            });

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(clientMock.Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            serverProxy.Authenticate("Barack Hussein", new AuthenticateExtraData());

            Assert.That(clientReason, Is.EqualTo("com.myapp.abortreason"));
            Assert.That(clientAbortDetails.Message, Is.EqualTo("aborted!"));

            var deserialized =
                clientAbortDetails.OriginalValue.Deserialize <MyAbortDetails>();

            Assert.That(deserialized.Year, Is.EqualTo(2015));
        }
        public void AuthenticateParametersArePassedToSessionAuthenticator()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
                new MockSessionAuthenticationFactory();

            string receivedSignature = null;
            AuthenticateExtraData receivedExtraData = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                mockSessionAuthenticator.SetAuthenticationMethod("ticket");
                mockSessionAuthenticator.SetAuthenticate((signature, extra) =>
                {
                    receivedSignature = signature;
                    receivedExtraData = extra;
                });
                return(mockSessionAuthenticator);
            });


            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            string clientAuthMethod = null;

            ChallengeDetails clientChallengeDetails = null;

            Mock <IWampClient <JToken> > clientMock = new Mock <IWampClient <JToken> >();

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(clientMock.Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            serverProxy.Authenticate("Barack Hussein", new MyAuthenticateExtraData()
            {
                Wife = "Michelle"
            });

            MyAuthenticateExtraData deserializedExtraData =
                receivedExtraData.OriginalValue.Deserialize <MyAuthenticateExtraData>();

            Assert.That(receivedSignature, Is.EqualTo("Barack Hussein"));

            Assert.That(deserializedExtraData.Wife, Is.EqualTo("Michelle"));
        }
        public void ChallengeParametersArePassedToClient()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
                new MockSessionAuthenticationFactory();

            WampPendingClientDetails authenticatorFactoryParameters = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                authenticatorFactoryParameters = clientDetails;
                MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                mockSessionAuthenticator.SetAuthenticationMethod("ticket");
                mockSessionAuthenticator.SetChallengeDetails
                    (new MyChallenge {
                    President = "Obama"
                });

                return(mockSessionAuthenticator);
            });

            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            string           clientAuthMethod       = null;
            ChallengeDetails clientChallengeDetails = null;

            Mock <IWampClient <JToken> > clientMock = new Mock <IWampClient <JToken> >();

            clientMock.Setup(x => x.Challenge(It.IsAny <string>(), It.IsAny <ChallengeDetails>()))
            .Callback((string authMethod, ChallengeDetails details) =>
            {
                clientAuthMethod       = authMethod;
                clientChallengeDetails = details;
            });

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(clientMock.Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            MyChallenge deserializedChallengeDetails =
                clientChallengeDetails.OriginalValue.Deserialize <MyChallenge>();

            Assert.That(deserializedChallengeDetails.President, Is.EqualTo("Obama"));
            Assert.That(clientAuthMethod, Is.EqualTo("ticket"));
        }
        public void NotAuthenticatedRaisesAbort()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
                new MockSessionAuthenticationFactory();

            WampPendingClientDetails authenticatorFactoryParameters = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                authenticatorFactoryParameters = clientDetails;
                MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                mockSessionAuthenticator.SetAuthenticationMethod("ticket");

                mockSessionAuthenticator.SetAuthenticate((signature, extraData) =>
                {
                    mockSessionAuthenticator.SetAuthenticationId(clientDetails.HelloDetails.AuthenticationId);
                    mockSessionAuthenticator.SetIsAuthenticated(false);
                });

                return(mockSessionAuthenticator);
            });

            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            Mock <IWampClient <JToken> > clientMock = new Mock <IWampClient <JToken> >();

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(clientMock.Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            serverProxy.Authenticate("Barack Hussein", new AuthenticateExtraData());

            clientMock.Verify(x => x.Abort(It.IsAny <AbortDetails>(), It.IsAny <string>()));
        }
Example #8
0
        private static void SetupHost(WampAuthenticationPlayground playground)
        {
            IWampHost host = playground.Host;

            IWampHostedRealm realm = host.RealmContainer.GetRealmByName("realm1");

            string[] topics = new[]
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };

            foreach (string topic in topics)
            {
                realm.TopicContainer.CreateTopicByUri(topic, true);
            }

            realm.Services.RegisterCallee(new Add2Service());

            host.Open();
        }
        public void WelcomeParametersArePassedToClient()
        {
            MockSessionAuthenticationFactory mockSessionAuthenticationFactory =
                new MockSessionAuthenticationFactory();

            WampPendingClientDetails authenticatorFactoryParameters = null;

            mockSessionAuthenticationFactory.SetGetSessionAuthenticator
                ((clientDetails, transportAuthenticator) =>
            {
                authenticatorFactoryParameters = clientDetails;
                MockSessionAuthenticator mockSessionAuthenticator = new MockSessionAuthenticator();
                mockSessionAuthenticator.SetAuthenticationMethod("ticket");

                mockSessionAuthenticator.SetAuthenticate((signature, extraData) =>
                {
                    mockSessionAuthenticator.SetAuthenticationId(clientDetails.HelloDetails.AuthenticationId);
                    mockSessionAuthenticator.SetIsAuthenticated(true);
                    mockSessionAuthenticator.SetWelcomeDetails(new MyWelcomeDetails()
                    {
                        AuthenticationProvider = "unittest",
                        AuthenticationRole     = "testee",
                        Country = "United States of America"
                    });

                    mockSessionAuthenticator.SetAuthorizer(new WampStaticAuthorizer(new List <WampUriPermissions>()));
                });

                return(mockSessionAuthenticator);
            });

            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground(mockSessionAuthenticationFactory);

            playground.Host.Open();

            long?          clientSessionId      = null;
            WelcomeDetails clientWelcomeDetails = null;

            Mock <IWampClient <JToken> > clientMock = new Mock <IWampClient <JToken> >();

            clientMock.Setup(x => x.Welcome(It.IsAny <long>(), It.IsAny <WelcomeDetails>()))
            .Callback((long sessionId, WelcomeDetails details) =>
            {
                clientSessionId      = sessionId;
                clientWelcomeDetails = details;
            });

            IWampServerProxy serverProxy =
                playground.CreateRawConnection(clientMock.Object);

            serverProxy.Hello("realm1", new HelloDetailsHack()
            {
                AuthenticationId      = "joe",
                AuthenticationMethods = new string[] { "wampcra", "ticket" }
            });

            serverProxy.Authenticate("Barack Hussein", new AuthenticateExtraData());

            Assert.That(clientWelcomeDetails.AuthenticationMethod, Is.EqualTo("ticket"));
            Assert.That(clientWelcomeDetails.AuthenticationId, Is.EqualTo("joe"));
            Assert.That(clientWelcomeDetails.AuthenticationProvider, Is.EqualTo("unittest"));
            Assert.That(clientWelcomeDetails.AuthenticationRole, Is.EqualTo("testee"));
            Assert.That(clientSessionId, Is.EqualTo(authenticatorFactoryParameters.SessionId));

            MyWelcomeDetails deserializedWelcomeDetails =
                clientWelcomeDetails.OriginalValue.Deserialize <MyWelcomeDetails>();

            Assert.That(deserializedWelcomeDetails.Country, Is.EqualTo("United States of America"));
        }
Example #10
0
        public async Task AuthenticatedIntegrationTest()
        {
            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground
                    (new WampCraUserDbAuthenticationFactory
                        (new MyAuthenticationProvider(),
                        new MyUserDb()));

            SetupHost(playground);

            IWampClientAuthenticator authenticator =
                new WampCraClientAuthenticator(authenticationId: "peter", secret: "secret1");

            IWampChannel channel =
                playground.CreateNewChannel("realm1", authenticator);

            IWampRealmProxy realmProxy = channel.RealmProxy;

            await channel.Open().ConfigureAwait(false);

            // call a procedure we are allowed to call (so this should succeed)
            //
            IAdd2AsyncService proxy = realmProxy.Services.GetCalleeProxy <IAdd2AsyncService>();

            int five = await proxy.Add2(2, 3).ConfigureAwait(false);

            Assert.That(five, Is.EqualTo(5));

            // (try to) register a procedure where we are not allowed to (so this should fail)
            //
            Mul2Service service = new Mul2Service();

            WampException registerException = null;

            try
            {
                await realmProxy.Services.RegisterCallee(service)
                .ConfigureAwait(false);
            }
            catch (WampException ex)
            {
                registerException = ex;
            }

            Assert.That(registerException, Is.Not.Null);

            // (try to) publish to some topics
            //
            string[] topics =
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };

            List <string> successfulTopics = new List <string>();

            foreach (string topic in topics)
            {
                IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);

                try
                {
                    await topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                             new object[] { "hello" })
                    .ConfigureAwait(false);

                    successfulTopics.Add(topic);
                }
                catch (WampException ex)
                {
                }
            }

            Assert.That(successfulTopics, Is.EquivalentTo(new string[]
            {
                "com.foobar.topic1",
                "com.example.topic1"
            }));
        }