-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auto login, auto register, and flipped username handling to Rue (#80
) * initial work on login side of Rue plugin * move autoregister to login plugin * Update Rue login plugin to use updated IPluginHost * Add config loading, cleanup code, add IsFlippedUsername config and handling * Fix nullable --------- Co-authored-by: Keith <[email protected]>
- Loading branch information
Showing
20 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,7 @@ | |
"Worlds": [ | ||
0, | ||
1 | ||
], | ||
"IsAutoRegister": true, | ||
"IsFlippedUsername": false | ||
] | ||
} | ||
], | ||
"GameStages": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/common/Edelstein.Common.Gameplay.Login/Handlers/CreateSecurityHandleHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Edelstein.Common.Gameplay.Packets; | ||
using Edelstein.Protocol.Gameplay.Login.Contracts; | ||
using Edelstein.Protocol.Gameplay.Login; | ||
using Edelstein.Protocol.Utilities.Packets; | ||
using Edelstein.Protocol.Utilities.Pipelines; | ||
|
||
namespace Edelstein.Common.Gameplay.Login.Handlers; | ||
|
||
public class CreateSecurityHandleHandler : AbstractPipedPacketHandler<ILoginStageUser, UserOnPacketCreateSecurityHandle> | ||
{ | ||
public CreateSecurityHandleHandler(IPipeline<UserOnPacketCreateSecurityHandle> pipeline) : base(pipeline) | ||
{ | ||
} | ||
|
||
public override short Operation => (short)PacketRecvOperations.CreateSecurityHandle; | ||
|
||
public override bool Check(ILoginStageUser user) => user.State == LoginState.CheckPassword; | ||
|
||
public override UserOnPacketCreateSecurityHandle? Serialize(ILoginStageUser user, IPacketReader reader) | ||
=> new(user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Edelstein.Plugin.Rue.Configs; | ||
|
||
public record RueConfigLogin | ||
{ | ||
public bool IsAutoRegister { get; set; } | ||
public bool IsAutoLogin { get; set; } | ||
public bool IsFlippedUsername { get; set; } | ||
|
||
public RueConfigLoginCredentials? LoginCredentials { get; set; } | ||
} |
7 changes: 7 additions & 0 deletions
7
src/plugin/Edelstein.Plugin.Rue/Configs/RueConfigLoginCredentials.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Edelstein.Plugin.Rue.Configs; | ||
|
||
public record RueConfigLoginCredentials | ||
{ | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
} |
14 changes: 14 additions & 0 deletions
14
src/plugin/Edelstein.Plugin.Rue/Contracts/UserOnPacketCheckPasswordFlipped.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Edelstein.Protocol.Gameplay.Login; | ||
using Edelstein.Protocol.Gameplay.Login.Contracts; | ||
|
||
namespace Edelstein.Plugin.Rue.Contracts; | ||
|
||
public record UserOnPacketCheckPasswordFlipped( | ||
ILoginStageUser User, | ||
string Username, | ||
string Password | ||
) : UserOnPacketCheckPassword( | ||
User, | ||
Username, | ||
Password | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/plugin/Edelstein.Plugin.Rue/Plugs/UserOnPacketCheckPasswordAutoLoginPlug.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Edelstein.Plugin.Rue.Configs; | ||
using Edelstein.Protocol.Gameplay.Login.Contexts; | ||
using Edelstein.Protocol.Gameplay.Login.Contracts; | ||
using Edelstein.Protocol.Services.Auth.Contracts; | ||
using Edelstein.Protocol.Utilities.Pipelines; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Edelstein.Plugin.Rue.Plugs; | ||
|
||
public class UserOnPacketCheckPasswordAutoLoginPlug : IPipelinePlug<UserOnPacketCheckPassword> | ||
{ | ||
private readonly ILogger? _logger; | ||
private readonly RueConfigLogin? _config; | ||
private readonly LoginContext _context; | ||
|
||
public UserOnPacketCheckPasswordAutoLoginPlug(ILogger? logger, RueConfigLogin? config, LoginContext context) | ||
{ | ||
_logger = logger; | ||
_config = config; | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(IPipelineContext ctx, UserOnPacketCheckPassword message) | ||
{ | ||
if ((_config?.IsAutoRegister ?? false) && | ||
(await _context.Services.Auth.Login(new AuthRequest(message.Username, message.Password))).Result == AuthResult.FailedInvalidUsername) | ||
{ | ||
await _context.Services.Auth.Register(new AuthRequest(message.Username, message.Password)); | ||
_logger?.LogInformation("Created new user {Username} with password {Password}", message.Username, message.Password); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/plugin/Edelstein.Plugin.Rue/Plugs/UserOnPacketCheckPasswordFlippedPlug.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Edelstein.Plugin.Rue.Configs; | ||
using Edelstein.Plugin.Rue.Contracts; | ||
using Edelstein.Protocol.Gameplay.Login.Contexts; | ||
using Edelstein.Protocol.Gameplay.Login.Contracts; | ||
using Edelstein.Protocol.Services.Auth.Contracts; | ||
using Edelstein.Protocol.Utilities.Pipelines; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Edelstein.Plugin.Rue.Plugs; | ||
|
||
public class UserOnPacketCheckPasswordFlippedPlug : IPipelinePlug<UserOnPacketCheckPassword> | ||
{ | ||
private readonly ILogger? _logger; | ||
private readonly RueConfigLogin? _config; | ||
private readonly LoginContext _context; | ||
|
||
public UserOnPacketCheckPasswordFlippedPlug(ILogger? logger, RueConfigLogin? config, LoginContext context) | ||
{ | ||
_logger = logger; | ||
_config = config; | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(IPipelineContext ctx, UserOnPacketCheckPassword message) | ||
{ | ||
if ((_config?.IsFlippedUsername ?? false) && message is not UserOnPacketCheckPasswordFlipped) | ||
{ | ||
await _context.Pipelines.UserOnPacketCheckPassword.Process(new UserOnPacketCheckPasswordFlipped( | ||
message.User, | ||
message.Password, | ||
message.Username | ||
)); | ||
ctx.Cancel(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/plugin/Edelstein.Plugin.Rue/Plugs/UserOnPacketCreateSecurityHandleAutoRegisterPlug.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Edelstein.Plugin.Rue.Configs; | ||
using Edelstein.Plugin.Rue.Contracts; | ||
using Edelstein.Protocol.Gameplay.Game.Contexts; | ||
using Edelstein.Protocol.Gameplay.Login.Contexts; | ||
using Edelstein.Protocol.Gameplay.Login.Contracts; | ||
using Edelstein.Protocol.Utilities.Pipelines; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Edelstein.Plugin.Rue.Plugs; | ||
|
||
public class UserOnPacketCreateSecurityHandleAutoRegisterPlug : IPipelinePlug<UserOnPacketCreateSecurityHandle> | ||
{ | ||
private readonly ILogger? _logger; | ||
private readonly RueConfigLogin? _config; | ||
private readonly LoginContext _context; | ||
|
||
public UserOnPacketCreateSecurityHandleAutoRegisterPlug(ILogger? logger, RueConfigLogin? config, LoginContext context) | ||
{ | ||
_logger = logger; | ||
_config = config; | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(IPipelineContext ctx, UserOnPacketCreateSecurityHandle message) | ||
{ | ||
if ((_config?.IsAutoLogin ?? false) && _config.LoginCredentials is { Username: not null, Password: not null }) | ||
{ | ||
_logger?.LogInformation( | ||
"Logging in user {Username} with password {Password}", | ||
_config.LoginCredentials.Username, _config.LoginCredentials.Password | ||
); | ||
|
||
await _context.Pipelines.UserOnPacketCheckPassword.Process(new UserOnPacketCheckPasswordFlipped( | ||
message.User, | ||
_config.LoginCredentials.Username, | ||
_config.LoginCredentials.Password | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Edelstein.Plugin.Rue.Configs; | ||
using Edelstein.Plugin.Rue.Plugs; | ||
using Edelstein.Protocol.Gameplay.Login.Contexts; | ||
using Edelstein.Protocol.Plugin; | ||
using Edelstein.Protocol.Plugin.Login; | ||
using Edelstein.Protocol.Utilities.Pipelines; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Edelstein.Plugin.Rue; | ||
|
||
public class RueLoginPlugin : ILoginPlugin | ||
{ | ||
public string ID => "RueLogin"; | ||
|
||
private ILogger? Logger { get; set; } | ||
private RueConfigLogin? Config { get; set; } | ||
|
||
public Task OnInit(IPluginHost<LoginContext> host, LoginContext ctx) | ||
{ | ||
Logger = host.Logger; | ||
Config = new RueConfigLogin(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task OnStart(IPluginHost<LoginContext> host, LoginContext ctx) | ||
{ | ||
host.Config.Bind(Config); | ||
|
||
ctx.Pipelines.UserOnPacketCreateSecurityHandle.Add(PipelinePriority.High, new UserOnPacketCreateSecurityHandleAutoRegisterPlug( | ||
Logger, | ||
Config, | ||
ctx | ||
)); | ||
ctx.Pipelines.UserOnPacketCheckPassword.Add(PipelinePriority.High, new UserOnPacketCheckPasswordAutoLoginPlug( | ||
Logger, | ||
Config, | ||
ctx | ||
)); | ||
ctx.Pipelines.UserOnPacketCheckPassword.Add(PipelinePriority.Highest, new UserOnPacketCheckPasswordFlippedPlug( | ||
Logger, | ||
Config, | ||
ctx | ||
)); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task OnStop() | ||
=> Task.CompletedTask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
{ | ||
"IsAutoRegister": true, | ||
"IsAutoLogin": false, | ||
"IsFlippedUsername": false, | ||
|
||
"LoginCredentials": { | ||
"Username": "username", | ||
"Password": "password" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/protocol/Edelstein.Protocol.Gameplay.Game/Contexts/GameContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/protocol/Edelstein.Protocol.Gameplay.Login/Contexts/LoginContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/protocol/Edelstein.Protocol.Gameplay.Login/Contracts/UserOnPacketCreateSecurityHandle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Edelstein.Protocol.Gameplay.Login.Contracts; | ||
|
||
public record UserOnPacketCreateSecurityHandle( | ||
ILoginStageUser User | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters