Skip to content

Commit bdaa3fc

Browse files
Cleanup
1 parent 0e2b4c2 commit bdaa3fc

File tree

4 files changed

+36
-117
lines changed

4 files changed

+36
-117
lines changed

src/Certify.Server/Certify.Server.Hub.Api/Controllers/v1/CertificateController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ public async Task<IActionResult> UpdateManagedCertificateDetails(string instance
277277
/// <summary>
278278
/// Begin the managed certificate request/renewal process for the given managed certificate id (on demand)
279279
/// </summary>
280+
/// <param name="instanceId"></param>
280281
/// <param name="id"></param>
281282
/// <returns></returns>
282283
[HttpPost]

src/Certify.Server/Certify.Server.Hub.Api/Services/ManagementAPI.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public partial class ManagementAPI
2828
/// </summary>
2929
/// <param name="mgmtStateProvider">The instance management state provider.</param>
3030
/// <param name="mgmtHubContext">The management hub context for SignalR or Direct communication.</param>
31+
/// <param name="log"></param>
3132
public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubContext<InstanceManagementHub, IInstanceManagementHub> mgmtHubContext, ILogger<ManagementAPI> log)
3233
{
3334
_mgmtStateProvider = mgmtStateProvider;
@@ -41,6 +42,7 @@ public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubCon
4142
/// <param name="mgmtStateProvider">The instance management state provider.</param>
4243
/// <param name="mgmtHubContext">The management hub context for SignalR communication.</param>
4344
/// <param name="certifyManager">The in-process Certify manager instance.</param>
45+
/// <param name="log"></param>
4446
public ManagementAPI(IInstanceManagementStateProvider mgmtStateProvider, IHubContext<InstanceManagementHub, IInstanceManagementHub> mgmtHubContext, Certify.Management.ICertifyManager certifyManager, ILogger<ManagementAPI> log)
4547
{
4648
_mgmtStateProvider = mgmtStateProvider;
@@ -125,12 +127,12 @@ private async Task SendCommandWithNoResult(string instanceId, InstanceCommandReq
125127
/// <returns>The deserialized result as type <typeparamref name="T"/> if available; otherwise, default.</returns>
126128
private async Task<T?> PerformInstanceCommandTaskWithResult<T>(string instanceId, KeyValuePair<string, string>[] args, string commandType)
127129
{
128-
InstanceCommandResult result;
129-
var cmd = new InstanceCommandRequest(commandType, args);
130-
131-
cmd.IsResultAwaited = true;
130+
var cmd = new InstanceCommandRequest(commandType, args)
131+
{
132+
IsResultAwaited = true
133+
};
132134

133-
result = await GetCommandResult(instanceId, cmd);
135+
var result = await GetCommandResult(instanceId, cmd);
134136

135137
if (result?.Value != null)
136138
{
@@ -623,6 +625,12 @@ internal async Task<List<ActionStep>> PerformInstanceImport(string instanceId, I
623625
return await PerformInstanceCommandTaskWithResult<ICollection<Models.ActionStep>>(instanceId, args, ManagementHubCommands.GetSystemStatusItems);
624626
}
625627

628+
/// <summary>
629+
/// Retrieves the service configuration for the specified instance.
630+
/// </summary>
631+
/// <param name="instanceId">The target instance identifier.</param>
632+
/// <param name="currentAuthContext">The current authentication context.</param>
633+
/// <returns>A <see cref="ServiceConfig"/> object containing the service configuration, or null if not found.</returns>
626634
public async Task<ServiceConfig?> GetServiceConfig(string instanceId, AuthContext? currentAuthContext)
627635
{
628636
var args = new KeyValuePair<string, string>[] {
@@ -632,6 +640,12 @@ internal async Task<List<ActionStep>> PerformInstanceImport(string instanceId, I
632640
return await PerformInstanceCommandTaskWithResult<ServiceConfig>(instanceId, args, ManagementHubCommands.GetServiceConfig);
633641
}
634642

643+
/// <summary>
644+
/// Retrieves service core settings for a specified instance asynchronously.
645+
/// </summary>
646+
/// <param name="instanceId">Identifies the specific instance for which the settings are being retrieved.</param>
647+
/// <param name="currentAuthContext">Provides the authentication context necessary for accessing the service settings.</param>
648+
/// <returns>Returns the preferences associated with the service core settings or null if not found.</returns>
635649
public async Task<Preferences?> GetServiceCoreSettings(string instanceId, AuthContext? currentAuthContext)
636650
{
637651
var args = new KeyValuePair<string, string>[] {
@@ -641,6 +655,13 @@ internal async Task<List<ActionStep>> PerformInstanceImport(string instanceId, I
641655
return await PerformInstanceCommandTaskWithResult<Preferences>(instanceId, args, ManagementHubCommands.GetServiceCoreSettings);
642656
}
643657

658+
/// <summary>
659+
/// Updates the core settings of a service instance based on provided preferences and authentication context.
660+
/// </summary>
661+
/// <param name="instanceId">Identifies the specific service instance to be updated.</param>
662+
/// <param name="prefs">Contains the new preferences to apply to the service instance.</param>
663+
/// <param name="currentAuthContext">Holds the current authentication context for authorization during the update.</param>
664+
/// <returns>Returns the result of the update operation, which may indicate success or failure.</returns>
644665
public async Task<ActionResult?> UpdateServiceCoreSettings(string instanceId, Preferences prefs, AuthContext? currentAuthContext)
645666
{
646667
var args = new KeyValuePair<string, string>[] {
@@ -651,6 +672,13 @@ internal async Task<List<ActionStep>> PerformInstanceImport(string instanceId, I
651672
return await PerformInstanceCommandTaskWithResult<ActionResult?>(instanceId, args, ManagementHubCommands.UpdateServiceCoreSettings);
652673
}
653674

675+
/// <summary>
676+
/// Updates the configuration of a specified service instance asynchronously.
677+
/// </summary>
678+
/// <param name="instanceId">Identifies the specific service instance to be updated.</param>
679+
/// <param name="config">Contains the new configuration settings for the service instance.</param>
680+
/// <param name="currentAuthContext">Represents the current authentication context for authorization checks.</param>
681+
/// <returns>Returns the result of the update operation, which may indicate success or failure.</returns>
654682
public async Task<ActionResult?> UpdateServiceConfig(string instanceId, ServiceConfig config, AuthContext? currentAuthContext)
655683
{
656684
var args = new KeyValuePair<string, string>[] {

src/Certify.Service/Controllers/ControllerBase.cs

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.Linq;
54
using System.Security.Claims;
65
using System.Security.Principal;
7-
using System.Threading.Tasks;
86
using System.Web.Http;
97
using System.Web.Http.Controllers;
108
using Microsoft.IdentityModel.JsonWebTokens;
@@ -27,30 +25,8 @@ protected override bool IsAuthorized(HttpActionContext actionContext)
2725
var request = actionContext.Request;
2826
var authorization = request.Headers.Authorization;
2927

30-
#if DEBUG // feature not in production
31-
if (authorization != null && authorization.Scheme == "Bearer")
32-
{
33-
//bearer token presented, validate principle
34-
var token = authorization.Parameter;
35-
36-
var secretKey = ((ControllerBase)actionContext.ControllerContext.Controller).GetAuthSecretKey();
37-
38-
var principal = AuthenticateJwtToken(token, secretKey).Result;
39-
40-
if (principal == null)
41-
{
42-
//invalid token
43-
return false;
44-
}
45-
else
46-
{
47-
actionContext.RequestContext.Principal = principal;
48-
return true;
49-
}
50-
}
51-
#endif
52-
5328
var user = actionContext.RequestContext.Principal as WindowsPrincipal;
29+
5430
if (user.IsInRole(WindowsBuiltInRole.Administrator))
5531
{
5632
return true;
@@ -63,93 +39,6 @@ protected override bool IsAuthorized(HttpActionContext actionContext)
6339

6440
return false;
6541
}
66-
67-
public static ClaimsIdentity GetClaimsIdentity(string token, string secret)
68-
{
69-
// adapted form https://stackoverflow.com/questions/40281050/jwt-authentication-for-asp-net-web-api
70-
try
71-
{
72-
var tokenHandler = new JsonWebTokenHandler();
73-
var jwtToken = tokenHandler.ReadToken(token) as JsonWebToken;
74-
75-
if (jwtToken == null)
76-
{
77-
return null;
78-
}
79-
80-
var symmetricKey = Convert.FromBase64String(secret);
81-
82-
var validationParameters = new TokenValidationParameters()
83-
{
84-
RequireExpirationTime = true,
85-
ValidateIssuer = false,
86-
ValidateAudience = false,
87-
IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
88-
};
89-
90-
var result = tokenHandler.ValidateToken(token, validationParameters);
91-
92-
if (result.IsValid)
93-
{
94-
return result.ClaimsIdentity;
95-
}
96-
else
97-
{
98-
return null;
99-
}
100-
}
101-
catch (Exception)
102-
{
103-
//should write log
104-
return null;
105-
}
106-
}
107-
108-
private static bool ValidateToken(string token, string secret, out string username)
109-
{
110-
username = null;
111-
112-
var identity = GetClaimsIdentity(token, secret);
113-
114-
if (identity == null || !identity.IsAuthenticated)
115-
{
116-
return false;
117-
}
118-
119-
var usernameClaim = identity.FindFirst(ClaimTypes.Name);
120-
username = usernameClaim?.Value;
121-
122-
if (string.IsNullOrEmpty(username))
123-
{
124-
return false;
125-
}
126-
127-
// More validation to check whether username exists in system etc
128-
129-
return true;
130-
}
131-
132-
protected Task<IPrincipal> AuthenticateJwtToken(string token, string secret)
133-
{
134-
135-
if (ValidateToken(token, secret, out var username))
136-
{
137-
// based on username to get more information from database
138-
// in order to build local identity
139-
var claims = new List<Claim>
140-
{
141-
new Claim(ClaimTypes.Name, username)
142-
// Add more claims if needed: Roles, ...
143-
};
144-
145-
var identity = new ClaimsIdentity(claims, "Jwt");
146-
IPrincipal user = new ClaimsPrincipal(identity);
147-
148-
return Task.FromResult(user);
149-
}
150-
151-
return Task.FromResult<IPrincipal>(null);
152-
}
15342
}
15443

15544
[CustomAuthCheck]

src/Certify.Tests/Certify.Core.Tests.Unit/Tests/CertifyServiceTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Net;
44
using System.Net.Http;
5+
using System.Net.Http.Json;
56
using System.Text.RegularExpressions;
67
using System.Threading.Tasks;
78
using Certify.Models;

0 commit comments

Comments
 (0)