Skip to content

Commit

Permalink
Adjust with review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 6, 2024
1 parent bc1d217 commit 84db02f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
12 changes: 1 addition & 11 deletions Modules/Authentication/Basic/BasicAuthenticationUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

namespace GenHTTP.Modules.Authentication.Basic;

public record BasicAuthenticationUser : IUser
public record BasicAuthenticationUser(string Name, params string[] Roles) : IUser
{

public string Name { get; }

public string DisplayName => Name;

public string[] Roles { get; }

public BasicAuthenticationUser(string name, params string[] roles)
{
Name = name;
Roles = roles;
}

}
7 changes: 3 additions & 4 deletions Modules/Authentication/RequireRoleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ namespace GenHTTP.Modules.Authentication;
/// When annotated on a service method, requests will only be allowed
/// if the authenticated user has the specified roles.
/// </summary>
/// <param name="roles"></param>
/// <param name="roles">The roles which need to be present in order to let the request pass</param>
[AttributeUsage(AttributeTargets.Method)]
public class RequireRoleAttribute(params string[] roles) : InterceptWithAttribute<RoleInterceptor>
{

/// <summary>
/// The roles which need to be present in order to let
/// the request pass.
/// The roles which need to be present in order to let the request pass.
/// </summary>
public string[] Roles { get; } = roles;
public string[] Roles => roles;

}
4 changes: 2 additions & 2 deletions Modules/Authentication/Roles/RoleInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Configure(object attribute)

if (user == null)
{
throw new ProviderException(ResponseStatus.Unauthorized, "Credentials are required for this endpoint");
throw new ProviderException(ResponseStatus.Unauthorized, "Authorization required to access this endpoint");
}

var userRoles = user.Roles;
Expand All @@ -51,7 +51,7 @@ public void Configure(object attribute)

if (missing.Count > 0)
{
throw new ProviderException(ResponseStatus.Forbidden, $"User lacks the following roles to access this endpoint: {string.Join(", ", missing)}");
throw new ProviderException(ResponseStatus.Forbidden, $"User is not authorized to access this endpoint.");
}
}

Expand Down
2 changes: 0 additions & 2 deletions Testing/Acceptance/Modules/Authentication/RoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public async Task TestInsufficientRoles(TestEngine engine)
using var response = await RunAsync(new RoleUser(["ADMIN"]), engine);

await response.AssertStatusAsync(HttpStatusCode.Forbidden);

AssertX.Contains("SUPER_ADMIN", await response.GetContentAsync());
}

[TestMethod]
Expand Down

0 comments on commit 84db02f

Please sign in to comment.