1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-22 21:51:22 +01:00

u2f db updates

This commit is contained in:
Kyle Spearrin 2018-07-21 08:44:21 -04:00
parent f7fb99a726
commit 941792bdd8
6 changed files with 83 additions and 3 deletions

View File

@ -10,6 +10,7 @@ using System.Linq;
using U2fLib = U2F.Core.Crypto.U2F;
using U2F.Core.Models;
using U2F.Core.Exceptions;
using System;
namespace Bit.Core.Identity
{
@ -80,7 +81,8 @@ namespace Bit.Core.Identity
Challenge = auth.Challenge,
KeyHandle = auth.KeyHandle,
Version = auth.Version,
UserId = user.Id
UserId = user.Id,
CreationDate = DateTime.UtcNow
});
challenges.Add(new

View File

@ -281,13 +281,14 @@ namespace Bit.Core.Services
public async Task<U2fRegistration> StartU2fRegistrationAsync(User user)
{
await _u2fRepository.DeleteManyByUserIdAsync(user.Id);
var reg = U2fLib.StartRegistration(Utilities.CoreHelpers.U2fAppIdUrl(_globalSettings));
var reg = U2fLib.StartRegistration(CoreHelpers.U2fAppIdUrl(_globalSettings));
await _u2fRepository.CreateAsync(new U2f
{
AppId = reg.AppId,
Challenge = reg.Challenge,
Version = reg.Version,
UserId = user.Id
UserId = user.Id,
CreationDate = DateTime.UtcNow
});
return new U2fRegistration

View File

@ -229,5 +229,6 @@
<Build Include="dbo\Stored Procedures\User_ReadByPremiumRenewal.sql" />
<Build Include="dbo\Stored Procedures\User_UpdateRenewalReminderDate.sql" />
<Build Include="dbo\Stored Procedures\Grant_DeleteExpired.sql" />
<Build Include="dbo\Stored Procedures\U2f_DeleteOld.sql" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,19 @@
CREATE PROCEDURE [dbo].[U2f_DeleteOld]
AS
BEGIN
SET NOCOUNT ON
DECLARE @BatchSize INT = 100
DECLARE @Threshold DATETIME2(7) = DATEADD (day, -7, GETUTCDATE())
WHILE @BatchSize > 0
BEGIN
DELETE TOP(@BatchSize)
FROM
[dbo].[U2f]
WHERE
[CreationDate] < @Threshold
SET @BatchSize = @@ROWCOUNT
END
END

View File

@ -10,3 +10,13 @@
CONSTRAINT [FK_U2f_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_U2f_CreationDate]
ON [dbo].[U2f]([CreationDate] ASC)
GO
CREATE NONCLUSTERED INDEX [IX_U2f_UserId]
ON [dbo].[U2f]([UserId] ASC);

View File

@ -27,6 +27,26 @@ BEGIN
END
GO
IF NOT EXISTS (
SELECT * FROM sys.indexes WHERE [Name]='IX_U2f_CreationDate'
AND object_id = OBJECT_ID('[dbo].[U2f]')
)
BEGIN
CREATE NONCLUSTERED INDEX [IX_U2f_CreationDate]
ON [dbo].[U2f]([CreationDate] ASC)
END
GO
IF NOT EXISTS (
SELECT * FROM sys.indexes WHERE [Name]='IX_U2f_UserId'
AND object_id = OBJECT_ID('[dbo].[U2f]')
)
BEGIN
CREATE NONCLUSTERED INDEX [IX_U2f_UserId]
ON [dbo].[U2f]([UserId] ASC)
END
GO
IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'UserView')
BEGIN
DROP VIEW [dbo].[UserView]
@ -41,6 +61,33 @@ FROM
[dbo].[User]
GO
IF OBJECT_ID('[dbo].[U2f_DeleteOld]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[U2f_DeleteOld]
END
GO
CREATE PROCEDURE [dbo].[U2f_DeleteOld]
AS
BEGIN
SET NOCOUNT ON
DECLARE @BatchSize INT = 100
DECLARE @Threshold DATETIME2(7) = DATEADD (day, -7, GETUTCDATE())
WHILE @BatchSize > 0
BEGIN
DELETE TOP(@BatchSize)
FROM
[dbo].[U2f]
WHERE
[CreationDate] < @Threshold
SET @BatchSize = @@ROWCOUNT
END
END
GO
IF OBJECT_ID('[dbo].[Grant_DeleteExpired]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Grant_DeleteExpired]