fixes, refacts
parent
1f6aaac1fe
commit
0c69d5abbc
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
using cli.Model;
|
||||||
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
|
|
||||||
|
namespace cli.Commands
|
||||||
|
{
|
||||||
|
public class AuthCommander : ICommander
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
var loginCommand = target.Command("login", app => {
|
||||||
|
var loginarg = app.Argument("login", "login to use", true);
|
||||||
|
app.Option( "-s | --save", "Save authentication token to file", CommandOptionType.NoValue);
|
||||||
|
app.HelpOption("-? | -h | --help");
|
||||||
|
} );
|
||||||
|
}, false);
|
||||||
|
authApp.OnExecute(()=>
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
return authApp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,28 @@
|
|||||||
|
|
||||||
|
using cli.Model;
|
||||||
using Microsoft.Extensions.CommandLineUtils;
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using cli.Services;
|
|
||||||
|
|
||||||
namespace cli {
|
namespace cli
|
||||||
|
{
|
||||||
|
|
||||||
public class CiBuildCommand {
|
public class CiBuildCommand : ICommander
|
||||||
|
{
|
||||||
|
public CommandLineApplication Integrate(CommandLineApplication rootApp)
|
||||||
|
{
|
||||||
|
CommandLineApplication ciBuildApp = rootApp.Command("build",
|
||||||
|
(target) =>
|
||||||
|
{
|
||||||
|
target.FullName = "Build this project.";
|
||||||
|
target.Description = "Build this project, as specified in .paul-ci.json";
|
||||||
|
target.HelpOption("-? | -h | --help");
|
||||||
|
}, false);
|
||||||
|
ciBuildApp.OnExecute(()=>
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
return ciBuildApp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using cli.Model;
|
||||||
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
|
using Yavsc.Authentication;
|
||||||
|
using static OAuth.AspNet.AuthServer.Constants;
|
||||||
|
|
||||||
|
namespace cli.Helpers
|
||||||
|
{
|
||||||
|
public static class ConsoleHelpers
|
||||||
|
{
|
||||||
|
public static CommandLineApplication Integrate(this CommandLineApplication rootApp, ICommander commander)
|
||||||
|
{
|
||||||
|
return commander.Integrate(rootApp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static OAuthenticator OAuthorInstance { get; set; }
|
||||||
|
public static OAuthenticator InitAuthor(
|
||||||
|
this ConnectionSettings settings,
|
||||||
|
string clientId,
|
||||||
|
string clientSecret,
|
||||||
|
string scope,
|
||||||
|
string authorizeUrl,
|
||||||
|
string redirectUrl,
|
||||||
|
string accessTokenUrl)
|
||||||
|
{
|
||||||
|
return OAuthorInstance = new OAuthenticator(settings.ClientId,
|
||||||
|
settings.ClientSecret,
|
||||||
|
settings.Scope,
|
||||||
|
new Uri(settings.AuthorizeUrl), new Uri(settings.RedirectUrl), new Uri(settings.AccessTokenUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<IDictionary<string, string>> GetAuthFromPass(
|
||||||
|
string login,
|
||||||
|
string pass)
|
||||||
|
{
|
||||||
|
var query = new Dictionary<string, string>();
|
||||||
|
query[Parameters.Username] = login;
|
||||||
|
query[Parameters.Password] = pass;
|
||||||
|
query[Parameters.GrantType] = GrantTypes.Password;
|
||||||
|
return await OAuthorInstance.RequestAccessTokenAsync(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetPassword()
|
||||||
|
{
|
||||||
|
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("*");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pwd.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
using Microsoft.Extensions.CommandLineUtils;
|
|
||||||
|
|
||||||
public interface ICliCommand
|
|
||||||
{
|
|
||||||
CommandLineApplication Integrates(CommandLineApplication rootApp);
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
|
|
||||||
|
namespace cli.Model
|
||||||
|
{
|
||||||
|
public interface ICommander
|
||||||
|
{
|
||||||
|
CommandLineApplication Integrate(CommandLineApplication rootApp);
|
||||||
|
}
|
||||||
|
}
|
@ -1,56 +1,34 @@
|
|||||||
namespace cli
|
namespace cli
|
||||||
{
|
{
|
||||||
using Yavsc.Abstract.Identity;
|
|
||||||
using Yavsc;
|
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Yavsc.Authentication;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
public class ConnectionSettings
|
public class ConnectionSettings
|
||||||
{
|
{
|
||||||
public TokenInfo UserToken { get; set; }
|
public string ClientId { get; set; }
|
||||||
public SiteSettings Site { get; set; }
|
public string ClientSecret { get; set; }
|
||||||
public OAuth2AppSettings ServerApiKey { get; set; }
|
public string Authority { get; set; }
|
||||||
|
public string Audience { get; set; }
|
||||||
public string SiteAccessSheme { get; set; } = "http";
|
public string SiteAccessSheme { get; set; } = "http";
|
||||||
public string Scope { get; set; } = "profile";
|
public string Scope { get; set; } = "profile";
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string AuthorizeUrl {get {
|
public string AuthorizeUrl {get {
|
||||||
return $"{SiteAccessSheme}://{Site.Authority}/authorize";
|
return $"{SiteAccessSheme}://{Authority}/authorize";
|
||||||
} }
|
} }
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string RedirectUrl {get {
|
public string RedirectUrl {get {
|
||||||
return $"{SiteAccessSheme}://{Site.Authority}/oauth/success";
|
return $"{SiteAccessSheme}://{Authority}/oauth/success";
|
||||||
} }
|
} }
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string AccessTokenUrl {get {
|
public string AccessTokenUrl {get {
|
||||||
return $"{SiteAccessSheme}://{Site.Authority}/token";
|
return $"{SiteAccessSheme}://{Authority}/token";
|
||||||
} }
|
} }
|
||||||
|
|
||||||
public async Task InitUserTokenFromLoginPass(string login, string pass)
|
|
||||||
{
|
|
||||||
var oauthor =new OAuthenticator( ServerApiKey.ClientId, ServerApiKey.ClientSecret, Scope,
|
|
||||||
new Uri( AuthorizeUrl) , new Uri(RedirectUrl) , new Uri(AccessTokenUrl));
|
|
||||||
var query = new Dictionary<string,string>();
|
|
||||||
query["username"]=login;
|
|
||||||
query["password"]=pass;
|
|
||||||
query["grant_type"]="password";
|
|
||||||
var result = await oauthor.RequestAccessTokenAsync(query);
|
|
||||||
UserToken = new TokenInfo {
|
|
||||||
AccessToken = result["access_token"],
|
|
||||||
RefreshToken = result["refresh_token"],
|
|
||||||
Received = DateTime.Now,
|
|
||||||
ExpiresIn = int.Parse(result["expires_in"]),
|
|
||||||
TokenType = result["token_type"]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue