From 519ec26826196aea2d69c31bb7813141f5c5d1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Silveira?= Date: Mon, 13 Nov 2023 14:42:42 +0100 Subject: [PATCH] * removed AuthenticationHeaderValue ctor from user credentials (and tests). --- src/EventStore.Client/UserCredentials.cs | 136 +++++++----------- .../UserCredentialsTests.cs | 34 ----- 2 files changed, 48 insertions(+), 122 deletions(-) diff --git a/src/EventStore.Client/UserCredentials.cs b/src/EventStore.Client/UserCredentials.cs index d67cbbf9d..d944d90d7 100644 --- a/src/EventStore.Client/UserCredentials.cs +++ b/src/EventStore.Client/UserCredentials.cs @@ -3,92 +3,52 @@ using static System.Convert; namespace EventStore.Client { - /// - /// Represents either a username/password pair or a JWT token used for authentication and - /// authorization to perform operations on the EventStoreDB. - /// - public class UserCredentials { - // ReSharper disable once InconsistentNaming - static readonly UTF8Encoding UTF8NoBom = new UTF8Encoding(false); - - /// - /// Constructs a new . - /// - public UserCredentials(string username, string password) { - Username = username; - Password = password; - - Authorization = new( - Constants.Headers.BasicScheme, - ToBase64String(UTF8NoBom.GetBytes($"{username}:{password}")) - ); - } - - /// - /// Constructs a new . - /// - public UserCredentials(string bearerToken) { - Authorization = new(Constants.Headers.BearerScheme, bearerToken); - } - - /// - /// Constructs a new . - /// - public UserCredentials(AuthenticationHeaderValue authorization) { - Authorization = authorization; - - if (authorization.Scheme != Constants.Headers.BasicScheme) - return; - - var (username, password) = DecodeBasicCredentials(Authorization); - - Username = username; - Password = password; - - return; - - static (string? Username, string? Password) DecodeBasicCredentials(AuthenticationHeaderValue value) { - if (value.Parameter is null) - return (null, null); - - var credentials = UTF8NoBom.GetString(FromBase64String(value.Parameter)).AsSpan(); - - var passwordStart = credentials.IndexOf(':') + 1; - var password = credentials[passwordStart..].ToString(); - - var usernameLength = credentials.Length - password.Length - 1; - var username = credentials[..usernameLength].ToString(); - - return (username, password); - - // var decoded = UTF8NoBom.GetString(FromBase64String(header.Parameter)); - // var parts = decoded.Split(':'); - // - // return parts.Length == 2 - // ? (parts[0], parts[1]) - // : (null, null); // should we throw? - } - } - - AuthenticationHeaderValue Authorization { get; } - - /// - /// The username - /// - public string? Username { get; } - - /// - /// The password - /// - public string? Password { get; } - - /// - public override string ToString() => Authorization.ToString(); - - /// - /// Implicitly convert a to a . - /// - - public static implicit operator string(UserCredentials self) => self.ToString(); - } + /// + /// Represents either a username/password pair or a JWT token used for authentication and + /// authorization to perform operations on the EventStoreDB. + /// + public class UserCredentials { + // ReSharper disable once InconsistentNaming + static readonly UTF8Encoding UTF8NoBom = new UTF8Encoding(false); + + /// + /// Constructs a new . + /// + public UserCredentials(string username, string password) { + Username = username; + Password = password; + + Authorization = new( + Constants.Headers.BasicScheme, + ToBase64String(UTF8NoBom.GetBytes($"{username}:{password}")) + ); + } + + /// + /// Constructs a new . + /// + public UserCredentials(string bearerToken) { + Authorization = new(Constants.Headers.BearerScheme, bearerToken); + } + + AuthenticationHeaderValue Authorization { get; } + + /// + /// The username + /// + public string? Username { get; } + + /// + /// The password + /// + public string? Password { get; } + + /// + public override string ToString() => Authorization.ToString(); + + /// + /// Implicitly convert a to a . + /// + public static implicit operator string(UserCredentials self) => self.ToString(); + } } \ No newline at end of file diff --git a/test/EventStore.Client.UserManagement.Tests/UserCredentialsTests.cs b/test/EventStore.Client.UserManagement.Tests/UserCredentialsTests.cs index 1b1458924..c8eb0a570 100644 --- a/test/EventStore.Client.UserManagement.Tests/UserCredentialsTests.cs +++ b/test/EventStore.Client.UserManagement.Tests/UserCredentialsTests.cs @@ -31,40 +31,6 @@ public void from_username_and_password() { credentials.ToString().ShouldBe(basicAuthInfo); } - [Theory] - [InlineData("madison", "itwill:befine")] - [InlineData("admin", "changeit")] - public void from_authentication_header_with_basic_scheme(string username, string password) { - var value = new AuthenticationHeaderValue( - Constants.Headers.BasicScheme, - EncodeCredentials(username, password) - ); - - var basicAuthInfo = value.ToString(); - - var credentials = new UserCredentials(value); - - credentials.Username.ShouldBe(username); - credentials.Password.ShouldBe(password); - credentials.ToString().ShouldBe(basicAuthInfo); - } - - [Fact] - public void from_authentication_header_with_bearer_scheme() { - var value = new AuthenticationHeaderValue( - Constants.Headers.BearerScheme, - JwtToken - ); - - var bearerToken = value.ToString(); - - var credentials = new UserCredentials(value); - - credentials.Username.ShouldBeNull(); - credentials.Password.ShouldBeNull(); - credentials.ToString().ShouldBe(bearerToken); - } - [Fact] public void from_bearer_token() { var credentials = new UserCredentials(JwtToken);