1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
using LibMatrix;
using LibMatrix.Homeservers;
using LibMatrix.Services;
using Microsoft.AspNetCore.Components;
namespace MatrixUtils.Web.Classes;
public class RmuSessionStore(
ILogger<RmuSessionStore> logger,
TieredStorageService storageService,
HomeserverProviderService homeserverProviderService,
NavigationManager navigationManager) {
private SessionInfo? CurrentSession { get; set; }
private Dictionary<string, SessionInfo> SessionCache { get; set; } = [];
private bool _isInitialized;
private static readonly SemaphoreSlim InitSemaphore = new(1, 1);
#region Sessions
public async Task<Dictionary<string, SessionInfo>> GetAllSessions() {
await LoadStorage();
logger.LogTrace("Getting all tokens.");
return SessionCache;
}
public async Task<SessionInfo?> GetSession(string sessionId) {
await LoadStorage();
if (string.IsNullOrEmpty(sessionId)) {
logger.LogWarning("No session ID provided.");
return null;
}
if (SessionCache.TryGetValue(sessionId, out var cachedSession))
return cachedSession;
logger.LogWarning("Session {sessionId} not found in all tokens.", sessionId);
return null;
}
public async Task<SessionInfo?> GetCurrentSession(bool log = true) {
await LoadStorage();
if (log) logger.LogTrace("Getting current token.");
if (CurrentSession is not null) return CurrentSession;
var currentSessionId = await storageService.DataStorageProvider!.LoadObjectAsync<string>("rmu.session");
if (currentSessionId == null) {
if (log) logger.LogWarning("No current session ID found in storage.");
return null;
}
return await GetSession(currentSessionId);
}
public async Task<string> AddSession(UserAuth auth) {
await LoadStorage();
logger.LogTrace("Adding token.");
var sessionId = auth.GetHashCode().ToString();
SessionCache[sessionId] = new() {
Auth = auth,
SessionId = sessionId
};
await SaveStorage();
if (CurrentSession == null) await SetCurrentSession(sessionId);
return sessionId;
}
public async Task RemoveSession(string sessionId) {
await LoadStorage();
if (SessionCache.Count == 0) {
logger.LogWarning("No sessions found.");
return;
}
logger.LogTrace("Removing session {sessionId}.", sessionId);
if ((await GetCurrentSession())?.SessionId == sessionId)
await SetCurrentSession(SessionCache.FirstOrDefault(x => x.Key != sessionId).Key);
if (SessionCache.Remove(sessionId)) {
logger.LogInformation("RemoveSession: Removed session {sessionId}.", sessionId);
logger.LogInformation("RemoveSession: Remaining sessions: {sessionIds}.", string.Join(", ", SessionCache.Keys));
await SaveStorage(log: true);
}
else
logger.LogWarning("RemoveSession: Session {sessionId} not found.", sessionId);
}
public async Task SetCurrentSession(string? sessionId) {
await LoadStorage();
logger.LogTrace("Setting current session to {sessionId}.", sessionId);
CurrentSession = await GetSession(sessionId);
await SaveStorage();
}
#endregion
#region Homeservers
public async Task<AuthenticatedHomeserverGeneric?> GetHomeserver(string session, bool log = true) {
await LoadStorage();
if (log) logger.LogTrace("Getting session.");
if (!SessionCache.TryGetValue(session, out var cachedSession)) return null;
if (cachedSession.Homeserver is not null) return cachedSession.Homeserver;
try {
cachedSession.Homeserver =
await homeserverProviderService.GetAuthenticatedWithToken(cachedSession.Auth.Homeserver, cachedSession.Auth.AccessToken, cachedSession.Auth.Proxy);
}
catch (Exception e) {
logger.LogError("Failed to get info for {0} via {1}: {2}", cachedSession.Auth.UserId, cachedSession.Auth.Homeserver, e);
logger.LogError("Continuing with server-less session");
cachedSession.Homeserver = await homeserverProviderService.GetAuthenticatedWithToken(cachedSession.Auth.Homeserver, cachedSession.Auth.AccessToken,
cachedSession.Auth.Proxy, useGeneric: true, enableServer: false);
}
return cachedSession.Homeserver;
}
public async Task<AuthenticatedHomeserverGeneric?> GetCurrentHomeserver(bool log = true, bool navigateOnFailure = false) {
await LoadStorage();
if (log) logger.LogTrace("Getting current session.");
if (CurrentSession?.Homeserver is not null) return CurrentSession.Homeserver;
var currentSession = CurrentSession ??= await GetCurrentSession(log: false);
if (currentSession == null) {
if (navigateOnFailure) {
logger.LogInformation("No session found. Navigating to login.");
navigationManager.NavigateTo("/Login");
}
return null;
}
try {
return currentSession.Homeserver ??= await GetHomeserver(currentSession.SessionId);
}
catch (MatrixException e) {
if (e.ErrorCode == "M_UNKNOWN_TOKEN" && navigateOnFailure) {
logger.LogWarning("Encountered invalid token for {user} on {homeserver}", currentSession.Auth.UserId, currentSession.Auth.Homeserver);
if (navigateOnFailure) {
navigationManager.NavigateTo("/InvalidSession?ctx=" + currentSession.SessionId);
}
}
throw;
}
}
public async IAsyncEnumerable<AuthenticatedHomeserverGeneric> TryGetAllHomeservers(bool log = true, bool ignoreFailures = true) {
await LoadStorage();
if (log) logger.LogTrace("Getting all homeservers.");
var tasks = SessionCache.Values.Select(async session => {
if (ignoreFailures && session.Auth.LastFailureReason != null && session.Auth.LastFailureReason != UserAuth.FailureReason.None) {
if (log) logger.LogTrace("Skipping session {sessionId} due to previous failure: {reason}", session.SessionId, session.Auth.LastFailureReason);
return null;
}
try {
var hs = await GetHomeserver(session.SessionId, log: false);
if (session.Auth.LastFailureReason != null) {
SessionCache[session.SessionId].Auth.LastFailureReason = null;
await SaveStorage();
}
return hs;
}
catch (Exception e) {
logger.LogError("TryGetAllHomeservers: Failed to get homeserver for {userId} via {homeserver}: {ex}", session.Auth.UserId, session.Auth.Homeserver, e);
var reason = SessionCache[session.SessionId].Auth.LastFailureReason = e switch {
MatrixException { ErrorCode: MatrixException.ErrorCodes.M_UNKNOWN_TOKEN } => UserAuth.FailureReason.InvalidToken,
HttpRequestException => UserAuth.FailureReason.NetworkError,
_ => UserAuth.FailureReason.UnknownError
};
await SaveStorage(log: true);
// await LoadStorage(true);
if (SessionCache[session.SessionId].Auth.LastFailureReason != reason) {
await Console.Error.WriteLineAsync(
$"Warning: Session {session.SessionId} failure reason changed during reload from {reason} to {SessionCache[session.SessionId].Auth.LastFailureReason}");
}
throw;
}
}).ToList();
while (tasks.Count != 0) {
var finished = await Task.WhenAny(tasks);
tasks.Remove(finished);
if (finished.IsFaulted) continue;
var result = await finished;
if (result != null) yield return result;
}
}
#endregion
#region Storage
private async Task LoadStorage(bool hasMigrated = false) {
if (!await storageService.DataStorageProvider!.ObjectExistsAsync("rmu.sessions") || !await storageService.DataStorageProvider.ObjectExistsAsync("rmu.session")) {
if (!hasMigrated) {
await RunMigrations();
await LoadStorage(true);
}
else
logger.LogWarning("No sessions found in storage.");
return;
}
SessionCache = (await storageService.DataStorageProvider.LoadObjectAsync<Dictionary<string, UserAuth>>("rmu.sessions") ?? throw new Exception("Failed to load sessions"))
.ToDictionary(x => x.Key, x => new SessionInfo {
SessionId = x.Key,
Auth = x.Value
});
var currentSessionId = await storageService.DataStorageProvider.LoadObjectAsync<string>("rmu.session");
if (currentSessionId == null) {
logger.LogWarning("No current session found in storage.");
return;
}
if (!SessionCache.TryGetValue(currentSessionId, out var currentSession)) {
logger.LogWarning("Current session {currentSessionId} not found in storage.", currentSessionId);
return;
}
CurrentSession = currentSession;
}
private async Task SaveStorage(bool log = false) {
if (log) logger.LogWarning("Saving {count} sessions to storage.", SessionCache.Count);
await storageService.DataStorageProvider!.SaveObjectAsync("rmu.sessions",
SessionCache.ToDictionary(
x => x.Key,
x => x.Value.Auth
)
);
await storageService.DataStorageProvider.SaveObjectAsync("rmu.session", CurrentSession?.SessionId);
if (log) logger.LogWarning("{count} sessions saved to storage.", SessionCache.Count);
}
#endregion
#region Migrations
public async Task RunMigrations() {
await MigrateFromMru();
await MigrateAccountsToKeyedStorage();
}
private async Task MigrateFromMru() {
var dsp = storageService.DataStorageProvider!;
if (await dsp.ObjectExistsAsync("token") || await dsp.ObjectExistsAsync("tokens")) {
logger.LogInformation("Migrating from unnamespaced localstorage!");
if (await dsp.ObjectExistsAsync("token")) {
var oldToken = await dsp.LoadObjectAsync<UserAuth>("token");
if (oldToken != null) {
await dsp.SaveObjectAsync("mru.token", oldToken);
await dsp.DeleteObjectAsync("token");
}
}
if (await dsp.ObjectExistsAsync("tokens")) {
var oldTokens = await dsp.LoadObjectAsync<List<UserAuth>>("tokens");
if (oldTokens != null) {
await dsp.SaveObjectAsync("mru.tokens", oldTokens);
await dsp.DeleteObjectAsync("tokens");
}
}
}
if (await dsp.ObjectExistsAsync("mru.token") || await dsp.ObjectExistsAsync("mru.tokens")) {
logger.LogInformation("Migrating from MRU token namespace!");
if (await dsp.ObjectExistsAsync("mru.token")) {
var oldToken = await dsp.LoadObjectAsync<UserAuth>("mru.token");
if (oldToken != null) {
await dsp.SaveObjectAsync("rmu.token", oldToken);
await dsp.DeleteObjectAsync("mru.token");
}
}
if (await dsp.ObjectExistsAsync("mru.tokens")) {
var oldTokens = await dsp.LoadObjectAsync<List<UserAuth>>("mru.tokens");
if (oldTokens != null) {
await dsp.SaveObjectAsync("rmu.tokens", oldTokens);
await dsp.DeleteObjectAsync("mru.tokens");
}
}
}
}
private async Task MigrateAccountsToKeyedStorage() {
var dsp = storageService.DataStorageProvider!;
if (!await dsp.ObjectExistsAsync("rmu.tokens")) return;
logger.LogInformation("Migrating accounts to keyed storage!");
var tokens = await dsp.LoadObjectAsync<UserAuth[]>("rmu.tokens") ?? throw new Exception("Failed to load tokens");
Dictionary<string, UserAuth> keyedTokens = tokens.ToDictionary(x => x.GetHashCode().ToString(), x => x);
if (await dsp.ObjectExistsAsync("rmu.token")) {
var token = await dsp.LoadObjectAsync<UserAuth>("rmu.token") ?? throw new Exception("Failed to load token");
var sessionId = keyedTokens.FirstOrDefault(x => x.Value.Equals(token)).Key;
if (sessionId is null) keyedTokens.Add(sessionId = token.GetHashCode().ToString(), token);
await dsp.SaveObjectAsync("rmu.session", sessionId);
await dsp.DeleteObjectAsync("rmu.token");
}
await dsp.SaveObjectAsync("rmu.sessions", keyedTokens);
await dsp.DeleteObjectAsync("rmu.tokens");
}
#endregion
public class Settings {
public DeveloperSettings DeveloperSettings { get; set; } = new();
}
public class DeveloperSettings {
public bool EnableLogViewers { get; set; }
public bool EnableConsoleLogging { get; set; } = true;
public bool EnablePortableDevtools { get; set; }
}
public class SessionInfo {
public required string SessionId { get; set; }
public required UserAuth Auth { get; set; }
public AuthenticatedHomeserverGeneric? Homeserver { get; set; }
}
}
|