From 0c69d5abbca9bbebe611776c9f68e65f73450fa7 Mon Sep 17 00:00:00 2001
From: Paul Schneider
Date: Sun, 5 Aug 2018 04:16:21 +0200
Subject: [PATCH] fixes, refacts
---
.../FileSystem/UserDirectoryInfo.cs | 8 +-
.../ViewModels/Account/RegisterViewModel.cs | 1 -
.../Accounting/AccountController.cs | 7 +-
Yavsc/Views/Account/ConfirmEmail.cshtml | 2 +-
Yavsc/project.json | 1 +
cli/Commands/AuthCommander.cs | 31 ++++++++
cli/Commands/CiBuildCommand.cs | 28 +++++--
cli/Commands/GenerateJsonSchema.cs | 10 +--
cli/Commands/SendMailCommand.cs | 5 +-
cli/Helpers/ConsoleHelpers.cs | 76 +++++++++++++++++++
cli/Makefile | 10 +--
cli/Misc/ICliCommander.cs | 6 --
cli/Model/ICommander.cs | 9 +++
cli/Program.cs | 13 ++--
cli/Settings/ConnectionSettings.cs | 38 ++--------
cli/Startup.cs | 26 +++----
cli/project.json | 3 +-
test/project.json | 1 +
yavsc.code-workspace | 3 +
19 files changed, 193 insertions(+), 85 deletions(-)
create mode 100644 cli/Commands/AuthCommander.cs
create mode 100644 cli/Helpers/ConsoleHelpers.cs
delete mode 100644 cli/Misc/ICliCommander.cs
create mode 100644 cli/Model/ICommander.cs
diff --git a/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs b/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs
index 2d5ae01d..fcf32867 100644
--- a/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs
+++ b/Yavsc.Abstract/FileSystem/UserDirectoryInfo.cs
@@ -35,7 +35,7 @@ namespace Yavsc.ViewModels.UserFiles
$"File name contains invalid chars, using path {finalPath}");
dInfo = new DirectoryInfo(
- userReposPath+FileSystemConstants.RemoteDirectorySeparator+finalPath);
+ userReposPath+Path.DirectorySeparatorChar+finalPath);
if (dInfo.Exists) {
Files = dInfo.GetFiles().Select
@@ -43,7 +43,11 @@ namespace Yavsc.ViewModels.UserFiles
CreationTime = entry.CreationTime, LastModified = entry.LastWriteTime }).ToArray();
SubDirectories = dInfo.GetDirectories().Select
( d=> new DirectoryShortInfo { Name= d.Name, IsEmpty=false } ).ToArray();
-
+ }
+ else {
+ // don't return null, but empty arrays
+ Files = new RemoteFileInfo[0];
+ SubDirectories = new DirectoryShortInfo[0];
}
}
}
diff --git a/Yavsc.Server/ViewModels/Account/RegisterViewModel.cs b/Yavsc.Server/ViewModels/Account/RegisterViewModel.cs
index b5ace84f..7816f3ef 100644
--- a/Yavsc.Server/ViewModels/Account/RegisterViewModel.cs
+++ b/Yavsc.Server/ViewModels/Account/RegisterViewModel.cs
@@ -27,7 +27,6 @@ namespace Yavsc.ViewModels.Account
[Compare("Password")]
public string ConfirmPassword { get; set; }
- public string GoogleRegId { get; set; }
}
}
diff --git a/Yavsc/Controllers/Accounting/AccountController.cs b/Yavsc/Controllers/Accounting/AccountController.cs
index 527bcecf..bfc66662 100644
--- a/Yavsc/Controllers/Accounting/AccountController.cs
+++ b/Yavsc/Controllers/Accounting/AccountController.cs
@@ -225,7 +225,7 @@ namespace Yavsc.Controllers
{
if (ModelState.IsValid)
{
- var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
+ var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
@@ -240,7 +240,8 @@ namespace Yavsc.Controllers
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
var emailSent = await _emailSender.SendEmailAsync(model.UserName, model.Email, _localizer["ConfirmYourAccountTitle"],
string.Format(_localizer["ConfirmYourAccountBody"], _siteSettings.Title, callbackUrl, _siteSettings.Slogan, _siteSettings.Audience));
- await _signInManager.SignInAsync(user, isPersistent: false);
+ // No, wait for more than a login pass submission:
+ // do not await _signInManager.SignInAsync(user, isPersistent: false);
if (emailSent==null)
{
_logger.LogWarning("User created with error sending email confirmation request");
@@ -249,7 +250,7 @@ namespace Yavsc.Controllers
_localizer["ErrorSendingEmailForConfirm"]
);
}
- else
+ else
this.NotifyInfo(
"E-mail confirmation",
_localizer["EmailSentForConfirm"]
diff --git a/Yavsc/Views/Account/ConfirmEmail.cshtml b/Yavsc/Views/Account/ConfirmEmail.cshtml
index 7be7acb0..fffccdcd 100755
--- a/Yavsc/Views/Account/ConfirmEmail.cshtml
+++ b/Yavsc/Views/Account/ConfirmEmail.cshtml
@@ -8,7 +8,7 @@
Merci d’avoir confirmé votre e-mail.
@if (User.GetUserId()==null) {
S’il vous plait,
- Cliquez ici pour vous connecter.
+ Cliquez ici pour vous connecter.
}
diff --git a/Yavsc/project.json b/Yavsc/project.json
index be929469..bf240a97 100755
--- a/Yavsc/project.json
+++ b/Yavsc/project.json
@@ -24,6 +24,7 @@
},
"userSecretsId": "aspnet5-YavscWeb-a0dadd21-2ced-43d3-96f9-7e504345102f",
"exclude": [
+ "bin",
"wwwroot",
"node_modules",
"bower_components",
diff --git a/cli/Commands/AuthCommander.cs b/cli/Commands/AuthCommander.cs
new file mode 100644
index 00000000..25db25be
--- /dev/null
+++ b/cli/Commands/AuthCommander.cs
@@ -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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/cli/Commands/CiBuildCommand.cs b/cli/Commands/CiBuildCommand.cs
index c12ee3aa..39848fca 100644
--- a/cli/Commands/CiBuildCommand.cs
+++ b/cli/Commands/CiBuildCommand.cs
@@ -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;
+ }
}
}
\ No newline at end of file
diff --git a/cli/Commands/GenerateJsonSchema.cs b/cli/Commands/GenerateJsonSchema.cs
index b118b64e..ad9ad38b 100644
--- a/cli/Commands/GenerateJsonSchema.cs
+++ b/cli/Commands/GenerateJsonSchema.cs
@@ -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;
diff --git a/cli/Commands/SendMailCommand.cs b/cli/Commands/SendMailCommand.cs
index aee985f9..caace732 100644
--- a/cli/Commands/SendMailCommand.cs
+++ b/cli/Commands/SendMailCommand.cs
@@ -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;
diff --git a/cli/Helpers/ConsoleHelpers.cs b/cli/Helpers/ConsoleHelpers.cs
new file mode 100644
index 00000000..b896dff7
--- /dev/null
+++ b/cli/Helpers/ConsoleHelpers.cs
@@ -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> GetAuthFromPass(
+ string login,
+ string pass)
+ {
+ var query = new Dictionary();
+ 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();
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/cli/Makefile b/cli/Makefile
index c98f2507..65def307 100644
--- a/cli/Makefile
+++ b/cli/Makefile
@@ -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
diff --git a/cli/Misc/ICliCommander.cs b/cli/Misc/ICliCommander.cs
deleted file mode 100644
index 69fb810a..00000000
--- a/cli/Misc/ICliCommander.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-using Microsoft.Extensions.CommandLineUtils;
-
-public interface ICliCommand
-{
- CommandLineApplication Integrates(CommandLineApplication rootApp);
-}
diff --git a/cli/Model/ICommander.cs b/cli/Model/ICommander.cs
new file mode 100644
index 00000000..75ca89e3
--- /dev/null
+++ b/cli/Model/ICommander.cs
@@ -0,0 +1,9 @@
+using Microsoft.Extensions.CommandLineUtils;
+
+namespace cli.Model
+{
+ public interface ICommander
+ {
+ CommandLineApplication Integrate(CommandLineApplication rootApp);
+ }
+}
\ No newline at end of file
diff --git a/cli/Program.cs b/cli/Program.cs
index b4f86a2c..88748467 100644
--- a/cli/Program.cs
+++ b/cli/Program.cs
@@ -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)
{
diff --git a/cli/Settings/ConnectionSettings.cs b/cli/Settings/ConnectionSettings.cs
index 266b98e3..1e876276 100644
--- a/cli/Settings/ConnectionSettings.cs
+++ b/cli/Settings/ConnectionSettings.cs
@@ -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();
- 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"]
- };
- }
+
}
}
\ No newline at end of file
diff --git a/cli/Startup.cs b/cli/Startup.cs
index afb67e10..ffae7f54 100644
--- a/cli/Startup.cs
+++ b/cli/Startup.cs
@@ -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(siteSettingsconf);
+ var cxSettings = Configuration.GetSection("Connection");
+ services.Configure(cxSettings);
var smtpSettingsconf = Configuration.GetSection("Smtp");
+
services.Configure(smtpSettingsconf);
services.AddInstance(typeof(ILoggerFactory), new LoggerFactory());
services.AddTransient(typeof(IEmailSender), typeof(MailSender));
services.AddTransient(typeof(RazorEngineHost), typeof(YaRazorEngineHost));
services.AddEntityFramework().AddNpgsql().AddDbContext();
services.AddTransient((s) => new RazorTemplateEngine(s.GetService()));
- // services.AddTransient<,>()
services.AddLogging();
services.AddTransient();
services.AddLocalization(options =>
diff --git a/cli/project.json b/cli/project.json
index a4164a43..c8471919 100644
--- a/cli/project.json
+++ b/cli/project.json
@@ -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": {
diff --git a/test/project.json b/test/project.json
index 8787ffa9..a11f6e9e 100644
--- a/test/project.json
+++ b/test/project.json
@@ -25,6 +25,7 @@
]
},
"exclude": [
+ "bin",
"wwwroot",
"node_modules",
"bower_components",
diff --git a/yavsc.code-workspace b/yavsc.code-workspace
index cf5679c7..9723a00a 100644
--- a/yavsc.code-workspace
+++ b/yavsc.code-workspace
@@ -23,6 +23,9 @@
},
{
"path": "test"
+ },
+ {
+ "path": ".vscode"
}
],
"settings": {