LoginPage.razor (3163B)
1 @page "/Login" 2 @using System.Text.Json 3 @using MatrixRoomUtils.Core.Authentication 4 @using MatrixRoomUtils.Web.Shared.SimpleComponents 5 @inject ILocalStorageService LocalStorage 6 @inject IJSRuntime JsRuntime 7 <h3>Login</h3> 8 <hr/> 9 10 <span> 11 <span>@@</span><!-- 12 --><FancyTextBox @bind-Value="@newRecordInput.username"></FancyTextBox><!-- 13 --><span>:</span><!-- 14 --><FancyTextBox @bind-Value="@newRecordInput.homeserver"></FancyTextBox> 15 </span> 16 <span style="display: block;"> 17 <label>Password:</label> 18 <FancyTextBox @bind-Value="@newRecordInput.password" IsPassword="true"></FancyTextBox> 19 </span> 20 <button @onclick="AddRecord">Add account to queue</button> 21 <br/> 22 23 <InputFile OnChange="@FileChanged" accept=".tsv"></InputFile> 24 <br/> 25 <button @onclick="Login">Login</button> 26 <br/><br/> 27 <h4>Parsed records</h4> 28 <hr/> 29 <table border="1"> 30 @foreach (var (homeserver, username, password) in records) 31 { 32 <tr style="background-color: @(RuntimeCache.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}") ? "green" : "unset")"> 33 <td style="border-width: 1px;">@username</td> 34 <td style="border-width: 1px;">@homeserver</td> 35 <td style="border-width: 1px;">@password.Length chars</td> 36 </tr> 37 } 38 </table> 39 <br/> 40 <br/> 41 <LogView></LogView> 42 43 @code { 44 List<(string homeserver, string username, string password)> records = new(); 45 (string homeserver, string username, string password) newRecordInput = ("", "", ""); 46 47 async Task Login() 48 { 49 foreach (var (homeserver, username, password) in records) 50 { 51 if (RuntimeCache.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}")) continue; 52 var result = await MatrixAuth.Login(homeserver, username, password); 53 Console.WriteLine($"Obtained access token for {result.UserId}!"); 54 55 var userinfo = new UserInfo() 56 { 57 LoginResponse = result 58 }; 59 userinfo.Profile = await (await new AuthenticatedHomeServer(result.UserId, result.AccessToken, result.HomeServer).Configure()).GetProfile(result.UserId); 60 RuntimeCache.LastUsedToken = result.AccessToken; 61 62 RuntimeCache.LoginSessions.Add(result.AccessToken, userinfo); 63 StateHasChanged(); 64 } 65 66 await LocalStorageWrapper.SaveToLocalStorage(LocalStorage); 67 } 68 69 private async Task FileChanged(InputFileChangeEventArgs obj) 70 { 71 Console.WriteLine(JsonSerializer.Serialize(obj, new JsonSerializerOptions() 72 { 73 WriteIndented = true 74 })); 75 await using var rs = obj.File.OpenReadStream(); 76 using var sr = new StreamReader(rs); 77 string TsvData = await sr.ReadToEndAsync(); 78 records.Clear(); 79 foreach (var line in TsvData.Split('\n')) 80 { 81 var parts = line.Split('\t'); 82 if (parts.Length != 3) 83 continue; 84 records.Add((parts[0], parts[1], parts[2])); 85 } 86 } 87 88 private void AddRecord() 89 { 90 records.Add(newRecordInput); 91 newRecordInput = ("", "", ""); 92 } 93 94 }