From a4accfc4ad55be5d36c24287857e4877c69d1aac Mon Sep 17 00:00:00 2001 From: EngincanV Date: Tue, 27 Apr 2021 14:49:55 +0300 Subject: [PATCH 1/2] Update LogoutAsync for deallocate developer seat when logged out --- .../Volo/Abp/Cli/Auth/AuthService.cs | 63 +++++++++++++++++-- .../Volo/Abp/Cli/CliConsts.cs | 2 + 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs index f3cb291f56..1f88d2a5a4 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs @@ -1,8 +1,16 @@ 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.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.IdentityModel; using Volo.Abp.IO; @@ -12,10 +20,18 @@ namespace Volo.Abp.Cli.Auth public class AuthService : ITransientDependency { protected IIdentityModelAuthenticationService AuthenticationService { get; } - - public AuthService(IIdentityModelAuthenticationService authenticationService) + protected ILogger Logger { get; } + protected CliHttpClientFactory CliHttpClientFactory { get; } + + public AuthService( + IIdentityModelAuthenticationService authenticationService, + ILogger logger, + CliHttpClientFactory cliHttpClientFactory + ) { AuthenticationService = authenticationService; + Logger = logger; + CliHttpClientFactory = cliHttpClientFactory; } 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); } - public Task LogoutAsync() + public async Task LogoutAsync() { - FileHelper.DeleteIfExists(CliPaths.AccessToken); - FileHelper.DeleteIfExists(CliPaths.Lic); - return Task.CompletedTask; + string accessToken = ""; + if (File.Exists(CliPaths.AccessToken)) + { + 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() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs index 37dbbba318..77fa3b8236 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliConsts.cs @@ -9,5 +9,7 @@ public const string DocsLink = "https://docs.abp.io"; public const string HttpClientName = "AbpHttpClient"; + + public const string LogoutUrl = CliUrls.WwwAbpIo + "api/license/logout"; } } From a9dcb03c99467bff7a837985660ec68607f9f5de Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Wed, 28 Apr 2021 03:01:37 +0300 Subject: [PATCH 2/2] refactor logout method --- .../Volo/Abp/Cli/Auth/AuthService.cs | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs index 1f88d2a5a4..61008ee826 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs @@ -6,14 +6,12 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; 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.IdentityModel; -using Volo.Abp.IO; +using Volo.Abp.Threading; namespace Volo.Abp.Cli.Auth { @@ -22,15 +20,18 @@ namespace Volo.Abp.Cli.Auth protected IIdentityModelAuthenticationService AuthenticationService { get; } protected ILogger Logger { get; } protected CliHttpClientFactory CliHttpClientFactory { get; } - + public ICancellationTokenProvider CancellationTokenProvider { get; } + public AuthService( - IIdentityModelAuthenticationService authenticationService, + IIdentityModelAuthenticationService authenticationService, ILogger logger, + ICancellationTokenProvider cancellationTokenProvider, CliHttpClientFactory cliHttpClientFactory ) { AuthenticationService = authenticationService; Logger = logger; + CancellationTokenProvider = cancellationTokenProvider; CliHttpClientFactory = cliHttpClientFactory; } @@ -58,21 +59,21 @@ namespace Volo.Abp.Cli.Auth public async Task LogoutAsync() { - string accessToken = ""; + string accessToken = null; if (File.Exists(CliPaths.AccessToken)) { accessToken = File.ReadAllText(CliPaths.AccessToken); - FileHelper.DeleteIfExists(CliPaths.AccessToken); + File.Delete(CliPaths.AccessToken); } - + if (File.Exists(CliPaths.Lic)) { if (!string.IsNullOrWhiteSpace(accessToken)) { await LogoutAsync(accessToken); } - - FileHelper.DeleteIfExists(CliPaths.Lic); + + File.Delete(CliPaths.Lic); } } @@ -81,20 +82,24 @@ namespace Volo.Abp.Cli.Auth try { var client = CliHttpClientFactory.CreateClient(); - var data = JsonSerializer.Serialize(new { token = accessToken }); - var content = new StringContent(data, Encoding.UTF8, "application/json"); + var content = new StringContent( + JsonSerializer.Serialize(new {token = accessToken}), + Encoding.UTF8, "application/json" + ); - using (var response = await client.PostAsync(CliConsts.LogoutUrl, content, CancellationToken.None)) + using (var response = await client.PostAsync(CliConsts.LogoutUrl, content, CancellationTokenProvider.Token)) { if (!response.IsSuccessStatusCode) { - Logger.LogWarning($"Cannot logout! Status Code: '{response.StatusCode}'"); + Logger.LogWarning( + $"Cannot logout from remote service! Response: {response.StatusCode}-{response.ReasonPhrase}" + ); } } } catch (Exception e) { - Logger.LogWarning($"Cannot logout. {e.Message}"); + Logger.LogWarning($"Error occured while logging out from remote service. {e.Message}"); } } @@ -103,4 +108,4 @@ namespace Volo.Abp.Cli.Auth return File.Exists(CliPaths.AccessToken); } } -} +} \ No newline at end of file