From 96a822937d6180d1e47eb41cf68fc6f11eb7693e Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Thu, 16 Oct 2025 16:04:58 +0200 Subject: [PATCH] Make Roles optional in ApiKey and Credential classes Updated the `ApiKey` and `Credential` record classes to make the `Roles` parameter optional by providing a default value of `null`. This change allows for the creation of instances without explicitly specifying roles. Overrode the `Equals` method in both classes to provide custom equality comparisons. Refactored the `GetHashCode` methods to use expression-bodied members for simplicity. --- src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs | 7 ++----- .../BasicAuthentication/Credential.cs | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs b/src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs index 0cc38e4..915bcbb 100644 --- a/src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs +++ b/src/SimpleAuthentication.Abstractions/ApiKey/ApiKey.cs @@ -6,7 +6,7 @@ namespace SimpleAuthentication.ApiKey; /// The API key value /// The user name associated with the current key /// The list of roles to assign to the user -public record class ApiKey(string Value, string UserName, IEnumerable Roles) +public record class ApiKey(string Value, string UserName, IEnumerable? Roles = null) { /// public virtual bool Equals(ApiKey? other) @@ -25,8 +25,5 @@ public virtual bool Equals(ApiKey? other) } /// - public override int GetHashCode() - { - return HashCode.Combine(Value, UserName); - } + public override int GetHashCode() => HashCode.Combine(Value, UserName); } diff --git a/src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs b/src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs index 8b1471e..93d8331 100644 --- a/src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs +++ b/src/SimpleAuthentication.Abstractions/BasicAuthentication/Credential.cs @@ -6,7 +6,7 @@ namespace SimpleAuthentication.BasicAuthentication; /// The user name /// The password /// The list of roles to assign to the user -public record class Credential(string UserName, string Password, IEnumerable Roles) +public record class Credential(string UserName, string Password, IEnumerable? Roles = null) { /// public virtual bool Equals(Credential? other) @@ -25,8 +25,5 @@ public virtual bool Equals(Credential? other) } /// - public override int GetHashCode() - { - return HashCode.Combine(UserName, Password); - } + public override int GetHashCode() => HashCode.Combine(UserName, Password); }