Fix storage of authentication token
This commit is contained in:
parent
a8190ad91b
commit
256b46e823
14 changed files with 34 additions and 55 deletions
|
|
@ -45,11 +45,7 @@ public class AuthenticationService(
|
|||
{
|
||||
using (var op = logger.BeginDebugOperation("Logging in using stored credentials"))
|
||||
{
|
||||
await Login(settingsProvider.CurrentValue.Authentication.ServerAddress, new SDK.Models.AuthToken
|
||||
{
|
||||
AccessToken = settingsProvider.CurrentValue.Authentication.AccessToken,
|
||||
RefreshToken = settingsProvider.CurrentValue.Authentication.RefreshToken,
|
||||
});
|
||||
await Login(settingsProvider.CurrentValue.Authentication.ServerAddress, settingsProvider.CurrentValue.Authentication.Token);
|
||||
|
||||
op.Complete();
|
||||
}
|
||||
|
|
@ -77,7 +73,7 @@ public class AuthenticationService(
|
|||
{
|
||||
await connectionClient.UpdateServerAddressAsync(serverAddress.ToString());
|
||||
|
||||
tokenProvider.SetToken(token.AccessToken);
|
||||
tokenProvider.SetToken(token);
|
||||
|
||||
if (await authenticationClient.ValidateTokenAsync())
|
||||
{
|
||||
|
|
@ -87,8 +83,6 @@ public class AuthenticationService(
|
|||
settingsProvider.Update(s =>
|
||||
{
|
||||
s.Authentication.ServerAddress = serverAddress;
|
||||
s.Authentication.AccessToken = token.AccessToken;
|
||||
s.Authentication.RefreshToken = token.RefreshToken;
|
||||
});
|
||||
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
|
|
@ -124,7 +118,6 @@ public class AuthenticationService(
|
|||
settingsProvider.Update(s =>
|
||||
{
|
||||
s.Authentication.ServerAddress = connectionClient.GetServerAddress();
|
||||
s.Authentication.AccessToken = tokenProvider.GetToken();
|
||||
});
|
||||
|
||||
await using var scope = scopeFactory.CreateAsyncScope();
|
||||
|
|
@ -191,14 +184,14 @@ public class AuthenticationService(
|
|||
{
|
||||
var token = tokenProvider.GetToken();
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
if (string.IsNullOrEmpty(token.AccessToken))
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
|
||||
return handler.ReadToken(token) as JwtSecurityToken;
|
||||
return handler.ReadToken(token.AccessToken) as JwtSecurityToken;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
@ -208,7 +201,7 @@ public class AuthenticationService(
|
|||
|
||||
public bool HasStoredCredentials()
|
||||
{
|
||||
if (string.IsNullOrEmpty(tokenProvider.GetToken()))
|
||||
if (string.IsNullOrEmpty(tokenProvider.GetToken().AccessToken))
|
||||
return false;
|
||||
|
||||
var decodedToken = DecodeToken();
|
||||
|
|
|
|||
|
|
@ -256,11 +256,10 @@ namespace LANCommander.Launcher.Services
|
|||
await client.Connection.UpdateServerAddressAsync(options.ServerAddress);
|
||||
|
||||
var token = await client.Authentication.AuthenticateAsync(options.Username, options.Password, client.Connection.GetServerAddress());
|
||||
|
||||
|
||||
client.Settings.Update(s =>
|
||||
{
|
||||
s.Authentication.AccessToken = token.AccessToken;
|
||||
s.Authentication.RefreshToken = token.RefreshToken;
|
||||
s.Authentication.Token = token;
|
||||
s.Authentication.ServerAddress = client.Connection.GetServerAddress();
|
||||
});
|
||||
|
||||
|
|
@ -278,8 +277,7 @@ namespace LANCommander.Launcher.Services
|
|||
|
||||
client.Settings.Update(s =>
|
||||
{
|
||||
s.Authentication.AccessToken = String.Empty;
|
||||
s.Authentication.RefreshToken = String.Empty;
|
||||
s.Authentication.Token = null;
|
||||
s.Authentication.OfflineModeEnabled = false;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,12 +64,8 @@ internal static class WindowService
|
|||
|
||||
if (appHook != null)
|
||||
appHook(app);
|
||||
|
||||
var settingsProvider = app.Services.GetService<SettingsProvider<Settings>>();
|
||||
var tokenProvider = app.Services.GetService<ITokenProvider>();
|
||||
var connectionClient = app.Services.GetService<IConnectionClient>();
|
||||
|
||||
tokenProvider?.SetToken(settingsProvider.CurrentValue.Authentication.AccessToken);
|
||||
var connectionClient = app.Services.GetService<IConnectionClient>();
|
||||
|
||||
connectionClient.ConnectAsync().Wait();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
{
|
||||
var currentUri = new Uri(NavigationManager.Uri);
|
||||
|
||||
if (String.IsNullOrEmpty(Client.Settings.CurrentValue.Authentication.AccessToken) && currentUri.LocalPath != "/Authenticate")
|
||||
if (String.IsNullOrEmpty(Client.Settings.CurrentValue.Authentication.Token?.AccessToken) && currentUri.LocalPath != "/Authenticate")
|
||||
NavigationManager.NavigateTo("/Authenticate", true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@
|
|||
ConnectionClient.OnOfflineModeEnabled += OnOfflineModeChanged;
|
||||
ConnectionClient.OnConnect += OnConnect;
|
||||
|
||||
if (!await Client.Authentication.ValidateTokenAsync())
|
||||
if (await Client.Authentication.ValidateTokenAsync())
|
||||
{
|
||||
await AuthenticationService.Login();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
using System.Threading.Tasks;
|
||||
using LANCommander.SDK.Models;
|
||||
|
||||
namespace LANCommander.SDK.Abstractions;
|
||||
|
||||
public interface ITokenProvider
|
||||
{
|
||||
void SetToken(string token);
|
||||
string GetToken();
|
||||
void SetToken(AuthToken token);
|
||||
AuthToken GetToken();
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ public class ApiRequestBuilder(
|
|||
ITokenProvider tokenProvider,
|
||||
ISettingsProvider settingsProvider)
|
||||
{
|
||||
private string _token { get; set; } = tokenProvider.GetToken();
|
||||
private AuthToken _token { get; set; } = tokenProvider.GetToken();
|
||||
private bool _ignoreVersion { get; set; }
|
||||
private object _body { get; set; }
|
||||
private string _route { get; set; }
|
||||
|
|
@ -49,7 +49,7 @@ public class ApiRequestBuilder(
|
|||
|
||||
public ApiRequestBuilder UseAuthenticationToken()
|
||||
{
|
||||
_request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
|
||||
_request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@ public class AuthenticationSettings
|
|||
{
|
||||
[YamlMember(typeof(string))]
|
||||
public Uri ServerAddress { get; set; }
|
||||
[YamlMember(ScalarStyle = ScalarStyle.Plain)]
|
||||
public string AccessToken { get; set; }
|
||||
[YamlMember(ScalarStyle = ScalarStyle.Plain)]
|
||||
public string RefreshToken { get; set; }
|
||||
public AuthToken Token { get; set; }
|
||||
public bool OfflineModeEnabled { get; set; }
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ public sealed class ServerConfigurationProvider : ConfigurationProvider
|
|||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, settings.Authentication.ServerAddress.Join("/api/Settings"));
|
||||
|
||||
request.Headers.Add("Authorization", $"Bearer {settings.Authentication.AccessToken}");
|
||||
request.Headers.Add("Authorization", $"Bearer {settings.Authentication.Token.AccessToken}");
|
||||
|
||||
var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
using LANCommander.SDK.Abstractions;
|
||||
using LANCommander.SDK.Models;
|
||||
|
||||
namespace LANCommander.SDK.Providers;
|
||||
|
||||
public class TokenProvider : ITokenProvider
|
||||
public class TokenProvider(ISettingsProvider settingsProvider) : ITokenProvider
|
||||
{
|
||||
private string _token { get; set; }
|
||||
|
||||
public void SetToken(string token)
|
||||
public void SetToken(AuthToken token)
|
||||
{
|
||||
_token = token;
|
||||
settingsProvider.Update(s =>
|
||||
{
|
||||
s.Authentication.Token = token;
|
||||
s.Authentication.OfflineModeEnabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
public string GetToken()
|
||||
{
|
||||
return _token;
|
||||
}
|
||||
AuthToken ITokenProvider.GetToken() => settingsProvider.CurrentValue.Authentication.Token;
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ internal partial class RpcSubscriber(ITokenProvider tokenProvider) : IRpcSubscri
|
|||
_connection = new HubConnectionBuilder()
|
||||
.WithUrl(serverAddress.Join("rpc"), options =>
|
||||
{
|
||||
options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken());
|
||||
options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken().AccessToken);
|
||||
})
|
||||
.Build();
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class AuthenticationClient(
|
|||
Expiration = result.Data.Expiration
|
||||
};
|
||||
|
||||
tokenProvider.SetToken(token.AccessToken);
|
||||
tokenProvider.SetToken(token);
|
||||
|
||||
await configRefresher.RefreshAsync();
|
||||
|
||||
|
|
@ -103,12 +103,6 @@ public class AuthenticationClient(
|
|||
}
|
||||
|
||||
tokenProvider.SetToken(null);
|
||||
settingsProvider.Update(s =>
|
||||
{
|
||||
s.Authentication.AccessToken = null;
|
||||
s.Authentication.RefreshToken = null;
|
||||
s.Authentication.OfflineModeEnabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task RegisterAsync(string username, string password, string passwordConfirmation)
|
||||
|
|
@ -138,7 +132,7 @@ public class AuthenticationClient(
|
|||
switch (result.Response.StatusCode)
|
||||
{
|
||||
case HttpStatusCode.OK:
|
||||
tokenProvider.SetToken(result.Data.AccessToken);
|
||||
tokenProvider.SetToken(result.Data);
|
||||
return;
|
||||
|
||||
case HttpStatusCode.BadRequest:
|
||||
|
|
@ -163,7 +157,7 @@ public class AuthenticationClient(
|
|||
{
|
||||
logger?.LogTrace("Validating token");
|
||||
|
||||
if (String.IsNullOrWhiteSpace(tokenProvider.GetToken()))
|
||||
if (String.IsNullOrWhiteSpace(tokenProvider.GetToken()?.AccessToken))
|
||||
{
|
||||
logger?.LogError("Token is empty");
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public class ConnectionClient(
|
|||
|
||||
public bool IsConfigured()
|
||||
{
|
||||
return HasServerAddress() && !String.IsNullOrEmpty(tokenProvider.GetToken());
|
||||
return HasServerAddress() && !String.IsNullOrEmpty(tokenProvider.GetToken()?.AccessToken);
|
||||
}
|
||||
|
||||
public bool IsOfflineMode()
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ namespace LANCommander.Server.Services
|
|||
|
||||
if (scripts.Any())
|
||||
{
|
||||
tokenProvider.SetToken(token.AccessToken);
|
||||
tokenProvider.SetToken(token);
|
||||
|
||||
foreach (var script in scripts)
|
||||
{
|
||||
|
|
@ -190,7 +190,7 @@ namespace LANCommander.Server.Services
|
|||
|
||||
if (scripts.Any())
|
||||
{
|
||||
tokenProvider.SetToken(token.AccessToken);
|
||||
tokenProvider.SetToken(token);
|
||||
|
||||
foreach (var script in scripts)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue