protected virtual OperationResponse ConnectAndAuthenticate(UnifiedTestClient client, string address,
                                                                   string userName, Dictionary <byte, object> authParameter = null, bool reuseToken = false, short expectedErrorCode = 0)
        {
            if (client.Connected)
            {
                client.Disconnect();
            }

            if (!reuseToken &&
                address == this.MasterAddress
#pragma warning disable CS0618 // Type or member is obsolete
                && this.authPolicy == AuthPolicy.AuthOnMaster)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                client.Token = string.Empty;
            }

            client.OperationResponseQueueClear();
            client.EventQueueClear();

            if (authParameter == null)
            {
                authParameter = new Dictionary <byte, object>();
            }

            if (this.authPolicy != AuthPolicy.UseAuthOnce ||
                this.NameServerAddress == address)
            {
                ConnectToServer(client, address);
                if (this.authPolicy != AuthPolicy.UseAuthOnce)
                {
                    return(client.Authenticate(userName, authParameter, expectedErrorCode));
                }

                return(client.AuthOnce(userName, authParameter, expectedErrorCode));
            }

            client.ConnectWithAuthOnce(address, expectedErrorCode);
            return(null);
        }
        public virtual void ConnectAndAuthenticate(UnifiedTestClient client, string address, string userName, Dictionary<byte, object> authParameter = null, bool reuseToken  = false)
        {
            if (client.Connected)
            {
                client.Disconnect();
            }

            if (!reuseToken && address == this.MasterAddress)
            {
                client.Token = String.Empty;
            }

            client.OperationResponseQueueClear();
            client.EventQueueClear();

            this.ConnectToServer(client, address);

            if (authParameter == null)
            {
                authParameter = new Dictionary<byte, object>();
            }

            client.Authenticate(userName, authParameter);
        }
Ejemplo n.º 3
0
        public virtual void ConnectAndAuthenticate(UnifiedTestClient client, string address, string userName, Dictionary <byte, object> authParameter = null, bool reuseToken = false)
        {
            if (client.Connected)
            {
                client.Disconnect();
            }

            if (!reuseToken && address == this.MasterAddress)
            {
                client.Token = String.Empty;
            }

            client.OperationResponseQueueClear();
            client.EventQueueClear();

            this.ConnectToServer(client, address);

            if (authParameter == null)
            {
                authParameter = new Dictionary <byte, object>();
            }

            client.Authenticate(userName, authParameter);
        }
