ci & admin & auth & cli
This commit is contained in:
@ -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,11 +7,12 @@ 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 id { get; set; }
|
public string message { get; set; }
|
||||||
}
|
public string id { get; set; }
|
||||||
|
}
|
||||||
public class UploadFilesToServerUsingWebRequest
|
public class UploadFilesToServerUsingWebRequest
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ namespace nuget_cli
|
|||||||
httpWebRequest.AllowAutoRedirect = false;
|
httpWebRequest.AllowAutoRedirect = false;
|
||||||
httpWebRequest.Headers.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
httpWebRequest.Headers.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||||
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
httpWebRequest.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||||
httpWebRequest.ContentLength = boundarybytes.Length +
|
httpWebRequest.ContentLength = boundarybytes.Length +
|
||||||
fileheaderbytes.Length + fi.Length + endBoundaryBytes.Length;
|
fileheaderbytes.Length + fi.Length + endBoundaryBytes.Length;
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +69,7 @@ namespace nuget_cli
|
|||||||
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
|
||||||
requestStream.Close();
|
requestStream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception rex)
|
catch (Exception rex)
|
||||||
{
|
{
|
||||||
@ -82,24 +83,24 @@ namespace nuget_cli
|
|||||||
}, httpWebRequest);
|
}, httpWebRequest);
|
||||||
|
|
||||||
WebResponse resp = httpWebRequest.GetResponse();
|
WebResponse resp = httpWebRequest.GetResponse();
|
||||||
|
|
||||||
Stream stream = resp.GetResponseStream();
|
Stream stream = resp.GetResponseStream();
|
||||||
StreamReader re = new StreamReader(stream);
|
StreamReader re = new StreamReader(stream);
|
||||||
if (resp is HttpWebResponse)
|
if (resp is HttpWebResponse)
|
||||||
{
|
{
|
||||||
String json = re.ReadToEnd();
|
String json = re.ReadToEnd();
|
||||||
report.Message = json;
|
report.Message = json;
|
||||||
|
|
||||||
var res = JsonConvert.DeserializeObject<nugetdresp>(json);
|
var res = JsonConvert.DeserializeObject<nugetdresp>(json);
|
||||||
report.AlreadyPresent = res.ecode == 1;
|
report.AlreadyPresent = res.ecode == 1;
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
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.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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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 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,7 +34,8 @@ namespace nuget_host
|
|||||||
|
|
||||||
|
|
||||||
services.AddIdentity<ApplicationUser, IdentityRole>()
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
.AddRoles<IdentityRole>()
|
||||||
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
.AddSignInManager()
|
.AddSignInManager()
|
||||||
.AddDefaultUI()
|
.AddDefaultUI()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
@ -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",
|
||||||
|
Reference in New Issue
Block a user