Exemplo n.º 1
0
        public void SetUp()
        {
            ManualResetEvent evt   = new ManualResetEvent(false);
            INError          error = null;

            client = new NClient.Builder(DefaultServerKey).Build();
            var message = NAuthenticateMessage.Device(DeviceId);

            client.Login(message, (INSession session) =>
            {
                userId = session.Id;
                client.Connect(session);
                evt.Set();
            }, (INError err) => {
                client.Register(message, (INSession session) =>
                {
                    userId = session.Id;
                    client.Connect(session);
                    evt.Set();
                }, (INError err2) => {
                    error = err2;
                    evt.Set();
                });
            });

            evt.WaitOne(1000, false);
            Assert.IsNull(error);
        }
    private void LoginOrRegister()
    {
        // See if we have a cached id in PlayerPrefs.
        var id = PlayerPrefs.GetString("nk.id");

        if (string.IsNullOrEmpty(id))
        {
            // We'll use device ID for the user account. Have a look at the docs
            // for other options: https://heroiclabs.com/docs/development/authentication/
            id = SystemInfo.deviceUniqueIdentifier;
            // Store the identifier for next game start.
            PlayerPrefs.SetString("nk.id", id);
        }

        var message = NAuthenticateMessage.Device(id);

        _client.Login(message, SessionHandler, (INError error) => {
            if (error.Message.Equals("ID not found"))
            {
                _client.Register(message, SessionHandler, ErrorHandler);
                return;
            }
            Debug.LogErrorFormat("Login error: code '{0}' - '{1}'.", error.Code, error.Message);
        });
    }
Exemplo n.º 3
0
        // This method connects the client to the server and
        // if neccessary authenticates with the server
        public void Connect(INAuthenticateMessage request)
        {
            // Check to see if we have a valid session token we can restore
            var session = RestoreSession();

            if (session != null)
            {
                // Session is valid, let's connect.
                _sessionHandler(session);
                return;
            }

            // Cache message for later use for reconnecting purposes in case the session had expired
            _authenticateMessage = request;

            // Let's login to authenticate and get a valid token
            _client.Login(request, _sessionHandler, err =>
            {
                if (err.Code == ErrorCode.UserNotFound)
                {
                    _client.Register(request, _sessionHandler, ErrorHandler);
                }
                else
                {
                    ErrorHandler(err);
                }
            });
        }
Exemplo n.º 4
0
        public void LoginGoogle_Invalid([ValueSource("BlankCases")] string oauthToken)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Google(oauthToken);

            client.Login(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            if (oauthToken == string.Empty)
            {
                Assert.AreEqual("Access token is required", result.Message);
            }
            else
            {
                Assert.AreEqual("Invalid Google access token, no spaces or control characters allowed", result.Message);
            }
        }