Ejemplo n.º 4
0
        public void SecureParamsTest()
        {
            UnifiedTestClient client    = null;
            UnifiedTestClient client2   = null;
            const int         SleepTime = 350;

            try
            {
                client        = (UnifiedTestClient)this.CreateTestClient();
                client.UserId = Player1;

                this.ConnectToServer(client, this.MasterAddress);

                var response = client.Authenticate(Player1, new Dictionary <byte, object>()
                {
                    { (byte)ParameterKey.ClientAuthenticationParams, "username=yes&token=yes" }
                });

                Assert.AreEqual("nick", response[(byte)ParameterKey.Nickname]);
                Assert.IsNotNull(response[(byte)ParameterKey.Token]);


                var request = new OperationRequest
                {
                    OperationCode = OperationCode.CreateGame,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.RoomName, "TestGame" },
                        { ParameterCode.Plugins, new string[] { "CheckSecurePlugin" } },
                    }
                };

                response = client.SendRequestAndWaitForResponse(request);

                this.ConnectAndAuthenticate(client, (string)response[ParameterCode.Address], client.UserId, new Dictionary <byte, object>()
                {
                    { (byte)ParameterKey.ClientAuthenticationParams, "username=yes&token=yes" }
                });

                client.SendRequestAndWaitForResponse(request);

                Thread.Sleep(SleepTime);
                CheckSecure("CreateGameAuthCookie");

                client2 = this.CreateMasterClientAndAuthenticate("User2", new Dictionary <byte, object>()
                {
                    { (byte)ParameterKey.ClientAuthenticationParams, "username=yes&token=yes" }
                });

                request = new OperationRequest
                {
                    OperationCode = OperationCode.JoinGame,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.RoomName, "TestGame" },
                    }
                };

                response = client2.SendRequestAndWaitForResponse(request);

                this.ConnectAndAuthenticate(client2, (string)response[ParameterCode.Address], client2.UserId, new Dictionary <byte, object>()
                {
                    { (byte)ParameterKey.ClientAuthenticationParams, "username=yes&token=yes" }
                });

                client2.SendRequestAndWaitForResponse(request);

                Thread.Sleep(SleepTime);
                CheckSecure("JoinGameAuthCookie");

                request = new OperationRequest
                {
                    OperationCode = OperationCode.RaiseEvent,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Code, (byte)1 },
                        { ParameterCode.EventForward, (byte)3 },
                    }
                };

                client2.SendRequest(request);

                Thread.Sleep(SleepTime);
                CheckSecure("RaiseEventAuthCookie");

                //just to ensure that there is nothing on server for RaiseEventAuthCookie
                CheckSecure("RaiseEventAuthCookie", expectToFail: true);

                request = new OperationRequest
                {
                    OperationCode = OperationCode.RaiseEvent,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Code, (byte)1 },
                        { ParameterCode.EventForward, (byte)1 },// we send request but without secure
                    }
                };

                client2.SendRequest(request);
                CheckSecure("RaiseEventAuthCookie", expectToFail: true);


                client.SendRequestAndWaitForResponse(new OperationRequest
                {
                    OperationCode = OperationCode.SetProperties,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Properties, new Hashtable {
                              { GamePropertyKey.IsOpen, true }
                          } },
                        { ParameterCode.EventForward, (byte)3 },
                    }
                });

                Thread.Sleep(SleepTime);
                CheckSecure("SetPropertiesAuthCookie");

                client.SendRequestAndWaitForResponse(new OperationRequest
                {
                    OperationCode = OperationCode.WebRpc,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Data, new Hashtable {
                              { GamePropertyKey.IsOpen, true }
                          } },
                        { ParameterCode.EventForward, (byte)3 },
                        { ParameterCode.UriPath, "RpcSecure" },
                    }
                });

                Thread.Sleep(SleepTime);
                CheckSecure("RpcAuthCookie");

                client.SendRequestAndWaitForResponse(new OperationRequest
                {
                    OperationCode = OperationCode.WebRpc,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Data, new Hashtable {
                              { GamePropertyKey.IsOpen, true }
                          } },
                        { ParameterCode.UriPath, "RpcSecure" },
                    }
                });

                Thread.Sleep(SleepTime);
                CheckSecure("RpcAuthCookie", true);

                var client3 = this.CreateMasterClientAndAuthenticate("User3", new Dictionary <byte, object>()
                {
                    { (byte)ParameterKey.ClientAuthenticationParams, "username=yes&token=yes" }
                });
                client3.SendRequestAndWaitForResponse(new OperationRequest
                {
                    OperationCode = OperationCode.WebRpc,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Data, new Hashtable {
                              { GamePropertyKey.IsOpen, true }
                          } },
                        { ParameterCode.UriPath, "RpcSecure" },
                        { ParameterCode.EventForward, (byte)3 },
                    }
                });

                Thread.Sleep(SleepTime);
                CheckSecure("RpcAuthCookie");

                client3.SendRequestAndWaitForResponse(new OperationRequest
                {
                    OperationCode = OperationCode.WebRpc,
                    Parameters    = new Dictionary <byte, object>
                    {
                        { ParameterCode.Data, new Hashtable {
                              { GamePropertyKey.IsOpen, true }
                          } },
                        { ParameterCode.UriPath, "RpcSecure" },
                        { ParameterCode.EventForward, (byte)1 },
                    }
                });

                Thread.Sleep(SleepTime);
                CheckSecure("RpcAuthCookie", expectToFail: true);
            }
            finally
            {
                DisposeClients(client, client2);
            }
        }