about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-17 00:21:24 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-17 00:21:24 +0200
commit1beca653b772cf10586c417b2c25df03a67df8a2 (patch)
treed539b5f329cb62f253e1bc8142c3a313719657b0 /MatrixRoomUtils.Web/Pages
parentChanges (diff)
downloadMatrixUtils-1beca653b772cf10586c417b2c25df03a67df8a2.tar.xz
Handle external logouts
Diffstat (limited to 'MatrixRoomUtils.Web/Pages')
-rw-r--r--MatrixRoomUtils.Web/Pages/About.razor4
-rw-r--r--MatrixRoomUtils.Web/Pages/Index.razor18
-rw-r--r--MatrixRoomUtils.Web/Pages/InvalidSession.razor97
-rw-r--r--MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor10
4 files changed, 119 insertions, 10 deletions
diff --git a/MatrixRoomUtils.Web/Pages/About.razor b/MatrixRoomUtils.Web/Pages/About.razor
index b8d9c4a..971bd9b 100644
--- a/MatrixRoomUtils.Web/Pages/About.razor
+++ b/MatrixRoomUtils.Web/Pages/About.razor
@@ -1,4 +1,4 @@
-@page "/About"
+@page "/About"
 @using System.Net
 @using System.Net.Sockets
 @inject NavigationManager NavigationManager
@@ -56,7 +56,7 @@
         var message = "Hello, World!\nThis is a terminal emulator!\n\nYou can type stuff here, and it will be sent to the server!\n\nThis is a test of the emergency broadcast system.\n\nThis is only a t";
         _terminal.Options.RendererType = RendererType.Dom;
         _terminal.Options.ScreenReaderMode = true;
-        TcpClient.
+//        TcpClient.
         for (var i = 0; i < message.Length; i++) {
             await _terminal.Write(message[i].ToString());
 
diff --git a/MatrixRoomUtils.Web/Pages/Index.razor b/MatrixRoomUtils.Web/Pages/Index.razor
index 16a6cee..01e2be4 100644
--- a/MatrixRoomUtils.Web/Pages/Index.razor
+++ b/MatrixRoomUtils.Web/Pages/Index.razor
@@ -21,10 +21,10 @@ Small collection of tools to do not-so-everyday things.
                 <input type="radio" name="csa" checked="@(_currentSession.AccessToken == _auth.AccessToken)" @onclick="@(()=>SwitchSession(_auth))" style="text-decoration-line: unset;"/>
                 <b>@_user.DisplayName</b> on <b>@_auth.Homeserver</b>
                 <a role="button" @onclick="@(() => RemoveUser(_auth))">Remove</a>
-                
+
             </p>
             <p style="margin-top: -1.5em; margin-left: 4em;">Member of @_user.RoomCount rooms</p>
-        
+
         </div>
     }
 </form>
@@ -39,7 +39,17 @@ Small collection of tools to do not-so-everyday things.
         var tokens = await MRUStorage.GetAllTokens();
         var profileTasks = tokens.Select(async token => {
             UserInfo userInfo = new();
-            var hs = await HomeserverProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken);
+            AuthenticatedHomeServer hs;
+            try {
+                hs = await HomeserverProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken);
+            }
+            catch (MatrixException e) {
+                if (e.ErrorCode == "M_UNKNOWN_TOKEN") {
+                    NavigationManager.NavigateTo("/InvalidSession?ctx="+token.AccessToken);
+                    return;
+                }
+                throw;
+            }
             var roomCountTask = hs.GetJoinedRooms();
             var profile = await hs.GetProfile(hs.WhoAmI.UserId);
             userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId;
@@ -75,4 +85,4 @@ Small collection of tools to do not-so-everyday things.
         await MRUStorage.SetCurrentToken(auth);
         await OnInitializedAsync();
     }
