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 fa94eb00bb..66d16f9305 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,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Text; using System.Threading.Tasks; using IdentityModel; @@ -17,20 +18,25 @@ namespace Volo.Abp.Cli.Auth AuthenticationService = authenticationService; } - public async Task LoginAsync(string userName, string password) + public async Task LoginAsync(string userName, string password, string organizationName = null) { - var accessToken = await AuthenticationService.GetAccessTokenAsync( - new IdentityClientConfiguration( - CliUrls.AccountAbpIo, - "role email abpio_www abpio_commercial", - "abp-cli", - "1q2w3e*", - OidcConstants.GrantTypes.Password, - userName, - password - ) + var configuration = new IdentityClientConfiguration( + CliUrls.AccountAbpIo, + "role email abpio abpio_www abpio_commercial", + "abp-cli", + "1q2w3e*", + OidcConstants.GrantTypes.Password, + userName, + password ); + if (!organizationName.IsNullOrWhiteSpace()) + { + configuration["[o]abp-organization-name"] = organizationName; + } + + var accessToken = await AuthenticationService.GetAccessTokenAsync(configuration); + File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs index 4521d1fb86..030f474c96 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs @@ -44,7 +44,11 @@ namespace Volo.Abp.Cli.Commands ); } - await AuthService.LoginAsync(commandLineArgs.Target, password); + await AuthService.LoginAsync( + commandLineArgs.Target, + password, + commandLineArgs.Options.GetOrNull(Options.Organization.Short, Options.Organization.Long) + ); Logger.LogInformation($"Successfully logged in as '{commandLineArgs.Target}'"); } @@ -70,5 +74,14 @@ namespace Volo.Abp.Cli.Commands { return string.Empty; } + + public static class Options + { + public static class Organization + { + public const string Short = "o"; + public const string Long = "organization"; + } + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs index e2e6e45e49..17040ff1db 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs @@ -81,7 +81,7 @@ namespace Volo.Abp.IdentityModel get => this.GetOrDefault(nameof(RequireHttps))?.To() ?? true; set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant(); } - + public IdentityClientConfiguration() { diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs index 032bd15457..c5ce0b854d 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; @@ -117,26 +118,12 @@ namespace Volo.Abp.IdentityModel { case OidcConstants.GrantTypes.ClientCredentials: return await httpClient.RequestClientCredentialsTokenAsync( - new ClientCredentialsTokenRequest - { - Address = discoveryResponse.TokenEndpoint, - Scope = configuration.Scope, - ClientId = configuration.ClientId, - ClientSecret = configuration.ClientSecret - }, + await CreateClientCredentialsTokenRequestAsync(discoveryResponse, configuration), CancellationTokenProvider.Token ); case OidcConstants.GrantTypes.Password: return await httpClient.RequestPasswordTokenAsync( - new PasswordTokenRequest - { - Address = discoveryResponse.TokenEndpoint, - Scope = configuration.Scope, - ClientId = configuration.ClientId, - ClientSecret = configuration.ClientSecret, - UserName = configuration.UserName, - Password = configuration.UserPassword - }, + await CreatePasswordTokenRequestAsync(discoveryResponse, configuration), CancellationTokenProvider.Token ); default: @@ -144,5 +131,49 @@ namespace Volo.Abp.IdentityModel } } } + + protected virtual Task CreatePasswordTokenRequestAsync(DiscoveryResponse discoveryResponse, IdentityClientConfiguration configuration) + { + var request = new PasswordTokenRequest + { + Address = discoveryResponse.TokenEndpoint, + Scope = configuration.Scope, + ClientId = configuration.ClientId, + ClientSecret = configuration.ClientSecret, + UserName = configuration.UserName, + Password = configuration.UserPassword + }; + + AddParametersToRequestAsync(configuration, request); + + return Task.FromResult(request); + } + + protected virtual Task CreateClientCredentialsTokenRequestAsync( + DiscoveryResponse discoveryResponse, + IdentityClientConfiguration configuration) + { + var request = new ClientCredentialsTokenRequest + { + Address = discoveryResponse.TokenEndpoint, + Scope = configuration.Scope, + ClientId = configuration.ClientId, + ClientSecret = configuration.ClientSecret + }; + + AddParametersToRequestAsync(configuration, request); + + return Task.FromResult(request); + } + + protected virtual Task AddParametersToRequestAsync(IdentityClientConfiguration configuration, Request request) + { + foreach (var pair in configuration.Where(p => p.Key.StartsWith("[o]", StringComparison.OrdinalIgnoreCase))) + { + request.Parameters[pair.Key] = pair.Value; + } + + return Task.CompletedTask; + } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs index 7c46fbd1ea..00d5d1039a 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs @@ -15,7 +15,7 @@ using Volo.Abp.Uow; namespace Volo.Abp.IdentityServer.AspNetIdentity { - public class AbpResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator //ResourceOwnerPasswordValidator + public class AbpResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator { private readonly SignInManager _signInManager; private readonly IEventService _events;