Skip to content

Commit 0fdc142

Browse files
Use problem details for login failure
1 parent f79aaa9 commit 0fdc142

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/Certify.Core/Management/Access/AccessControl.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public async Task<bool> AddResourcePolicy(string contextUserId, ResourcePolicy r
398398
return true;
399399
}
400400

401-
public async Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, SecurityPrinciplePasswordUpdate passwordUpdate)
401+
public async Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, SecurityPrinciplePasswordUpdate passwordUpdate, bool requirePasswordConfirmation = true)
402402
{
403403
if (passwordUpdate.SecurityPrincipleId != contextUserId && !await IsPrincipleInRole(contextUserId, contextUserId, StandardRoles.Administrator.Id))
404404
{
@@ -410,7 +410,7 @@ public async Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, Se
410410

411411
var principle = await GetSecurityPrinciple(contextUserId, passwordUpdate.SecurityPrincipleId, includePassword: true);
412412

413-
if (IsPasswordValid(passwordUpdate.Password, principle.Password))
413+
if (!requirePasswordConfirmation || (requirePasswordConfirmation && IsPasswordValid(passwordUpdate.Password, principle.Password)))
414414
{
415415
try
416416
{
@@ -419,10 +419,10 @@ public async Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, Se
419419
await _store.Update<SecurityPrinciple>(nameof(SecurityPrinciple), updateSp);
420420
updated = true;
421421
}
422-
catch
422+
catch (Exception exp)
423423
{
424-
await AuditWarning("User {contextUserId} attempted to use UpdateSecurityPrinciple password [{principleId}], but was not successful", contextUserId, principle?.Id);
425-
return false;
424+
await AuditError("User {contextUserId} attempted to use UpdateSecurityPrinciple password [{principleId}], but was not successful : {exp}", contextUserId, principle?.Id, exp);
425+
updated = false;
426426
}
427427
}
428428
else
@@ -453,7 +453,8 @@ public bool IsPasswordValid(string password, string currentHash)
453453
var components = currentHash.Split('.');
454454

455455
// hash provided password with same salt to compare result
456-
return currentHash == HashPassword(password, components[1]);
456+
var hashedPassword = HashPassword(password, components[1]);
457+
return currentHash == hashedPassword;
457458
}
458459

459460
/// <summary>

src/Certify.Models/Providers/IAccessControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IAccessControl
2424
Task<RoleStatus> GetSecurityPrincipleRoleStatus(string contextUserId, string id);
2525
Task<bool> UpdateSecurityPrinciple(string contextUserId, SecurityPrinciple principle);
2626
Task<bool> UpdateAssignedRoles(string contextUserId, SecurityPrincipleAssignedRoleUpdate update);
27-
Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, SecurityPrinciplePasswordUpdate passwordUpdate);
27+
Task<bool> UpdateSecurityPrinciplePassword(string contextUserId, SecurityPrinciplePasswordUpdate passwordUpdate, bool requirePasswordConfirmation = true);
2828
Task<SecurityPrincipleCheckResponse> CheckSecurityPrinciplePassword(string contextUserId, SecurityPrinciplePasswordCheck passwordCheck);
2929

3030
Task<bool> AddRole(string contextUserId, Role role, bool bypassIntegrityCheck = false);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ public async Task<IActionResult> Login(AuthRequest login)
8080
}
8181
else
8282
{
83-
return Unauthorized(new ProblemDetails { Status = StatusCodes.Status401Unauthorized, Title = "Invalid username or password" });
83+
//return Unauthorized("Invalid username or password");
84+
return Problem(
85+
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1",
86+
title: "Login Failed",
87+
detail: "Invalid username or password",
88+
statusCode: StatusCodes.Status401Unauthorized);
89+
8490
}
8591
}
8692

src/Certify.Server/Certify.Server.Hub.Api/Startup.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public void ConfigureServices(IServiceCollection services)
7272
});
7373

7474
services.AddRouting(r => r.LowercaseUrls = true);
75+
services.AddProblemDetails();
76+
7577

7678
services
7779
.AddSignalR(opt => opt.MaximumReceiveMessageSize = null)
@@ -285,7 +287,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
285287
endpoints.MapHub<InstanceManagementHub>("/api/internal/managementhub");
286288
});
287289

288-
#if DEBUG
290+
291+
// Converts unhandled exceptions into Problem Details responses
292+
app.UseExceptionHandler();
293+
294+
// Returns the Problem Details response for (empty) non-successful responses
295+
app.UseStatusCodePages();
296+
297+
#if DEBUG
289298
// Enable middleware to serve generated Swagger as a JSON endpoint.
290299
app.UseSwagger();
291300

0 commit comments

Comments
 (0)