|
|
|
@ -1,21 +1,38 @@ |
|
|
|
using System; |
|
|
|
using System.IO; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Text; |
|
|
|
using System.Text.Json; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using IdentityModel; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Volo.Abp.Cli.Commands; |
|
|
|
using Volo.Abp.Cli.Http; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
using Volo.Abp.IdentityModel; |
|
|
|
using Volo.Abp.IO; |
|
|
|
using Volo.Abp.Threading; |
|
|
|
|
|
|
|
namespace Volo.Abp.Cli.Auth |
|
|
|
{ |
|
|
|
public class AuthService : ITransientDependency |
|
|
|
{ |
|
|
|
protected IIdentityModelAuthenticationService AuthenticationService { get; } |
|
|
|
protected ILogger<NewCommand> Logger { get; } |
|
|
|
protected CliHttpClientFactory CliHttpClientFactory { get; } |
|
|
|
public ICancellationTokenProvider CancellationTokenProvider { get; } |
|
|
|
|
|
|
|
public AuthService(IIdentityModelAuthenticationService authenticationService) |
|
|
|
public AuthService( |
|
|
|
IIdentityModelAuthenticationService authenticationService, |
|
|
|
ILogger<NewCommand> logger, |
|
|
|
ICancellationTokenProvider cancellationTokenProvider, |
|
|
|
CliHttpClientFactory cliHttpClientFactory |
|
|
|
) |
|
|
|
{ |
|
|
|
AuthenticationService = authenticationService; |
|
|
|
Logger = logger; |
|
|
|
CancellationTokenProvider = cancellationTokenProvider; |
|
|
|
CliHttpClientFactory = cliHttpClientFactory; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task LoginAsync(string userName, string password, string organizationName = null) |
|
|
|
@ -40,11 +57,50 @@ namespace Volo.Abp.Cli.Auth |
|
|
|
File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); |
|
|
|
} |
|
|
|
|
|
|
|
public Task LogoutAsync() |
|
|
|
public async Task LogoutAsync() |
|
|
|
{ |
|
|
|
FileHelper.DeleteIfExists(CliPaths.AccessToken); |
|
|
|
FileHelper.DeleteIfExists(CliPaths.Lic); |
|
|
|
return Task.CompletedTask; |
|
|
|
string accessToken = null; |
|
|
|
if (File.Exists(CliPaths.AccessToken)) |
|
|
|
{ |
|
|
|
accessToken = File.ReadAllText(CliPaths.AccessToken); |
|
|
|
File.Delete(CliPaths.AccessToken); |
|
|
|
} |
|
|
|
|
|
|
|
if (File.Exists(CliPaths.Lic)) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrWhiteSpace(accessToken)) |
|
|
|
{ |
|
|
|
await LogoutAsync(accessToken); |
|
|
|
} |
|
|
|
|
|
|
|
File.Delete(CliPaths.Lic); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task LogoutAsync(string accessToken) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var client = CliHttpClientFactory.CreateClient(); |
|
|
|
var content = new StringContent( |
|
|
|
JsonSerializer.Serialize(new {token = accessToken}), |
|
|
|
Encoding.UTF8, "application/json" |
|
|
|
); |
|
|
|
|
|
|
|
using (var response = await client.PostAsync(CliConsts.LogoutUrl, content, CancellationTokenProvider.Token)) |
|
|
|
{ |
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
Logger.LogWarning( |
|
|
|
$"Cannot logout from remote service! Response: {response.StatusCode}-{response.ReasonPhrase}" |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
Logger.LogWarning($"Error occured while logging out from remote service. {e.Message}"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static bool IsLoggedIn() |
|
|
|
@ -52,4 +108,4 @@ namespace Volo.Abp.Cli.Auth |
|
|
|
return File.Exists(CliPaths.AccessToken); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |