ci & admin & auth & cli

broken/ef
Paul Schneider 3 years ago
parent 981f3209e0
commit 5cb35f54d5
12 changed files with 143 additions and 34 deletions

@ -7,8 +7,7 @@ image: busybox:latest
before_script: before_script:
- dotnet restore - dotnet restore
after_script: #after_script:
- dotnet nuget remove source gitlab
nonreg: nonreg:
stage: test stage: test

@ -7,9 +7,10 @@ using Newtonsoft.Json;
namespace nuget_cli namespace nuget_cli
{ {
public class nugetdresp { public class nugetdresp
public int ecode {get; set; } {
public string message {get; set; } public int ecode { get; set; }
public string message { get; set; }
public string id { get; set; } public string id { get; set; }
} }
public class UploadFilesToServerUsingWebRequest public class UploadFilesToServerUsingWebRequest
@ -96,10 +97,10 @@ namespace nuget_cli
var hrep = resp as HttpWebResponse; var hrep = resp as HttpWebResponse;
report.StatusCode = hrep.StatusCode.ToString(); report.StatusCode = hrep.StatusCode.ToString();
// ecode == 1 => package already present server side. // ecode == 1 => package already present server side.
report.OK = hrep.StatusCode == report.AlreadyPresent = res.ecode == 1;
HttpStatusCode.Accepted report.OK = hrep.StatusCode == HttpStatusCode.Accepted
|| hrep.StatusCode == HttpStatusCode.OK || hrep.StatusCode == HttpStatusCode.OK
|| res.ecode == 1; || report.AlreadyPresent;
} }
else throw new Exception("Invalid server response type"); else throw new Exception("Invalid server response type");
} }

@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Authorization;
namespace nuget_host.Authorization
{
internal class ValidApiKeyRequirement : IAuthorizationRequirement
{
}
}

@ -0,0 +1,13 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace nuget_host.Authorization
{
internal class ValidApiKeyRequirementHandler : AuthorizationHandler<ValidApiKeyRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ValidApiKeyRequirement requirement)
{
throw new System.NotImplementedException();
}
}
}

@ -0,0 +1,9 @@
namespace nuget_host
{
public static class Constants
{
public const string AdministratorRoleName = "Admin";
public const string RequireAdminPolicyName = "RequireAdministratorRole";
public const string RequireValidApiKey = "RequireValideApiKey";
}
}

@ -6,9 +6,12 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using nuget_host.Data; using nuget_host.Data;
using nuget_host.Data.Roles;
using System; using System;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace nuget_host.Controllers namespace nuget_host.Controllers
@ -20,15 +23,18 @@ namespace nuget_host.Controllers
private readonly SignInManager<ApplicationUser> _signInManager; private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly AdminStartupList _startupAdminList;
public AccountController( public AccountController(
IAuthenticationSchemeProvider schemeProvider, IAuthenticationSchemeProvider schemeProvider,
SignInManager<ApplicationUser> signInManager, SignInManager<ApplicationUser> signInManager,
UserManager<ApplicationUser> userManager) UserManager<ApplicationUser> userManager,
IOptions<AdminStartupList> startupAdminListConfig )
{ {
_schemeProvider = schemeProvider; _schemeProvider = schemeProvider;
_signInManager = signInManager; _signInManager = signInManager;
_userManager = userManager; _userManager = userManager;
_startupAdminList = startupAdminListConfig.Value;
} }
/// <summary> /// <summary>
@ -232,5 +238,23 @@ namespace nuget_host.Controllers
return vm; return vm;
} }
[Authorize]
public async Task<IActionResult> GetAdminrole()
{
string username = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (_startupAdminList.Users.Contains(username))
{
var user = await _userManager.FindByNameAsync(username);
var roles = await _userManager.GetRolesAsync(user);
if (!roles.Contains(Constants.AdministratorRoleName))
{
await _userManager.AddToRoleAsync(user, Constants.AdministratorRoleName);
}
return Ok();
}
return BadRequest();
}
} }
} }