Exemplo n.º 5
0
        public void LoginEmail_Valid()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INSession        result = null;

            string   piece    = random.GetString().Substring(8);
            string   email    = String.Format("{0}@{0}.com", piece);
            string   password = TestContext.CurrentContext.Random.GetString();
            INClient client   = NClient.Default(DefaultServerKey);
            var      message  = NAuthenticateMessage.Email(email, password);

            client.Register(message, (INSession _) =>
            {
                client.Login(message, (INSession session) =>
                {
                    result = session;
                    evt.Set();
                }, (INError error) =>
                {
                    evt.Set();
                });
            }, (INError error) =>
            {
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
Exemplo n.º 6
0
        public void LoginEmail_Invalid([ValueSource("BlankCases")] string email,
                                       [ValueSource("BlankCases")] string password)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Email(email, password);

            client.Login(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            if (email == string.Empty)
            {
                Assert.AreEqual("Email address is required", result.Message);
            }
            else
            {
                Assert.AreEqual("Invalid email address, no spaces or control characters allowed", result.Message);
            }
        }
Exemplo n.º 7
0
        public void LoginDevice_Valid()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INSession        result = null;

            string   id      = random.GetString();
            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Device(id);

            client.Register(message, (INSession _) =>
            {
                client.Login(message, (INSession session) =>
                {
                    result = session;
                    evt.Set();
                }, (INError error) =>
                {
                    evt.Set();
                });
            }, (INError error) =>
            {
                evt.Set();
            });

            evt.WaitOne(1000, false);
            Assert.NotNull(result);
            Assert.NotNull(result.Token);
        }
    public void ClientLogin()
    {
        if (userNameInputField.text.Length < 10)
        {
            loginErrorString = "Invalid UserName. Must be at least 10 characters. UserName entered was only " + userNameInputField.text.Length + " characters";
            return;
        }
        if (serverIPInputField.text.Length < 11)
        {
            loginErrorString = "Invalid IP!";
            return;
        }

        ServerIP = serverIPInputField.text;

        ClientBuilder(); //Build the Nakama client object

        deviceID = userNameInputField.text;
        Debug.Log("Logging in as: " + deviceID + " to IP: " + ServerIP);
        PlayerPrefs.SetString("UserName", deviceID);

        //Allows us to wait until the register check is complete before moving onto login
        ManualResetEvent loginEvent = new ManualResetEvent(false);

        ////Try to Login
        var request = NAuthenticateMessage.Device(deviceID);

        client.Login(request, (INSession session) =>
        {
            if (ServerInstance.session == session)
            {
                loginErrorString = "This user is already logged in!";
                loginEvent.Set();
                return;
            }
            //outputText += "Player logged in successfully. ID: " + deviceID.ToString() + "\n";
            this.session           = session;
            ServerInstance.session = session;
            client.Connect(session);
            clientConnected = true;
            NakamaData.Singleton.Session = session;
            loginEvent.Set();
        }, (INError err) =>
        {
            loginErrorString = "Player failed to login! ID: " + deviceID.ToString() + " Error: " + err.ToString() + "\n";
            loginEvent.Set();
        });

        loginEvent.WaitOne(1000, false);

        if (clientConnected)
        {
            FetchClientInfo();
            UpdateUserInfo();
            JoinDefaultRoom();
            CompleteLogin();
        }
    }
Exemplo n.º 9
0
    public void Player2LoginClick()
    {
        var request = NAuthenticateMessage.Email(EMAIL, PASSWORD);

        client2.Login(request, (INSession session) => {
            Debug.Log("Player 2 Logged in successfully");
            this.session2 = session;
            RegisterButtonPlayer2Enable = false;
            LoginButtonPlayer2Enable    = false;
            ConnectPlayer2Enable        = true;
        }, (INError error) => {
            Debug.LogErrorFormat("Email login '{0}' failed: {1}", EMAIL, error);
        });
    }
Exemplo n.º 10
0
    public void Player1LoginClick()
    {
        string id      = SystemInfo.deviceUniqueIdentifier;
        var    request = NAuthenticateMessage.Device(id);

        client1.Login(request, (INSession session) => {
            Debug.Log("Player 1 Logged in successfully");
            this.session1 = session;
            RegisterButtonPlayer1Enable = false;
            LoginButtonPlayer1Enable    = false;
            ConnectPlayer1Enable        = true;
        }, (INError error) => {
            Debug.LogErrorFormat("ID login '{0}' failed: {1}", id, error);
        });
    }
Exemplo n.º 11
0
        public void LoginSteam_Invalid([ValueSource("BlankCases")] string sessionToken)
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Steam(sessionToken);

            client.Login(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            Assert.AreEqual("Steam login not available", result.Message);
        }
Exemplo n.º 12
0
        public void LoginDevice_Invalid_NoId()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            string   id      = random.GetString();
            INClient client  = NClient.Default(DefaultServerKey);
            var      message = NAuthenticateMessage.Device(id);

            client.Login(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            Assert.AreEqual("ID not found", result.Message);
        }
Exemplo n.º 13
0
        // +++ ICustomAuthenticator implementation ++++++++++++++++++++++++++++++++++++++++++++++++++++
        public void Login(object userData, Action <INSession> successCallback, Action <INError> failCallback)
        {
            string id;

            if (userData == null)
            {
                id = System.Guid.NewGuid().ToString();
            }
            else
            {
                if (userData.GetType() != typeof(Guid))
                {
                    throw new ArgumentException("Userdata has to be typeof (Guid).");
                }
                id = ((Guid)userData).ToString();
            }

            var authenticationMessage = NAuthenticateMessage.Device(id);

            _client.Login(authenticationMessage,
                          successCallback,
                          (INError error) =>
            {
                if (error.Code == ErrorCode.UserNotFound)
                {
                    _client.Register(
                        authenticationMessage,
                        successCallback,
                        failCallback
                        );
                }
                else
                {
                    failCallback(error);
                }
            }
                          );
        }
    public void LoginOrRegisterWithCustomId(string customId, Action onLoginSucceeded, Action onLoginError)
    {
        var message = NAuthenticateMessage.Custom(customId);

        _client.Login(message,
                      (INSession session) =>
        {
            Debug.Log("Login Callback");
            SessionHandler(session);
        },
                      (INError err) =>
        {
            if (err.Code == ErrorCode.UserNotFound)
            {
                _client.Register(message, SessionHandler, ErrorHandler);
            }
            else
            {
                ErrorHandler(err);
            }
        }
                      );
    }
Exemplo n.º 15
0
        public void LoginEmail_Invalid_Email()
        {
            ManualResetEvent evt    = new ManualResetEvent(false);
            INError          result = null;

            string   email    = random.GetString();
            string   password = random.GetString();
            INClient client   = NClient.Default(DefaultServerKey);
            var      message  = NAuthenticateMessage.Email(email, password);

            client.Login(message, (INSession session) =>
            {
                evt.Set();
            }, (INError error) =>
            {
                result = error;
                evt.Set();
            });

            evt.WaitOne(500, false);
            Assert.NotNull(result);
            Assert.AreEqual("Invalid email address format", result.Message);
        }
    void LoginWithDeviceId()
    {
        var message = NAuthenticateMessage.Device(this.GetId());

        client.Login(message, OnServerSessionCreatedOrRestored, OnLoginError);
    }