Migrate from MongoDB to Couchbase Lite for local storage; update related services and configurations
Some checks failed
Publish Container / publish (push) Failing after 3m43s
Some checks failed
Publish Container / publish (push) Failing after 3m43s
This commit is contained in:
parent
374163bf11
commit
f976d70db8
24 changed files with 328 additions and 218 deletions
|
|
@ -1,20 +1,12 @@
|
|||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace WorkTracker.Services.Auth;
|
||||
|
||||
public sealed class MongoAuthUser
|
||||
public sealed class AuthUser
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string Id { get; init; } = string.Empty;
|
||||
|
||||
[BsonElement("email")]
|
||||
public string Email { get; init; } = string.Empty;
|
||||
|
||||
[BsonElement("emailNormalized")]
|
||||
public string EmailNormalized { get; init; } = string.Empty;
|
||||
|
||||
[BsonElement("passwordHash")]
|
||||
public string PasswordHash { get; init; } = string.Empty;
|
||||
}
|
||||
}
|
||||
102
Services/Auth/CouchbaseLiteAuthService.cs
Normal file
102
Services/Auth/CouchbaseLiteAuthService.cs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
using Couchbase.Lite;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using WorkTracker.Configuration;
|
||||
using WorkTracker.Services.Storage;
|
||||
|
||||
namespace WorkTracker.Services.Auth;
|
||||
|
||||
public sealed class CouchbaseLiteAuthService : IAuthService
|
||||
{
|
||||
private readonly Collection users;
|
||||
private readonly PasswordHasher<AuthUser> passwordHasher = new();
|
||||
private readonly IOptions<SingleUserOptions> options;
|
||||
private readonly ILogger<CouchbaseLiteAuthService> logger;
|
||||
|
||||
public CouchbaseLiteAuthService(
|
||||
CouchbaseLiteDatabaseProvider databaseProvider,
|
||||
IOptions<SingleUserOptions> options,
|
||||
ILogger<CouchbaseLiteAuthService> logger)
|
||||
{
|
||||
users = databaseProvider.Users;
|
||||
this.options = options;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public Task EnsureSeedUserAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var email = options.Value.Email.Trim();
|
||||
var normalizedEmail = NormalizeEmail(email);
|
||||
|
||||
if (users.GetDocument(normalizedEmail) is not null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var user = new AuthUser
|
||||
{
|
||||
Id = normalizedEmail,
|
||||
Email = email,
|
||||
EmailNormalized = normalizedEmail,
|
||||
PasswordHash = string.Empty
|
||||
};
|
||||
|
||||
var passwordHash = passwordHasher.HashPassword(user, options.Value.Password);
|
||||
var userToCreate = new AuthUser
|
||||
{
|
||||
Id = normalizedEmail,
|
||||
Email = email,
|
||||
EmailNormalized = normalizedEmail,
|
||||
PasswordHash = passwordHash
|
||||
};
|
||||
|
||||
SaveUser(userToCreate);
|
||||
logger.LogInformation("Seeded single user account {Email} in Couchbase Lite", email);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<AuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var user = GetUser(NormalizeEmail(email));
|
||||
if (user is null)
|
||||
{
|
||||
return Task.FromResult<AuthUser?>(null);
|
||||
}
|
||||
|
||||
var result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
|
||||
return Task.FromResult(result == PasswordVerificationResult.Failed ? null : user);
|
||||
}
|
||||
|
||||
private AuthUser? GetUser(string normalizedEmail)
|
||||
{
|
||||
var document = users.GetDocument(normalizedEmail);
|
||||
if (document is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new AuthUser
|
||||
{
|
||||
Id = document.Id,
|
||||
Email = document.GetString("email") ?? string.Empty,
|
||||
EmailNormalized = document.GetString("emailNormalized") ?? string.Empty,
|
||||
PasswordHash = document.GetString("passwordHash") ?? string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
private void SaveUser(AuthUser user)
|
||||
{
|
||||
var document = new MutableDocument(user.Id);
|
||||
document.SetString("email", user.Email);
|
||||
document.SetString("emailNormalized", user.EmailNormalized);
|
||||
document.SetString("passwordHash", user.PasswordHash);
|
||||
|
||||
users.Save(document);
|
||||
}
|
||||
|
||||
private static string NormalizeEmail(string email) => email.Trim().ToUpperInvariant();
|
||||
}
|
||||
8
Services/Auth/IAuthService.cs
Normal file
8
Services/Auth/IAuthService.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace WorkTracker.Services.Auth;
|
||||
|
||||
public interface IAuthService
|
||||
{
|
||||
Task EnsureSeedUserAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<AuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken);
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace WorkTracker.Services.Auth;
|
||||
|
||||
public interface IMongoAuthService
|
||||
{
|
||||
Task EnsureSeedUserAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task<MongoAuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken);
|
||||
}
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Driver;
|
||||
using WorkTracker.Configuration;
|
||||
|
||||
namespace WorkTracker.Services.Auth;
|
||||
|
||||
public sealed class MongoAuthService : IMongoAuthService
|
||||
{
|
||||
private const string UsersCollectionName = "users";
|
||||
|
||||
private readonly IMongoCollection<MongoAuthUser> users;
|
||||
private readonly PasswordHasher<MongoAuthUser> passwordHasher = new();
|
||||
private readonly IOptions<SingleUserOptions> options;
|
||||
private readonly ILogger<MongoAuthService> logger;
|
||||
|
||||
public MongoAuthService(
|
||||
IMongoDatabase database,
|
||||
IOptions<SingleUserOptions> options,
|
||||
ILogger<MongoAuthService> logger)
|
||||
{
|
||||
users = database.GetCollection<MongoAuthUser>(UsersCollectionName);
|
||||
this.options = options;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task EnsureSeedUserAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var email = options.Value.Email.Trim();
|
||||
var normalizedEmail = NormalizeEmail(email);
|
||||
|
||||
var existingUser = await users
|
||||
.Find(x => x.EmailNormalized == normalizedEmail)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (existingUser is not null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var user = new MongoAuthUser
|
||||
{
|
||||
Email = email,
|
||||
EmailNormalized = normalizedEmail,
|
||||
PasswordHash = string.Empty
|
||||
};
|
||||
|
||||
var passwordHash = passwordHasher.HashPassword(user, options.Value.Password);
|
||||
var userToCreate = new MongoAuthUser
|
||||
{
|
||||
Email = email,
|
||||
EmailNormalized = normalizedEmail,
|
||||
PasswordHash = passwordHash
|
||||
};
|
||||
|
||||
await users.InsertOneAsync(userToCreate, cancellationToken: cancellationToken);
|
||||
logger.LogInformation("Seeded single user account {Email} in MongoDB", email);
|
||||
}
|
||||
|
||||
public async Task<MongoAuthUser?> ValidateCredentialsAsync(string email, string password, CancellationToken cancellationToken)
|
||||
{
|
||||
var normalizedEmail = NormalizeEmail(email);
|
||||
var user = await users
|
||||
.Find(x => x.EmailNormalized == normalizedEmail)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
|
||||
return result == PasswordVerificationResult.Failed ? null : user;
|
||||
}
|
||||
|
||||
private static string NormalizeEmail(string email) => email.Trim().ToUpperInvariant();
|
||||
}
|
||||
|
|
@ -5,12 +5,12 @@ namespace WorkTracker.Services.Auth;
|
|||
|
||||
public sealed class SingleUserSeedService : IHostedService
|
||||
{
|
||||
private readonly IMongoAuthService authService;
|
||||
private readonly IAuthService authService;
|
||||
private readonly IOptions<SingleUserOptions> options;
|
||||
private readonly ILogger<SingleUserSeedService> logger;
|
||||
|
||||
public SingleUserSeedService(
|
||||
IMongoAuthService authService,
|
||||
IAuthService authService,
|
||||
IOptions<SingleUserOptions> options,
|
||||
ILogger<SingleUserSeedService> logger)
|
||||
{
|
||||
|
|
@ -32,7 +32,7 @@ public sealed class SingleUserSeedService : IHostedService
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Unable to seed MongoDB single user account {Email}", options.Value.Email);
|
||||
logger.LogError(ex, "Unable to seed Couchbase Lite single user account {Email}", options.Value.Email);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue