Skip to content

Commit

Permalink
Inject Hasher class and register as singleton service
Browse files Browse the repository at this point in the history
The `Hasher` class is now being injected into the constructors of `CreateLinkBundle`, `DeleteLinkBundle`, `GetLinkBundlesForUser`, and `UpdateLinkBundle` classes, replacing the previous implementation where a new `Hasher` instance was created within these classes' methods. This change improves the testability and reusability of the `Hasher` class. The `Hasher` class is also registered as a singleton service in the `Program.cs` file, ensuring only one instance is used throughout the application's lifetime. It is initialized with `HASHER_KEY` and `HASHER_SALT` configuration values. The `Api` namespace, where `Hasher` is defined, is now being used in the `Program.cs` file. The creation of new `Hasher` instances has been removed from various places in the aforementioned classes due to the new injection approach.
  • Loading branch information
jongalloway committed Feb 20, 2024
1 parent 708d2d8 commit c31eb0f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Api/Functions/CreateLinkBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Api.Functions
{
public partial class CreateLinkBundle(CosmosClient cosmosClient)
public partial class CreateLinkBundle(CosmosClient cosmosClient, Hasher hasher)
{
protected const string CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789";
protected const string VANITY_REGEX = @"^([\w\d-])+(/([\w\d-])+)*$";
Expand Down Expand Up @@ -43,7 +43,6 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
if (clientPrincipal != null)
{
string username = clientPrincipal.UserDetails;
Hasher hasher = new();
linkBundle.UserId = hasher.HashString(username);
linkBundle.Provider = clientPrincipal.IdentityProvider;
}
Expand Down
3 changes: 1 addition & 2 deletions Api/Functions/DeleteLinkBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Api.Functions
{
public class DeleteLinkBundle(ILoggerFactory loggerFactory, CosmosClient cosmosClient)
public class DeleteLinkBundle(ILoggerFactory loggerFactory, CosmosClient cosmosClient, Hasher hasher)
{
private readonly ILogger _logger = loggerFactory.CreateLogger<DeleteLinkBundle>();

Expand All @@ -30,7 +30,6 @@ public async Task<HttpResponseData> Run(

if (result.Count != 0)
{
Hasher hasher = new();
var hashedUsername = hasher.HashString(principal.UserDetails);
if (hashedUsername != result.First().UserId || principal.IdentityProvider != result.First().Provider)
{
Expand Down
3 changes: 1 addition & 2 deletions Api/Functions/GetLinkBundlesForUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Api.Functions
{
public class GetLinkBundlesForUser(CosmosClient cosmosClient)
public class GetLinkBundlesForUser(CosmosClient cosmosClient, Hasher hasher)
{
[Function(nameof(GetLinkBundlesForUser))]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "user")] HttpRequestData req)
Expand All @@ -23,7 +23,6 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou

if (clientPrincipal != null)
{
Hasher hasher = new();
string username = hasher.HashString(clientPrincipal.UserDetails);
string provider = clientPrincipal.IdentityProvider;

Expand Down
3 changes: 1 addition & 2 deletions Api/Functions/UpdateLinkBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Api
{
public class UpdateLinkBundle(CosmosClient cosmosClient)
public class UpdateLinkBundle(CosmosClient cosmosClient, Hasher hasher)
{
[Function(nameof(UpdateLinkBundle))]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "links/{vanityUrl}")] HttpRequestData req,
Expand Down Expand Up @@ -45,7 +45,6 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou

if (result.Count != 0)
{
Hasher hasher = new();
var hashedUsername = hasher.HashString(principal.UserDetails);
if (hashedUsername != result.First().UserId || principal.IdentityProvider != result.First().Provider)
{
Expand Down
4 changes: 4 additions & 0 deletions Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Api;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -23,6 +24,9 @@
context.Configuration["CosmosDb:Endpoint"],
context.Configuration["CosmosDb:Key"],
cosmosClientOptions));
services.AddSingleton<Hasher>(services => new Hasher(
context.Configuration["HASHER_KEY"],
context.Configuration["HASHER_SALT"]));
})
.ConfigureFunctionsWorkerDefaults()
.Build();
Expand Down

0 comments on commit c31eb0f

Please sign in to comment.