Browse Source

IdentityModelAuthenticationService support custom parameters

pull/1689/head
Halil İbrahim Kalkan 7 years ago
parent
commit
08837f1fba
  1. 30
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Auth/AuthService.cs
  2. 15
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/LoginCommand.cs
  3. 2
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs
  4. 63
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs
  5. 2
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs

30
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.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using IdentityModel; using IdentityModel;
@ -17,20 +18,25 @@ namespace Volo.Abp.Cli.Auth
AuthenticationService = authenticationService; 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( var configuration = new IdentityClientConfiguration(
new IdentityClientConfiguration( CliUrls.AccountAbpIo,
CliUrls.AccountAbpIo, "role email abpio abpio_www abpio_commercial",
"role email abpio_www abpio_commercial", "abp-cli",
"abp-cli", "1q2w3e*",
"1q2w3e*", OidcConstants.GrantTypes.Password,
OidcConstants.GrantTypes.Password, userName,
userName, password
password
)
); );
if (!organizationName.IsNullOrWhiteSpace())
{
configuration["[o]abp-organization-name"] = organizationName;
}
var accessToken = await AuthenticationService.GetAccessTokenAsync(configuration);
File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8); File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8);
} }

15
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}'"); Logger.LogInformation($"Successfully logged in as '{commandLineArgs.Target}'");
} }
@ -70,5 +74,14 @@ namespace Volo.Abp.Cli.Commands
{ {
return string.Empty; return string.Empty;
} }
public static class Options
{
public static class Organization
{
public const string Short = "o";
public const string Long = "organization";
}
}
} }
} }

2
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs

@ -81,7 +81,7 @@ namespace Volo.Abp.IdentityModel
get => this.GetOrDefault(nameof(RequireHttps))?.To<bool>() ?? true; get => this.GetOrDefault(nameof(RequireHttps))?.To<bool>() ?? true;
set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant(); set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant();
} }
public IdentityClientConfiguration() public IdentityClientConfiguration()
{ {

63
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -117,26 +118,12 @@ namespace Volo.Abp.IdentityModel
{ {
case OidcConstants.GrantTypes.ClientCredentials: case OidcConstants.GrantTypes.ClientCredentials:
return await httpClient.RequestClientCredentialsTokenAsync( return await httpClient.RequestClientCredentialsTokenAsync(
new ClientCredentialsTokenRequest await CreateClientCredentialsTokenRequestAsync(discoveryResponse, configuration),
{
Address = discoveryResponse.TokenEndpoint,
Scope = configuration.Scope,
ClientId = configuration.ClientId,
ClientSecret = configuration.ClientSecret
},
CancellationTokenProvider.Token CancellationTokenProvider.Token
); );
case OidcConstants.GrantTypes.Password: case OidcConstants.GrantTypes.Password:
return await httpClient.RequestPasswordTokenAsync( return await httpClient.RequestPasswordTokenAsync(
new PasswordTokenRequest await CreatePasswordTokenRequestAsync(discoveryResponse, configuration),
{
Address = discoveryResponse.TokenEndpoint,
Scope = configuration.Scope,
ClientId = configuration.ClientId,
ClientSecret = configuration.ClientSecret,
UserName = configuration.UserName,
Password = configuration.UserPassword
},
CancellationTokenProvider.Token CancellationTokenProvider.Token
); );
default: default:
@ -144,5 +131,49 @@ namespace Volo.Abp.IdentityModel
} }
} }
} }
protected virtual Task<PasswordTokenRequest> 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<ClientCredentialsTokenRequest> 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;
}
} }
} }

2
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 namespace Volo.Abp.IdentityServer.AspNetIdentity
{ {
public class AbpResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator //ResourceOwnerPasswordValidator<IdentityUser> public class AbpResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{ {
private readonly SignInManager<IdentityUser> _signInManager; private readonly SignInManager<IdentityUser> _signInManager;
private readonly IEventService _events; private readonly IEventService _events;

Loading…
Cancel
Save