-}
\ No newline at end of file
+}
diff --git a/MatrixRoomUtils.Web/Pages/InvalidSession.razor b/MatrixRoomUtils.Web/Pages/InvalidSession.razor
new file mode 100644
index 0000000..3bcd797
--- /dev/null
+++ b/MatrixRoomUtils.Web/Pages/InvalidSession.razor
@@ -0,0 +1,97 @@
+@page "/InvalidSession"
+@using MatrixRoomUtils.Core.Helpers
+@using MatrixRoomUtils.Core.Responses
+@using MatrixRoomUtils.Web.Shared.SimpleComponents
+
+<PageTitle>Invalid session</PageTitle>
+
+<h3>Rory&::MatrixUtils - Invalid session encountered</h3>
+<p>A session was encountered that is no longer valid. This can happen if you have logged out of the account on another device, or if the access token has expired.</p>
+
+@if (_login is not null) {
+    <p>It appears that the affected user is @_login.UserId (@_login.DeviceId) on @_login.Homeserver!</p>
+    <LinkButton OnClick="@(OpenRefreshDialog)">Refresh token</LinkButton>
+    <LinkButton OnClick="@(RemoveUser)">Remove</LinkButton>
+
+    @if (_showRefreshDialog) {
+        <ModalWindow MinWidth="300" X="275" Y="300" Title="@($"Password for {_login.UserId}")">
+            <FancyTextBox IsPassword="true" @bind-Value="@_password"></FancyTextBox><br/>
+            <LinkButton OnClick="TryLogin">Log in</LinkButton>
+            @if (_loginException is not null) {
+                <pre style="color: red;">@_loginException.RawContent</pre>
+            }
+        </ModalWindow>
+    }
+}
+
+@code
+{
+    [Parameter]
+    [SupplyParameterFromQuery(Name = "ctx")]
+    public string Context { get; set; }
+
+    private LoginResponse _login { get; set; }
+
+    private bool _showRefreshDialog { get; set; } = false;
+
+    private string _password { get; set; } = "";
+
+    private MatrixException _loginException { get; set; }
+
+    protected override async Task OnInitializedAsync() {
+        var tokens = await MRUStorage.GetAllTokens();
+        if (tokens is null || tokens.Count == 0) {
+            NavigationManager.NavigateTo("/Login");
+            return;
+        }
+
+        _login = tokens.FirstOrDefault(x => x.AccessToken == Context);
+
+        if (_login is null) {
+            Console.WriteLine($"Could not find {_login} in stored tokens!");
+        }
+
+        await base.OnInitializedAsync();
+    }
+
+    private async Task RemoveUser() {
+        await MRUStorage.RemoveToken(_login);
+        if ((await MRUStorage.GetCurrentToken()).AccessToken == _login.AccessToken)
+            MRUStorage.SetCurrentToken((await MRUStorage.GetAllTokens()).FirstOrDefault());
+        await OnInitializedAsync();
+    }
+
+    private async Task OpenRefreshDialog() {
+        _showRefreshDialog = true;
+        StateHasChanged();
+    }
+
+    private async Task SwitchSession(LoginResponse auth) {
+        Console.WriteLine($"Switching to {auth.Homeserver} {auth.AccessToken} {auth.UserId}");
+        await MRUStorage.SetCurrentToken(auth);
+        await OnInitializedAsync();
+    }
+
+    private async Task TryLogin() {
+        try {
+            var result = await HomeserverProvider.Login(_login.Homeserver, _login.UserId, _password);
+            if (result is null) {
+                Console.WriteLine($"Failed to login to {_login.Homeserver} as {_login.UserId}!");
+                return;
+            }
+            Console.WriteLine($"Obtained access token for {result.UserId}!");
+
+            await RemoveUser();
+            await MRUStorage.AddToken(result);
+            if (result.UserId == (await MRUStorage.GetCurrentToken())?.UserId)
+                await MRUStorage.SetCurrentToken(result);
+            NavigationManager.NavigateTo("/");
+        }
+        catch (MatrixException e) {
+            Console.WriteLine($"Failed to login to {_login.Homeserver} as {_login.UserId}!");
+            Console.WriteLine(e);
+            _loginException = e;
+            StateHasChanged();
+        }
+    }
+}
diff --git a/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor b/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor
index 4cb16b8..cd4788b 100644
--- a/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor
+++ b/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor
@@ -207,7 +207,9 @@ else {
     }
 
     private async Task LoadStatesAsync() {
-        var hs = await MRUStorage.GetCurrentSession();
+        var hs = await MRUStorage.GetCurrentSessionOrNavigate();
+        if (hs is null) return;
+
         var room = await hs.GetRoom(RoomId);
 
         var states = room.GetFullStateAsync();
@@ -215,8 +217,8 @@ else {
             if (!state.Type.StartsWith("m.policy.rule")) continue;
             PolicyEvents.Add(state);
         }
-        
-        
+
+
         // var stateEventsQuery = await room.GetStateAsync("");
         // var stateEvents = stateEventsQuery.Value.Deserialize<List<StateEventResponse>>();
         // PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule"))
@@ -247,4 +249,4 @@ else {
         StateHasChanged();
     }
 
-}
\ No newline at end of file
+}