ci & admin & auth & cli
This commit is contained in:
@ -7,8 +7,7 @@ image: busybox:latest
|
||||
before_script:
|
||||
- dotnet restore
|
||||
|
||||
after_script:
|
||||
- dotnet nuget remove source gitlab
|
||||
#after_script:
|
||||
|
||||
nonreg:
|
||||
stage: test
|
||||
|
@ -7,7 +7,8 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace nuget_cli
|
||||
{
|
||||
public class nugetdresp {
|
||||
public class nugetdresp
|
||||
{
|
||||
public int ecode { get; set; }
|
||||
public string message { get; set; }
|
||||
public string id { get; set; }
|
||||
@ -96,10 +97,10 @@ namespace nuget_cli
|
||||
var hrep = resp as HttpWebResponse;
|
||||
report.StatusCode = hrep.StatusCode.ToString();
|
||||
// ecode == 1 => package already present server side.
|
||||
report.OK = hrep.StatusCode ==
|
||||
HttpStatusCode.Accepted
|
||||
report.AlreadyPresent = res.ecode == 1;
|
||||
report.OK = hrep.StatusCode == HttpStatusCode.Accepted
|
||||
|| hrep.StatusCode == HttpStatusCode.OK
|
||||
|| res.ecode == 1;
|
||||
|| report.AlreadyPresent;
|
||||
}
|
||||
else throw new Exception("Invalid server response type");
|
||||
}
|
||||
|
10
src/nuget-host/Authorisation/ValidApiKeyRequirement.cs
Normal file
10
src/nuget-host/Authorisation/ValidApiKeyRequirement.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
9
src/nuget-host/Constants.cs
Normal file
9
src/nuget-host/Constants.cs
Normal file
@ -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.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using nuget_host.Data;
|
||||
using nuget_host.Data.Roles;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace nuget_host.Controllers
|
||||
@ -20,15 +23,18 @@ namespace nuget_host.Controllers
|
||||
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly AdminStartupList _startupAdminList;
|
||||
|
||||
public AccountController(
|
||||
IAuthenticationSchemeProvider schemeProvider,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
UserManager<ApplicationUser> userManager)
|
||||
UserManager<ApplicationUser> userManager,
|
||||
IOptions<AdminStartupList> startupAdminListConfig )
|
||||
{
|
||||
_schemeProvider = schemeProvider;
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
_startupAdminList = startupAdminListConfig.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -232,5 +238,23 @@ namespace nuget_host.Controllers
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
src/nuget-host/Controllers/NewUpdateController.cs
Normal file
17
src/nuget-host/Controllers/NewUpdateController.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
11
src/nuget-host/Data/NewReleaseInfo.cs
Normal file
11
src/nuget-host/Data/NewReleaseInfo.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
7
src/nuget-host/Data/Roles/Administrator.cs
Normal file
7
src/nuget-host/Data/Roles/Administrator.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace nuget_host.Data.Roles
|
||||
{
|
||||
public class AdminStartupList
|
||||
{
|
||||
public string [] Users { get; set;}
|
||||
}
|
||||
}
|
@ -1,24 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.UI;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Identity.UI.Services;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using nuget_host.Data;
|
||||
using nuget_host.Interfaces;
|
||||
using nuget_host.Services;
|
||||
using nuget_host.Entities;
|
||||
using nuget_host.Data;
|
||||
using System.Reflection;
|
||||
using nuget_host.Authorization;
|
||||
using nuget_host.Data.Roles;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace nuget_host
|
||||
{
|
||||
@ -40,6 +34,7 @@ namespace nuget_host
|
||||
|
||||
|
||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||
.AddRoles<IdentityRole>()
|
||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||
.AddSignInManager()
|
||||
.AddDefaultUI()
|
||||
@ -48,14 +43,27 @@ namespace nuget_host
|
||||
services.AddMvc();
|
||||
|
||||
services.AddDataProtection();
|
||||
|
||||
services.AddTransient<IMailer, 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");
|
||||
services.Configure<SmtpSettings>(smtpSettingsconf);
|
||||
var nugetSettingsconf = Configuration.GetSection("Nuget");
|
||||
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.
|
||||
|
@ -1,4 +1,9 @@
|
||||
{
|
||||
"AdminStartupList": {
|
||||
"Users": [
|
||||
"paul@pschneider.fr"
|
||||
]
|
||||
},
|
||||
"Nuget": {
|
||||
"PackagesRootDir" : "packages",
|
||||
"ProtectionTitle": "protected-data-v1",
|
||||
|
@ -1,4 +1,9 @@
|
||||
{
|
||||
"AdminStartupList": {
|
||||
"Users": [
|
||||
"happy-new-root"
|
||||
]
|
||||
},
|
||||
"Nuget": {
|
||||
"PackagesRootDir" : "<your-Source-dir>",
|
||||
"ProtectionTitle": "protected-data-v1",
|
||||
|
Reference in New Issue
Block a user