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); }
private bool CheckPin(IUnsafePacketReader reader) { string suggested = reader.ReadLengthString(); string expected = this.Account.AccountPin; var isValid = suggested == expected; return isValid; }
private bool CheckPin(IUnsafePacketReader reader) { string suggested = reader.ReadLengthString(); string expected = Account.AccountPin; var isValid = suggested == expected; return isValid; }
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; }
/// <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>()); }
// 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)); }
/// <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); }
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()); }
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; } }
/// <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; } } }
/// <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; } } }
/// <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); }
/// <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; }
private static int FailingReturningRead(IUnsafePacketReader reader) { reader.Skip(reader.Remaining - 1); return(reader.ReadInt32()); }
private void HandleWorldListRequest(IUnsafePacketReader reader) { ServerSession.WritePacket(WorldListResponse()); }
private void HandleCharacterListRequest(IUnsafePacketReader reader) { throw new NotImplementedException(); }
/// <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);
private void HandleWorldListRequest(IUnsafePacketReader reader) { this.ServerSession.WritePacket(this.WorldListResponse()); }
private void HandlePinAssignment(IUnsafePacketReader reader) { throw new NotImplementedException(); }
private void HandleCharacterSelect(IUnsafePacketReader reader) { throw new NotImplementedException(); }
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()); }
private static int FailingReturningRead(IUnsafePacketReader reader) { reader.Skip(reader.Remaining - 1); return reader.ReadInt32(); }
private void HandleChannelSelect(IUnsafePacketReader reader) { throw new NotImplementedException(); }
private static void FailingRead(IUnsafePacketReader reader) { reader.Skip(reader.Remaining - 1); reader.ReadInt32(); }
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; } }