|
|
@ -1,8 +1,16 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.IO; |
|
|
using System.IO; |
|
|
|
|
|
using System.Net.Http; |
|
|
using System.Text; |
|
|
using System.Text; |
|
|
|
|
|
using System.Text.Json; |
|
|
|
|
|
using System.Threading; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using IdentityModel; |
|
|
using IdentityModel; |
|
|
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
using Volo.Abp.Cli.Commands; |
|
|
|
|
|
using Volo.Abp.Cli.Http; |
|
|
|
|
|
using Volo.Abp.Cli.ProjectBuilding; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
using Volo.Abp.DependencyInjection; |
|
|
using Volo.Abp.IdentityModel; |
|
|
using Volo.Abp.IdentityModel; |
|
|
using Volo.Abp.IO; |
|
|
using Volo.Abp.IO; |
|
|
@ -12,10 +20,18 @@ namespace Volo.Abp.Cli.Auth |
|
|
public class AuthService : ITransientDependency |
|
|
public class AuthService : ITransientDependency |
|
|
{ |
|
|
{ |
|
|
protected IIdentityModelAuthenticationService AuthenticationService { get; } |
|
|
protected IIdentityModelAuthenticationService AuthenticationService { get; } |
|
|
|
|
|
protected ILogger<NewCommand> Logger { get; } |
|
|
public AuthService(IIdentityModelAuthenticationService authenticationService) |
|
|
protected CliHttpClientFactory CliHttpClientFactory { get; } |
|
|
|
|
|
|
|
|
|
|
|
public AuthService( |
|
|
|
|
|
IIdentityModelAuthenticationService authenticationService, |
|
|
|
|
|
ILogger<NewCommand> logger, |
|
|
|
|
|
CliHttpClientFactory cliHttpClientFactory |
|
|
|
|
|
) |
|
|
{ |
|
|
{ |
|
|
AuthenticationService = authenticationService; |
|
|
AuthenticationService = authenticationService; |
|
|
|
|
|
Logger = logger; |
|
|
|
|
|
CliHttpClientFactory = cliHttpClientFactory; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task LoginAsync(string userName, string password, string organizationName = null) |
|
|
public async Task LoginAsync(string userName, string password, string organizationName = null) |
|
|
@ -40,11 +56,46 @@ namespace Volo.Abp.Cli.Auth |
|
|
File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); |
|
|
File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task LogoutAsync() |
|
|
public async Task LogoutAsync() |
|
|
{ |
|
|
{ |
|
|
FileHelper.DeleteIfExists(CliPaths.AccessToken); |
|
|
string accessToken = ""; |
|
|
FileHelper.DeleteIfExists(CliPaths.Lic); |
|
|
if (File.Exists(CliPaths.AccessToken)) |
|
|
return Task.CompletedTask; |
|
|
{ |
|
|
|
|
|
accessToken = File.ReadAllText(CliPaths.AccessToken); |
|
|
|
|
|
FileHelper.DeleteIfExists(CliPaths.AccessToken); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(CliPaths.Lic)) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(accessToken)) |
|
|
|
|
|
{ |
|
|
|
|
|
await LogoutAsync(accessToken); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
FileHelper.DeleteIfExists(CliPaths.Lic); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task LogoutAsync(string accessToken) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
var client = CliHttpClientFactory.CreateClient(); |
|
|
|
|
|
var data = JsonSerializer.Serialize(new { token = accessToken }); |
|
|
|
|
|
var content = new StringContent(data, Encoding.UTF8, "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
using (var response = await client.PostAsync(CliConsts.LogoutUrl, content, CancellationToken.None)) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.LogWarning($"Cannot logout! Status Code: '{response.StatusCode}'"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception e) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.LogWarning($"Cannot logout. {e.Message}"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static bool IsLoggedIn() |
|
|
public static bool IsLoggedIn() |
|
|
|