Ejemplo n.º 1
0
        public AuthenticationResult Authenticate(IUnsafePacketReader credentialsReader, out IAccountSession session, out Account account)
        {
            session = null;
            account = null;

            var username = credentialsReader.ReadLengthString();

            if (username != "admin")
            {
                return(AuthenticationResult.NotRegistered);
            }

            var password = credentialsReader.ReadLengthString();

            if (password != "admin")
            {
                return(AuthenticationResult.IncorrectPassword);
            }

            session = new StubAccountSession();
            account = new Account()
            {
                AccountId       = 1,
                UserName        = "******",
                Password        = "******",
                Gender          = Gender.Male,
                GameMasterLevel = GameMasterLevel.GameMaster,
                Status          = AccountStatus.Active,
                AccountPin      = "0000",
            };
            return(AuthenticationResult.Success);
        }
Ejemplo n.º 2
0
 private bool CheckPin(IUnsafePacketReader reader)
 {
     string suggested = reader.ReadLengthString();
     string expected = this.Account.AccountPin;
     var isValid = suggested == expected;
     return isValid;
 }
Ejemplo n.º 3
0
 private bool CheckPin(IUnsafePacketReader reader)
 {
     string suggested = reader.ReadLengthString();
     string expected = Account.AccountPin;
     var isValid = suggested == expected;
     return isValid;
 }
