Implemented login

pull/1100/head
Halil ibrahim Kalkan 7 years ago
parent 09298d4d9a
commit 6d2d52095c

@ -21,6 +21,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Ddd.Domain\Volo.Abp.Ddd.Domain.csproj" />
<ProjectReference Include="..\Volo.Abp.IdentityModel\Volo.Abp.IdentityModel.csproj" />
<ProjectReference Include="..\Volo.Abp.Json\Volo.Abp.Json.csproj" />
</ItemGroup>

@ -1,6 +1,7 @@
using System.Text;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Domain;
using Volo.Abp.IdentityModel;
using Volo.Abp.Json;
using Volo.Abp.Modularity;
@ -8,7 +9,8 @@ namespace Volo.Abp.Cli
{
[DependsOn(
typeof(AbpDddDomainModule),
typeof(AbpJsonModule)
typeof(AbpJsonModule),
typeof(AbpIdentityModelModule)
)]
public class AbpCliCoreModule : AbpModule
{

@ -1,18 +1,43 @@
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using IdentityModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.IdentityModel;
using Volo.Abp.IO;
namespace Volo.Abp.Cli.Auth
{
public class AuthService : ITransientDependency
{
protected IIdentityModelAuthenticationService AuthenticationService { get; }
public AuthService(IIdentityModelAuthenticationService authenticationService)
{
AuthenticationService = authenticationService;
}
public async Task LoginAsync(string userName, string password)
{
var accessToken = await AuthenticationService.GetAccessTokenAsync(
new IdentityClientConfiguration(
"https://localhost:44333", //TODO: Configure
"role email abpio_www",
"abp-cli",
"1q2w3e*",
OidcConstants.GrantTypes.Password,
userName,
password
)
);
File.WriteAllText(CliPaths.AccessToken, accessToken, Encoding.UTF8);
}
public async Task LogoutAsync()
public Task LogoutAsync()
{
FileHelper.DeleteIfExists(CliPaths.AccessToken);
return Task.CompletedTask;
}
}
}

@ -5,8 +5,10 @@ namespace Volo.Abp.Cli
{
public static class CliPaths
{
public static string TemplateCachePath => Path.Combine(AbpRootPath, "templates");
public static string CliLogPath => Path.Combine(AbpRootPath, "cli", "logs");
public static string TemplateCache => Path.Combine(AbpRootPath, "templates"); //TODO: Move somewhere else?
public static string Log => Path.Combine(AbpRootPath, "cli", "logs");
public static string Root => Path.Combine(AbpRootPath, "cli");
public static string AccessToken => Path.Combine(AbpRootPath, "cli", "access-token.bin");
private static readonly string AbpRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".abp");
}

@ -1,9 +1,10 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Auth;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Commands
@ -24,8 +25,33 @@ namespace Volo.Abp.Cli.Commands
{
if (commandLineArgs.Target.IsNullOrEmpty())
{
Logger.LogWarning("Username is missing.");
LogHelp();
return;
}
Console.Write("Password: ");
var password = ConsoleHelper.ReadSecret();
if (password.IsNullOrWhiteSpace())
{
Logger.LogWarning("Password is missing.");
LogHelp();
return;
}
await AuthService.LoginAsync(commandLineArgs.Target, password);
Console.WriteLine($"Successfully logged in as '{commandLineArgs.Target}'");
}
private void LogHelp()
{
Logger.LogWarning("");
Logger.LogWarning("Usage:");
Logger.LogWarning(" abp login <username>");
Logger.LogWarning("");
Logger.LogWarning("Example:");
Logger.LogWarning(" abp login john");
}
}
}

@ -0,0 +1,26 @@
using System;
using System.Text;
namespace Volo.Abp.Cli.Utils
{
public static class ConsoleHelper
{
public static string ReadSecret()
{
var sb = new StringBuilder();
while (true)
{
var keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Enter)
{
break;
}
sb.Append(keyInfo.KeyChar);
}
return sb.ToString();
}
}
}

@ -20,7 +20,7 @@ namespace Volo.Abp.ProjectBuilding
public async Task<TemplateFile> GetAsync(string templateName, string version)
{
var localCacheFolder = Path.Combine(CliPaths.TemplateCachePath, version);
var localCacheFolder = Path.Combine(CliPaths.TemplateCache, version);
DirectoryHelper.CreateIfNotExists(localCacheFolder);
var localCacheFile = Path.Combine(localCacheFolder, templateName + ".zip");

@ -9,6 +9,9 @@ namespace Volo.Abp.Cli
)]
public class AbpCliModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
}
}
}

@ -14,7 +14,7 @@ namespace Volo.Abp.Cli
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(Path.Combine(CliPaths.CliLogPath, "abp-cli-logs.txt"))
.WriteTo.File(Path.Combine(CliPaths.Log, "abp-cli-logs.txt"))
.WriteTo.Console()
.CreateLogger();

Loading…
Cancel
Save