@ -0,0 +1,17 @@
using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using nuget_host.Data;
namespace nuget_host.Controllers
{
public class NewUpdateController : Controller
{
[Authorize(Policy = Constants.RequireAdminPolicyName)]
public IActionResult NewRelease(NewReleaseInfo version)
{
return View(version);
}
}
}

@ -0,0 +1,11 @@
using System;
namespace nuget_host.Data
{
public class NewReleaseInfo
{
public string Version { get; set; }
public string ChangeLog { get; set; }
public DateTime BuildDate { get; set; }
}
}

@ -0,0 +1,7 @@
namespace nuget_host.Data.Roles
{
public class AdminStartupList
{
public string [] Users { get; set;}
}
}

@ -1,24 +1,18 @@
using System; using Microsoft.AspNetCore.Builder;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity.UI.Services; using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using nuget_host.Data; using nuget_host.Data;
using nuget_host.Interfaces; using nuget_host.Interfaces;
using nuget_host.Services; using nuget_host.Services;
using nuget_host.Entities; using nuget_host.Entities;
using nuget_host.Data; using nuget_host.Authorization;
using System.Reflection; using nuget_host.Data.Roles;
using Microsoft.AspNetCore.Authorization;
namespace nuget_host namespace nuget_host
{ {
@ -40,6 +34,7 @@ namespace nuget_host
services.AddIdentity<ApplicationUser, IdentityRole>() services.AddIdentity<ApplicationUser, IdentityRole>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>() .AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager() .AddSignInManager()
.AddDefaultUI() .AddDefaultUI()
@ -48,14 +43,27 @@ namespace nuget_host
services.AddMvc(); services.AddMvc();
services.AddDataProtection(); services.AddDataProtection();
services.AddTransient<IMailer, EmailSender>(); services.AddTransient<IMailer, EmailSender>();
services.AddTransient<IEmailSender, EmailSender>(); services.AddTransient<IEmailSender, EmailSender>();
services.AddAuthorization(options =>
{
options.AddPolicy(Constants.RequireAdminPolicyName,
policy => policy.RequireRole(Constants.AdministratorRoleName));
options.AddPolicy(Constants.RequireValidApiKey, policy =>
policy.Requirements.Add(new ValidApiKeyRequirement()));
});
services.AddSingleton<IAuthorizationHandler, ValidApiKeyRequirementHandler>();
var smtpSettingsconf = Configuration.GetSection("Smtp"); var smtpSettingsconf = Configuration.GetSection("Smtp");
services.Configure<SmtpSettings>(smtpSettingsconf); services.Configure<SmtpSettings>(smtpSettingsconf);
var nugetSettingsconf = Configuration.GetSection("Nuget"); var nugetSettingsconf = Configuration.GetSection("Nuget");
services.Configure<NugetSettings>(nugetSettingsconf); services.Configure<NugetSettings>(nugetSettingsconf);
var adminStartupListConf = Configuration.GetSection("AdminList");
services.Configure<AdminStartupList>(adminStartupListConf);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

@ -1,4 +1,9 @@
{ {
"AdminStartupList": {
"Users": [
"paul@pschneider.fr"
]
},
"Nuget": { "Nuget": {
"PackagesRootDir" : "packages", "PackagesRootDir" : "packages",
"ProtectionTitle": "protected-data-v1", "ProtectionTitle": "protected-data-v1",

@ -1,4 +1,9 @@
{ {
"AdminStartupList": {
"Users": [
"happy-new-root"
]
},
"Nuget": { "Nuget": {
"PackagesRootDir" : "<your-Source-dir>", "PackagesRootDir" : "<your-Source-dir>",
"ProtectionTitle": "protected-data-v1", "ProtectionTitle": "protected-data-v1",

Loading…