using AutoMapper; using Bit.Core.Auth.Repositories; using Bit.Infrastructure.EntityFramework.Auth.Models; using Bit.Infrastructure.EntityFramework.Repositories; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Bit.Infrastructure.EntityFramework.Auth.Repositories; public class WebAuthnCredentialRepository : Repository, IWebAuthnCredentialRepository { public WebAuthnCredentialRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(serviceScopeFactory, mapper, (context) => context.WebAuthnCredentials) { } public async Task GetByIdAsync(Guid id, Guid userId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = dbContext.WebAuthnCredentials.Where(d => d.Id == id && d.UserId == userId); var cred = await query.FirstOrDefaultAsync(); return Mapper.Map(cred); } } public async Task> GetManyByUserIdAsync(Guid userId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = dbContext.WebAuthnCredentials.Where(d => d.UserId == userId); var creds = await query.ToListAsync(); return Mapper.Map>(creds); } } public async Task UpdateAsync(Core.Auth.Entities.WebAuthnCredential credential) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var cred = await dbContext.WebAuthnCredentials .FirstOrDefaultAsync(d => d.Id == credential.Id && d.UserId == credential.UserId); if (cred == null) { return false; } cred.EncryptedPrivateKey = credential.EncryptedPrivateKey; cred.EncryptedPublicKey = credential.EncryptedPublicKey; cred.EncryptedUserKey = credential.EncryptedUserKey; await dbContext.SaveChangesAsync(); return true; } } }