fixes, refacts

This commit is contained in:
2018-08-05 04:16:21 +02:00
parent 1f6aaac1fe
commit 0c69d5abbc
19 changed files with 193 additions and 85 deletions

View File

@ -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;
}
}
}

View File

@ -1,12 +1,28 @@
using cli.Model;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using cli.Services;
namespace cli {
public class CiBuildCommand {
namespace cli
{
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;
}
}
}

View File

@ -1,19 +1,15 @@
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using cli.Services;
using System.Threading.Tasks;
using NJsonSchema;
using System.IO;
using cli.Model;
namespace cli
{
public class GenerateJsonSchema
public class GenerateJsonSchema : ICommander
{
public CommandLineApplication Integrates(CommandLineApplication rootApp)
public CommandLineApplication Integrate(CommandLineApplication rootApp)
{
CommandArgument genargclass = null;
CommandArgument genargjson = null;

View File

@ -4,11 +4,12 @@ using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using cli.Services;
using cli.Model;
namespace cli {
public class SendMailCommandProvider : ICliCommand {
public CommandLineApplication Integrates(CommandLineApplication rootApp)
public class SendMailCommandProvider : ICommander {
public CommandLineApplication Integrate(CommandLineApplication rootApp)
{
CommandArgument sendMailCommandArg = null;
CommandOption sendHelpOption = null;

View File

@ -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();
}
}
}

View File

@ -11,8 +11,9 @@ msbuild-restore:
check: run
project.lock.json: project.json
$(dnu) restore
sed 's/System.XML/System.Xml/' project.lock.json > project.lock.json.new && mv project.lock.json.new project.lock.json
@$(dnu) restore
@# fixing package id reference case, to System.Xml, from package NJsonSchema.CodeGeneration.CSharp
@sed 's/System.XML/System.Xml/' project.lock.json > project.lock.json.new && mv project.lock.json.new project.lock.json
run: project.lock.json
ASPNET_ENV=$(ASPNET_ENV) dnx --configuration=$(CONFIGURATION) run send monthly
@ -20,6 +21,5 @@ run: project.lock.json
info:
@echo $(PRJNAME)
# Due to NJsonSchema.CodeGeneration.CSharp package:
.PHONY: project.lock.json

View File

@ -1,6 +0,0 @@
using Microsoft.Extensions.CommandLineUtils;
public interface ICliCommand
{
CommandLineApplication Integrates(CommandLineApplication rootApp);
}

9
cli/Model/ICommander.cs Normal file
View File

@ -0,0 +1,9 @@
using Microsoft.Extensions.CommandLineUtils;
namespace cli.Model
{
public interface ICommander
{
CommandLineApplication Integrate(CommandLineApplication rootApp);
}
}

View File

@ -1,9 +1,7 @@

using System;
using System.IO;
using System.Threading.Tasks;
using cli.Commands;
using Microsoft.Extensions.CommandLineUtils;
using NJsonSchema;
namespace cli
{
@ -21,10 +19,11 @@ namespace cli
cliapp.ShortVersionGetter = () => "v1.0";
cliapp.LongVersionGetter = () => "version 1.0 (stable)";
rootCommandHelpOption = cliapp.HelpOption("-? | -h | --help");
var command = new SendMailCommandProvider();
command.Integrates(cliapp);
var gencmd = new GenerateJsonSchema();
gencmd.Integrates(cliapp);
(new SendMailCommandProvider()).Integrate(cliapp);
(new GenerateJsonSchema()).Integrate(cliapp);
(new AuthCommander()).Integrate(cliapp);
(new CiBuildCommand()).Integrate(cliapp);
if (args.Length == 0)
{

View File

@ -1,56 +1,34 @@
namespace cli
{
using Yavsc.Abstract.Identity;
using Yavsc;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
using Yavsc.Authentication;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class ConnectionSettings
{
public TokenInfo UserToken { get; set; }
public SiteSettings Site { get; set; }
public OAuth2AppSettings ServerApiKey { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string Authority { get; set; }
public string Audience { get; set; }
public string SiteAccessSheme { get; set; } = "http";
public string Scope { get; set; } = "profile";
[NotMapped]
[JsonIgnore]
public string AuthorizeUrl {get {
return $"{SiteAccessSheme}://{Site.Authority}/authorize";
return $"{SiteAccessSheme}://{Authority}/authorize";
} }
[NotMapped]
[JsonIgnore]
public string RedirectUrl {get {
return $"{SiteAccessSheme}://{Site.Authority}/oauth/success";
return $"{SiteAccessSheme}://{Authority}/oauth/success";
} }
[NotMapped]
[JsonIgnore]
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"]
};
}
}
}

View File

@ -1,19 +1,18 @@
using System;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Razor;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.OptionsModel;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Razor;
using Microsoft.Extensions.PlatformAbstractions;
using cli.Services;
using Microsoft.Extensions.WebEncoders;
using Yavsc;
using Yavsc.Models;
using Yavsc.Services;
using Microsoft.Data.Entity;
using Microsoft.AspNet.Authentication;
using Microsoft.Extensions.WebEncoders;
using cli.Services;
namespace cli
{
@ -24,8 +23,7 @@ namespace cli
get ; set;
}
public static SiteSettings SiteSetup { get; private set; }
public static SmtpSettings SmtpSettup { get; private set; }
public static ConnectionSettings Settings { get; private set; }
public static IConfiguration Configuration { get; set; }
public static string HostingFullName { get; private set; }
@ -44,24 +42,24 @@ namespace cli
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
Configuration = builder.Build();
ConnectionString = Configuration["Data:DefaultConnection:ConnectionString"];
AppDomain.CurrentDomain.SetData("YAVSC_CONNECTION", ConnectionString);
}
public void ConfigureServices (IServiceCollection services)
{
services.AddOptions();
var siteSettingsconf = Configuration.GetSection("Site");
services.Configure<SiteSettings>(siteSettingsconf);
var cxSettings = Configuration.GetSection("Connection");
services.Configure<ConnectionSettings>(cxSettings);
var smtpSettingsconf = Configuration.GetSection("Smtp");
services.Configure<SmtpSettings>(smtpSettingsconf);
services.AddInstance(typeof(ILoggerFactory), new LoggerFactory());
services.AddTransient(typeof(IEmailSender), typeof(MailSender));
services.AddTransient(typeof(RazorEngineHost), typeof(YaRazorEngineHost));
services.AddEntityFramework().AddNpgsql().AddDbContext<ApplicationDbContext>();
services.AddTransient((s) => new RazorTemplateEngine(s.GetService<RazorEngineHost>()));
// services.AddTransient<,>()
services.AddLogging();
services.AddTransient<EMailer>();
services.AddLocalization(options =>

View File

@ -58,7 +58,8 @@
"version": "1.0.5-rc22",
"target": "package"
},
"Yavsc.Lib.Portable": "1.0.2"
"Yavsc.Lib.Portable": "1.0.2",
"OAuth.AspNet.AuthServer": { "target": "project" }
},
"frameworks": {
"dnx451": {