Some fixes and security updates
This commit is contained in:
parent
43acf366af
commit
68e6fa2353
22 changed files with 1114 additions and 58 deletions
|
|
@ -46,7 +46,13 @@ public class ChatClientConnection : IDisposable
|
|||
{
|
||||
try
|
||||
{
|
||||
var res = await _httpClient.GetAsync("/api/info");
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "/api/info");
|
||||
if (!string.IsNullOrWhiteSpace(_state.Token))
|
||||
{
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _state.Token);
|
||||
}
|
||||
|
||||
var res = await _httpClient.SendAsync(request);
|
||||
if (res.IsSuccessStatusCode)
|
||||
{
|
||||
// Auto-upgrade ServerUrl if redirected to HTTPS
|
||||
|
|
@ -67,6 +73,11 @@ public class ChatClientConnection : IDisposable
|
|||
}
|
||||
return info;
|
||||
}
|
||||
else if (res.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
// Server requires auth for info endpoint, return null and continue
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.SetStatus($"Server returned HTTP {(int)res.StatusCode} ({res.ReasonPhrase}) on /api/info");
|
||||
|
|
@ -79,20 +90,30 @@ public class ChatClientConnection : IDisposable
|
|||
return null;
|
||||
}
|
||||
|
||||
public async Task<AuthResponse> RegisterAsync(string username, string password, string? serverPassword = null)
|
||||
public async Task<AuthResponse> RegisterAsync(string username, string password, string? serverPassword = null, string? inviteToken = null)
|
||||
{
|
||||
var req = new RegisterRequest
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
ServerPassword = serverPassword
|
||||
ServerPassword = serverPassword,
|
||||
InviteToken = inviteToken
|
||||
};
|
||||
var json = JsonSerializer.Serialize(req);
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
try
|
||||
{
|
||||
var res = await _httpClient.PostAsync("/api/auth/register", content);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "/api/auth/register")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
// Add CSRF token
|
||||
var csrfToken = Guid.NewGuid().ToString("N");
|
||||
request.Headers.Add("X-CSRF-Token", csrfToken);
|
||||
|
||||
var res = await _httpClient.SendAsync(request);
|
||||
var resJson = await res.Content.ReadAsStringAsync();
|
||||
if (string.IsNullOrWhiteSpace(resJson))
|
||||
{
|
||||
|
|
@ -139,7 +160,16 @@ public class ChatClientConnection : IDisposable
|
|||
|
||||
try
|
||||
{
|
||||
var res = await _httpClient.PostAsync("/api/auth/login", content);
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "/api/auth/login")
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
// Add CSRF token
|
||||
var csrfToken = Guid.NewGuid().ToString("N");
|
||||
request.Headers.Add("X-CSRF-Token", csrfToken);
|
||||
|
||||
var res = await _httpClient.SendAsync(request);
|
||||
var resJson = await res.Content.ReadAsStringAsync();
|
||||
if (string.IsNullOrWhiteSpace(resJson))
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue