diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index ccf697a..7404240 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -13,17 +13,16 @@ namespace nuget_host.Controllers { private readonly ILogger _logger; readonly IHostingEnvironment _environment; - public IDataProtector DataProtector { get; } - public SmtpSettings Options { get; } //set only via Secret Manager + public SmtpSettings _smtpSettings { get; } //set only via Secret Manager public HomeController( IOptions smtpSettings, - IDataProtectionProvider provider, Microsoft.AspNetCore.Hosting.IHostingEnvironment environment, ILogger logger) + IHostingEnvironment environment, + ILogger logger) { _environment = environment; _logger = logger; - Options = smtpSettings.Value; - DataProtector = provider.CreateProtector(Options.ProtectionTitle); + _smtpSettings = smtpSettings.Value; } public IActionResult Index() diff --git a/Controllers/PackagesController.cs b/Controllers/PackagesController.cs index ca03c16..ac4240d 100644 --- a/Controllers/PackagesController.cs +++ b/Controllers/PackagesController.cs @@ -7,7 +7,9 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using NuGet.Packaging; +using nuget_host.Entities; namespace nuget_host.Controllers { @@ -16,10 +18,16 @@ namespace nuget_host.Controllers private readonly ILogger logger; private readonly IDataProtector protector; - public PackagesController(ILoggerFactory loggerFactory, IDataProtectionProvider provider) + private readonly NugetSettings nugetSettings; + + public PackagesController( + ILoggerFactory loggerFactory, + IDataProtectionProvider provider, + IOptions nugetOptions) { logger = loggerFactory.CreateLogger(); - protector = provider.CreateProtector("Packages.v1"); + nugetSettings = nugetOptions.Value; + protector = provider.CreateProtector(nugetSettings.ProtectionTitle); } [HttpPut("packages/{*spec}")] @@ -61,7 +69,7 @@ namespace nuget_host.Controllers var version = reader.GetVersion(); - path = Path.Combine(Startup.SourceDir, + path = Path.Combine(nugetSettings.PackagesRootDir, Path.Combine(pkgid, Path.Combine(version.ToFullString(), $"{pkgid}-{version}.nupkg"))); @@ -85,12 +93,7 @@ namespace nuget_host.Controllers } else { - ViewData["spec"] = spec; - // TODO Assert valid sem ver spec - var filelst = new DirectoryInfo(Startup.SourceDir); - var fi = new FileInfo(spec); - var lst = filelst.GetFiles(fi.Name + "*.nupkg"); - ViewData["lst"] = lst.Select(entry => entry.Name); + return BadRequest(); } return Ok(ViewData); } @@ -101,18 +104,12 @@ namespace nuget_host.Controllers if (string.IsNullOrEmpty(spec)) { ViewData["warn"] = "no spec"; - /* - usr/lib/mono/msbuild/Current/bin/NuGet.targets(128,5): error : Failed to retrieve information about 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' from remote source 'http://localhost:5000/Packages/FindPackagesById()?id='Microsoft.VisualStudio.Web.CodeGeneration.Tools'&semVerLevel=2.0.0'. [/home/paul/workspace/nuget-host/nuget-host.csproj] - - - - */ } else { ViewData["spec"] = spec; // TODO Assert valid sem ver spec - var filelst = new DirectoryInfo(Startup.SourceDir); + var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir); var fi = new FileInfo(spec); var lst = filelst.GetDirectories(spec); ViewData["lst"] = lst.Select(entry => entry.Name); @@ -120,6 +117,8 @@ namespace nuget_host.Controllers return Ok(ViewData); } + + [Authorize] [HttpGet("api/get-key/{*apikey}")] public IActionResult GetApiKey(string apiKey) diff --git a/Entities/NugetSettings.cs b/Entities/NugetSettings.cs new file mode 100644 index 0000000..199d594 --- /dev/null +++ b/Entities/NugetSettings.cs @@ -0,0 +1,9 @@ +namespace nuget_host.Entities +{ + public class NugetSettings + { + public string ProtectionTitle {get; set;} + public string PackagesRootDir {get; set;} + + } +} \ No newline at end of file diff --git a/Entities/SmtpSettings.cs b/Entities/SmtpSettings.cs index 41e56de..8f64a27 100644 --- a/Entities/SmtpSettings.cs +++ b/Entities/SmtpSettings.cs @@ -8,6 +8,5 @@ namespace nuget_host.Entities public string SenderEMail {get; set;} public string UserName {get; set;} public string Password {get; set;} - public string ProtectionTitle {get; set;} } } \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index 0c872b0..6ec9234 100644 --- a/Startup.cs +++ b/Startup.cs @@ -29,8 +29,6 @@ namespace nuget_host } public IConfiguration Configuration { get; } - public static string ExternalUrl { get; private set; } - public static string SourceDir { get; private set; } public static string RootApiKeySecret { get; private set; } // This method gets called by the runtime. Use this method to add services to the container. @@ -55,6 +53,8 @@ namespace nuget_host var smtpSettingsconf = Configuration.GetSection("SmtpSettings"); services.Configure(smtpSettingsconf); + var nugetSettingsconf = Configuration.GetSection("NugetSettings"); + services.Configure(nugetSettingsconf); } @@ -71,9 +71,6 @@ namespace nuget_host app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } - ExternalUrl = Configuration["NuGet:ExternalUrl"]; - SourceDir = Configuration["NuGet:SourceDir"]; - RootApiKeySecret = Configuration["RootApiKeySecret"]; app.UseStaticFiles(); diff --git a/appsettings.Development.json b/appsettings.Development.json index d007c57..ffa68ab 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -1,7 +1,12 @@ { "NuGet": { "ExternalUrl" : "", "NuGet": { "ExternalUrl" : "" + "PackagesRootDir" : "", + "ProtectionTitle": "protected-data-v1" }, "ConnectionStrings": { "DefaultConnection": "Server=;Port=;Database=;Username=;Password=;" }, "AllowedHosts": "*", - "SmtpSettings": { - "Server": "localhost", + "Smtp": { + "Server": "", "Port": 25, - "SenderName": "Paul Schneider", - "SenderEmail": "paul@pschneider.fr", - "ProtectionTitle": "protected-data-v1" + "SenderName": "", + "SenderEmail": "" }, "Logging": { "LogLevel": {