yavsc/src/cli/Commands/AuthCommander.cs

120 lines
4.9 KiB
C#
Raw Normal View History

2018-08-05 04:16:21 +02:00
2019-06-25 01:51:23 +01:00
using System;
using System.Collections.Generic;
using System.Text;
2018-08-05 04:16:21 +02:00
using cli.Model;
using Microsoft.Extensions.CommandLineUtils;
2019-06-25 01:51:23 +01:00
using Microsoft.Extensions.Logging;
using Yavsc.Authentication;
2018-08-05 04:16:21 +02:00
namespace cli.Commands
{
public class AuthCommander : ICommander
{
2019-06-25 01:51:23 +01:00
private CommandArgument _login;
private CommandOption _apiKey;
private CommandOption _secret;
private CommandOption _scope;
private CommandOption _save;
2020-09-12 01:11:30 +01:00
readonly ILogger _logger;
2019-06-25 01:51:23 +01:00
public AuthCommander(ILoggerFactory loggerFactory)
2020-09-12 01:11:30 +01:00
{
_logger = loggerFactory.CreateLogger<AuthCommander>();
}
2019-06-25 01:51:23 +01:00
2018-08-05 04:16:21 +02:00
public CommandLineApplication Integrate(CommandLineApplication rootApp)
{
CommandLineApplication authApp = rootApp.Command("auth",
(target) =>
{
target.FullName = "Authentication methods";
target.Description = "Login, save credentials and get authorized.";
target.HelpOption("-? | -h | --help");
2020-09-12 01:11:30 +01:00
var loginCommand = target.Command("login", app =>
{
2019-06-25 01:51:23 +01:00
_login = app.Argument("login", "login to use", true);
_apiKey = app.Option("-a | --api", "API key to use against authorization server", CommandOptionType.SingleValue);
2020-09-12 01:11:30 +01:00
_secret = app.Option("-e | --secret", "Secret phrase associated to API key", CommandOptionType.SingleValue);
_scope = app.Option("-c | --scope", "invoked scope asking for a security token", CommandOptionType.SingleValue);
_save = app.Option("-s | --save", "Save authentication token to given file", CommandOptionType.SingleValue);
2018-08-05 04:16:21 +02:00
app.HelpOption("-? | -h | --help");
2020-09-12 01:11:30 +01:00
});
loginCommand.OnExecute(async () =>
2018-08-05 04:16:21 +02:00
{
2020-09-12 01:11:30 +01:00
string authUrl = Startup.ConnectionSettings.AuthorizeUrl;
string redirect = Startup.ConnectionSettings.RedirectUrl;
string tokenUrl = Startup.ConnectionSettings.AccessTokenUrl;
2019-06-25 01:51:23 +01:00
2020-09-12 01:11:30 +01:00
OAuthenticator oauthor = new OAuthenticator(_apiKey.HasValue() ? _apiKey.Value() : Startup.ConnectionSettings.ClientId,
_secret.HasValue() ? _secret.Value() : Startup.ConnectionSettings.ClientSecret,
_scope.HasValue() ? _scope.Value() : Startup.ConnectionSettings.Scope,
new Uri(authUrl), new Uri(redirect), new Uri(tokenUrl));
Dictionary<string, string> query = new Dictionary<string, string>
{
["username"] = _login.Value,
["password"] = GetPassword(_login.Value),
["grant_type"] = "password"
};
try
{
2019-06-25 01:51:23 +01:00
var result = await oauthor.RequestAccessTokenAsync(query);
Startup.UserConnectionSettings.AccessToken = result["access_token"];
Startup.UserConnectionSettings.ExpiresIn = result["expires_in"];
Startup.UserConnectionSettings.RefreshToken = result["refresh_token"];
Startup.UserConnectionSettings.TokenType = result["token_type"];
Startup.UserConnectionSettings.UserName = _login.Value;
2020-09-12 01:11:30 +01:00
Startup.SaveCredentials(_save.HasValue() ? _save.Value() : Startup.UserConnectionsettingsFileName);
2019-06-25 01:51:23 +01:00
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
2018-08-05 04:16:21 +02:00
return 0;
});
2019-06-25 01:51:23 +01:00
}, false);
2018-08-05 04:16:21 +02:00
return authApp;
}
2019-06-25 01:51:23 +01:00
public static string GetPassword(string userName)
{
var oldBack = Console.BackgroundColor;
var oldFore = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"{userName}'s password:");
Console.BackgroundColor = oldBack;
Console.ForegroundColor = oldFore;
var pwd = new StringBuilder();
while (true)
{
var len = pwd.ToString().Length;
ConsoleKeyInfo i = Console.ReadKey(true);
if (i.Key == ConsoleKey.Enter)
{
break;
}
else if (i.Key == ConsoleKey.Backspace)
{
if (pwd.Length > 0)
{
pwd.Remove(len - 1, 1);
Console.Write("\b \b");
}
}
else
{
pwd.Append(i.KeyChar);
Console.Write("*");
}
}
Console.WriteLine();
return pwd.ToString();
}
2020-09-12 01:11:30 +01:00
2018-08-05 04:16:21 +02:00
}
2020-09-12 01:11:30 +01:00
}