From 3dfb7b81b0fe19d37a7bf1183e248ca10c56277c Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 23 Feb 2024 11:23:27 +0000 Subject: HS emulator --- .../Controllers/AuthController.cs | 23 ++++++ .../Controllers/HEDebug/HEDebugController.cs | 18 +++++ .../Controllers/UserController.cs | 81 ++++++++++++++++++++++ .../Controllers/VersionsController.cs | 48 +++++++++++++ .../Controllers/WellKnownController.cs | 32 +++++++++ 5 files changed, 202 insertions(+) create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs (limited to 'Tests/LibMatrix.HomeserverEmulator/Controllers') diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs new file mode 100644 index 0000000..d0496bf --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs @@ -0,0 +1,23 @@ +using System.Text.Json.Nodes; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class AuthController(ILogger logger, UserStore userStore) : ControllerBase { + [HttpPost("login")] + public async Task Login(LoginRequest request) { + var user = await userStore.CreateUser($"@{Guid.NewGuid().ToString()}:{Request.Host}", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), new Dictionary()); + var loginResponse = new LoginResponse { + AccessToken = user.AccessToken, + DeviceId = user.DeviceId, + UserId = user.UserId + }; + + return loginResponse; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs new file mode 100644 index 0000000..0c4d8bd --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/HEDebug/HEDebugController.cs @@ -0,0 +1,18 @@ +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_hsEmulator")] +public class HEDebugController(ILogger logger, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("users")] + public async Task> GetUsers() { + return userStore._users; + } + + [HttpGet("rooms")] + public async Task> GetRooms() { + return roomStore._rooms.ToList(); + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs new file mode 100644 index 0000000..d763b26 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/UserController.cs @@ -0,0 +1,81 @@ +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class UserController(ILogger logger, TokenService tokenService, UserStore userStore) : ControllerBase { + [HttpGet("account/whoami")] + public async Task Login() { + var token = tokenService.GetAccessToken(); + if (token is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "No token passed." + }; + + var user = await userStore.GetUserByToken(token, Random.Shared.Next(101) <= 10, tokenService.GenerateServerName()); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "Invalid token." + }; + var whoAmIResponse = new WhoAmIResponse { + UserId = user.UserId + }; + return whoAmIResponse; + } + + [HttpGet("profile/{userId}")] + public async Task> GetProfile(string userId) { + var user = await userStore.GetUserById(userId, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "User not found." + }; + return user.Profile; + } + + [HttpGet("profile/{userId}/{key}")] + public async Task GetProfile(string userId, string key) { + var user = await userStore.GetUserById(userId, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "User not found." + }; + if (!user.Profile.TryGetValue(key, out var value)) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Key not found." + }; + return value; + } + + [HttpGet("joined_rooms")] + public async Task GetJoinedRooms() { + var token = tokenService.GetAccessToken(); + if (token is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "No token passed." + }; + + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + // return user.JoinedRooms; + + return new { + joined_rooms = user.JoinedRooms + }; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs new file mode 100644 index 0000000..1349fac --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs @@ -0,0 +1,48 @@ +using System.Text.Json.Nodes; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/")] +public class VersionsController(ILogger logger) : ControllerBase { + [HttpGet("client/versions")] + public async Task GetClientVersions() { + var clientVersions = new ClientVersionsResponse { + Versions = new() { + "r0.0.1", + "r0.1.0", + "r0.2.0", + "r0.3.0", + "r0.4.0", + "r0.5.0", + "r0.6.0", + "r0.6.1", + "v1.1", + "v1.2", + "v1.3", + "v1.4", + "v1.5", + "v1.6", + "v1.7", + "v1.8", + }, + UnstableFeatures = new() + }; + return clientVersions; + } + + [HttpGet("federation/v1/version")] + public async Task GetServerVersions() { + var clientVersions = new ServerVersionResponse() { + Server = new() { + Name = "LibMatrix.HomeserverEmulator", + Version = "0.0.0" + } + }; + return clientVersions; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs new file mode 100644 index 0000000..97e460d --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/WellKnownController.cs @@ -0,0 +1,32 @@ +using System.Text.Json.Nodes; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/.well-known/matrix/")] +public class WellKnownController(ILogger logger) : ControllerBase { + [HttpGet("client")] + public JsonObject GetClientWellKnown() { + var obj = new JsonObject() { + ["m.homeserver"] = new JsonObject() { + ["base_url"] = $"{Request.Scheme}://{Request.Host}" + } + }; + + logger.LogInformation("Serving client well-known: {}", obj); + + return obj; + } + [HttpGet("server")] + public JsonObject GetServerWellKnown() { + var obj = new JsonObject() { + ["m.server"] = $"{Request.Scheme}://{Request.Host}" + }; + + logger.LogInformation("Serving server well-known: {}", obj); + + return obj; + } +} \ No newline at end of file -- cgit 1.5.1