Ejemplo n.º 4
0
        public AuthenticationResult Authenticate(IUnsafePacketReader credentialsReader, out IAccountSession session, out Account account)
        {
            session = null;
            account = null;

            var username = credentialsReader.ReadLengthString();
            if (username != "admin")
            {
                return AuthenticationResult.NotRegistered;
            }

            var password = credentialsReader.ReadLengthString();
            if (password != "admin")
            {
                return AuthenticationResult.IncorrectPassword;
            }

            session = new StubAccountSession();
            account = new Account()
                      {
                          AccountId = 1,
                          UserName = "******",
                          Password = "******",
                          Gender = Gender.Male,
                          GameMasterLevel = GameMasterLevel.GameMaster,
                          Status = AccountStatus.Active,
                          AccountPin = "0000",
                      };
            return AuthenticationResult.Success;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads a byte corresponding to a <typeparamref name="TEnum" /> value decorated with <see cref="PacketValueAttribute"/>.
        /// </summary>
        /// <typeparam name="TEnum">The target <see langword="enum" /> type.</typeparam>
        /// <param name="reader">The reader to use.</param>
        /// <inheritdoc cref="PacketReader.ReadByte()" select="exception[@cref='PacketReadingException']" />
        /// <returns>the corresponding <typeparamref name="TEnum" /> value.</returns>
        public static TEnum ReadByte <TEnum>(this IUnsafePacketReader reader)
            where TEnum : struct
        {
            Guard.NotNull(() => reader, reader);

            return(reader.ReadByte().ToEnumValue <TEnum>());
        }
Ejemplo n.º 6
0
        // You may use these as samples for writing custom readers/writers.
        // Just write an extension method to PacketReader or PacketBuilder (or one of their interfaces like below) and it works! Magic!

        /// <summary>
        /// Reads a <see cref="PointS"/>.
        /// </summary>
        /// <param name="reader">The <see cref="IUnsafePacketReader">packet reader</see> to use.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="reader"/> is <see langword="null"/>.</exception>
        /// <returns>a <see cref="PointS"/> that was read.</returns>
        public static PointS ReadVector(this IUnsafePacketReader reader)
        {
            Guard.NotNull(() => reader, reader);

            var x = reader.ReadInt16();
            var y = reader.ReadInt16();

            return(new PointS(x, y));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads a flags instance.
        /// </summary>
        /// <typeparam name="TFlags">The <see cref="Flags"/>-derived type to read into.</typeparam>
        /// <param name="reader">The reader to use.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="reader"/> is <see langword="null"/>.</exception>
        public static TFlags ReadFlags <TFlags>(this IUnsafePacketReader reader)
            where TFlags : Flags, new()
        {
            Guard.NotNull(() => reader, reader);

            var flags = new TFlags();

            flags.Read(reader);
            return(flags);
        }
Ejemplo n.º 8
0
        private void HandleAuthentication(IUnsafePacketReader reader)
        {
            if (State != AuthClientState.NotLoggedIn)
            {
                Disconnect("Invalid client authentication state.");
                return;
            }

            // TODO: more stuff to read, later.
            IAccountSession accountSession;
            Account         account;
            var             result = _authenticator.Authenticate(reader, out accountSession, out account);

            if (result == AuthenticationResult.Success)
            {
                AccountSession = accountSession;
                Account        = account;

                if (!account.Gender.HasValue)
                {
                    State = AuthClientState.SetGender;
                }
                else
                {
                    // TODO: cover cases where further authentication is not required and this should be set to LoggedIn.
                    if (account.AccountPin == null)
                    {
                        State = AuthClientState.SetPin;
                    }
                    else
                    {
                        State = AuthClientState.AskPin;
                    }
                }
            }
            else if (LoginAttempts++ > MaxLoginAttempts)
            {
                Disconnect("Too many login attempts.");
                return;
            }

            ServerSession.WritePacket(AuthResponse(result, account));
            ServerSession.WritePacket(CheckPinResponse());
        }
Ejemplo n.º 9
0
        private void HandlePinValidation(IUnsafePacketReader reader)
        {
            // TODO: Configuring the server to not require PINs.

            var action = reader.ReadByte <PinRequestType>();

            reader.Skip(5);

            switch (action)
            {
            case PinRequestType.PinNotSet:
                State = AuthClientState.AskPin;

                ServerSession.WritePacket(SetPinResponse());
                break;

            case PinRequestType.CheckPin:
                if (CheckPin(reader))
                {
                    State = AuthClientState.LoggedIn;

                    ServerSession.WritePacket(PinAcceptedResponse());
                }
                else
                {
                    ServerSession.WritePacket(InvalidPinResponse());
                }
                break;

            case PinRequestType.AssignPin:
                if (CheckPin(reader))
                {
                    State = AuthClientState.LoggedIn;

                    ServerSession.WritePacket(SetPinResponse());
                }
                else
                {
                    ServerSession.WritePacket(InvalidPinResponse());
                }
                break;
            }
        }
Ejemplo n.º 10
0
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="reader"/> is <see langword="null"/>.</exception>
        /// <inheritdoc />
        public override sealed void Read(IUnsafePacketReader reader)
        {
            Guard.NotNull(() => reader, reader);

            int bitCount = this.Bits.Length;
            int numberCount = bitCount / LongBitCount;

            for (int i = 0; i < numberCount; i++)
            {
                ulong number = reader.ReadUInt64();
                int startIndex = i * LongBitCount;
                int endIndex = Math.Min(startIndex + LongBitCount, bitCount);
                for (int j = startIndex; j < endIndex; j++)
                {
                    this.Bits[j] = Convert.ToBoolean(number & 1);
                    number >>= 1;
                }
            }
        }
Ejemplo n.º 11
0
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="reader"/> is <see langword="null"/>.</exception>
        /// <inheritdoc />
        public sealed override void Read(IUnsafePacketReader reader)
        {
            Guard.NotNull(() => reader, reader);

            int bitCount    = Bits.Length;
            int numberCount = bitCount / LongBitCount;

            for (int i = 0; i < numberCount; i++)
            {
                ulong number     = reader.ReadUInt64();
                int   startIndex = i * LongBitCount;
                int   endIndex   = Math.Min(startIndex + LongBitCount, bitCount);
                for (int j = startIndex; j < endIndex; j++)
                {
                    Bits[j]  = Convert.ToBoolean(number & 1);
                    number >>= 1;
                }
            }
        }
Ejemplo n.º 12
0
        /// <inheritdoc />
        public AuthenticationResult Authenticate(IUnsafePacketReader credentialsReader, out IAccountSession session, out Account account)
        {
            // Default value for failure scenarios:
            session = null;

            // TODO: user name validation, throw IllegalPacketException if not valid
            var userName = credentialsReader.ReadLengthString();

            // Attempt to load the account.
            account = _accountProvider.LoadByUserName(userName);
            if (account == null)
            {
                // Fail with 'NotRegistered' if no account matches.
                return(AuthenticationResult.NotRegistered);
            }

            // TODO: password validation, throw IllegalPacketException if not valid
            var password = credentialsReader.ReadLengthString();

            string hash = LoginCrypto.GetMd5HashString(password, true);

            if (!string.Equals(hash, account.Password, StringComparison.Ordinal))
            {
                // Fail with 'IncorrectPassword' if password hash is bad.
                return(AuthenticationResult.IncorrectPassword);
            }

            // TODO: read other stuff from packet

            int sessionId;

            if (!_accountService.TryRegisterSession(account.AccountId, out sessionId))
            {
                // Fail with 'AlreadyLoggedIn' if there is another session running on this account.
                return(AuthenticationResult.AlreadyLoggedIn);
            }

            // Create the session.
            session = new AccountSession(_accountService, sessionId, account);
            return(AuthenticationResult.Success);
        }
Ejemplo n.º 13
0
        /// <inheritdoc />
        public AuthenticationResult Authenticate(IUnsafePacketReader credentialsReader, out IAccountSession session, out Account account)
        {
            // Default value for failure scenarios:
            session = null;

            // TODO: user name validation, throw IllegalPacketException if not valid
            var userName = credentialsReader.ReadLengthString();
            // Attempt to load the account.
            account = this.accountProvider.LoadByUserName(userName);
            if (account == null)
            {
                // Fail with 'NotRegistered' if no account matches.
                return AuthenticationResult.NotRegistered;
            }

            // TODO: password validation, throw IllegalPacketException if not valid
            var password = credentialsReader.ReadLengthString();

            string hash = LoginCrypto.GetMd5HashString(password, true);
            if (!string.Equals(hash, account.Password, StringComparison.Ordinal))
            {
                // Fail with 'IncorrectPassword' if password hash is bad.
                return AuthenticationResult.IncorrectPassword;
            }

            // TODO: read other stuff from packet

            int sessionId;
            if (!this.accountService.TryRegisterSession(account.AccountId, out sessionId))
            {
                // Fail with 'AlreadyLoggedIn' if there is another session running on this account.
                return AuthenticationResult.AlreadyLoggedIn;
            }

            // Create the session.
            session = new AccountSession(this.accountService, sessionId, account);
            return AuthenticationResult.Success;
        }
Ejemplo n.º 14
0
 private static int FailingReturningRead(IUnsafePacketReader reader)
 {
     reader.Skip(reader.Remaining - 1);
     return(reader.ReadInt32());
 }
Ejemplo n.º 15
0
 private void HandleWorldListRequest(IUnsafePacketReader reader)
 {
     ServerSession.WritePacket(WorldListResponse());
 }
Ejemplo n.º 16
0
 private void HandleCharacterListRequest(IUnsafePacketReader reader)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Reads the flag bits from the specified <see cref="IUnsafePacketReader"/>.
 /// </summary>
 /// <param name="reader">The reader to use.</param>
 public abstract void Read(IUnsafePacketReader reader);
Ejemplo n.º 18
0
 private void HandleWorldListRequest(IUnsafePacketReader reader)
 {
     this.ServerSession.WritePacket(this.WorldListResponse());
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Reads the flag bits from the specified <see cref="IUnsafePacketReader"/>.
 /// </summary>
 /// <param name="reader">The reader to use.</param>
 public abstract void Read(IUnsafePacketReader reader);
Ejemplo n.º 20
0
 private void HandlePinAssignment(IUnsafePacketReader reader)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 private void HandleCharacterSelect(IUnsafePacketReader reader)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 22
0
        private void HandleAuthentication(IUnsafePacketReader reader)
        {
            if (this.State != AuthClientState.NotLoggedIn)
            {
                this.Disconnect("Invalid client authentication state.");
                return;
            }

            // TODO: more stuff to read, later.
            IAccountSession accountSession;
            Account account;
            var result = this.authenticator.Authenticate(reader, out accountSession, out account);
            if (result == AuthenticationResult.Success)
            {
                this.AccountSession = accountSession;
                this.Account = account;

                if (!account.Gender.HasValue)
                {
                    this.State = AuthClientState.SetGender;
                }
                else
                {
                    // TODO: cover cases where further authentication is not required and this should be set to LoggedIn.
                    if (account.AccountPin == null)
                    {
                        this.State = AuthClientState.SetPin;
                    }
                    else
                    {
                        this.State = AuthClientState.AskPin;
                    }
                }
            }
            else if (this.LoginAttempts++ > MaxLoginAttempts)
            {
                this.Disconnect("Too many login attempts.");
                return;
            }

            this.ServerSession.WritePacket(this.AuthResponse(result, account));
            this.ServerSession.WritePacket(this.CheckPinResponse());
        }
Ejemplo n.º 23
0
 private static int FailingReturningRead(IUnsafePacketReader reader)
 {
     reader.Skip(reader.Remaining - 1);
     return reader.ReadInt32();
 }
Ejemplo n.º 24
0
 private void HandleChannelSelect(IUnsafePacketReader reader)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 25
0
 private static void FailingRead(IUnsafePacketReader reader)
 {
     reader.Skip(reader.Remaining - 1);
     reader.ReadInt32();
 }
Ejemplo n.º 26
0
        private void HandlePinValidation(IUnsafePacketReader reader)
        {
            // TODO: Configuring the server to not require PINs.

            var action = reader.ReadByte<PinRequestType>();

            reader.Skip(5);

            switch (action)
            {
                case PinRequestType.PinNotSet:
                    this.State = AuthClientState.AskPin;

                    this.ServerSession.WritePacket(this.SetPinResponse());
                    break;

                case PinRequestType.CheckPin:
                    if (this.CheckPin(reader))
                    {
                        this.State = AuthClientState.LoggedIn;

                        this.ServerSession.WritePacket(this.PinAcceptedResponse());
                    }
                    else
                    {
                        this.ServerSession.WritePacket(this.InvalidPinResponse());
                    }
                    break;

                case PinRequestType.AssignPin:
                    if (this.CheckPin(reader))
                    {
                        this.State = AuthClientState.LoggedIn;

                        this.ServerSession.WritePacket(this.SetPinResponse());
                    }
                    else
                    {
                        this.ServerSession.WritePacket(this.InvalidPinResponse());
                    }
                    break;
            }
        }
Ejemplo n.º 27
0
 private void HandlePinAssignment(IUnsafePacketReader reader)
 {
     throw new NotImplementedException();
 }