1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-28 17:57:37 +01:00

add support for storing u2f challenges

This commit is contained in:
Kyle Spearrin 2017-06-21 16:55:45 -04:00
parent 50c0b3e752
commit 0c84f9c151
9 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,20 @@
using System;
namespace Bit.Core.Models.Table
{
public class U2f : IDataObject<int>
{
public int Id { get; set; }
public Guid UserId { get; set; }
public string KeyHandle { get; set; }
public string Challenge { get; set; }
public string AppId { get; set; }
public string Version { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public void SetNewId()
{
// do nothing since it is an identity
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
namespace Bit.Core.Repositories
{
public interface IU2fRepository : IRepository<U2f, int>
{
Task<ICollection<U2f>> GetManyByUserIdAsync(Guid userId);
Task DeleteManyByUserIdAsync(Guid userId);
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using System.Data;
using Dapper;
namespace Bit.Core.Repositories.SqlServer
{
public class U2fRepository : Repository<U2f, int>, IU2fRepository
{
public U2fRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString)
{ }
public U2fRepository(string connectionString)
: base(connectionString)
{ }
public async Task<ICollection<U2f>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<U2f>(
$"[{Schema}].[U2f_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task DeleteManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
$"[{Schema}].[U2f_DeleteByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
}
}
public override Task<U2f> GetByIdAsync(int id)
{
throw new NotSupportedException();
}
public override Task ReplaceAsync(U2f obj)
{
throw new NotSupportedException();
}
public override Task UpsertAsync(U2f obj)
{
throw new NotSupportedException();
}
public override Task DeleteAsync(U2f obj)
{
throw new NotSupportedException();
}
}
}

View File

@ -195,5 +195,10 @@
<Build Include="dbo\Stored Procedures\Cipher_Delete.sql" />
<Build Include="dbo\Functions\UserCipherDetails.sql" />
<Build Include="dbo\Stored Procedures\Cipher_Move.sql" />
<Build Include="dbo\Tables\U2f.sql" />
<Build Include="dbo\Stored Procedures\U2f_Create.sql" />
<Build Include="dbo\Stored Procedures\U2f_DeleteByUserId.sql" />
<Build Include="dbo\Stored Procedures\U2f_ReadByUserId.sql" />
<Build Include="dbo\Views\U2fView.sql" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,33 @@
CREATE PROCEDURE [dbo].[U2f_Create]
@Id INT,
@UserId UNIQUEIDENTIFIER,
@KeyHandle VARCHAR(50),
@Challenge VARCHAR(50),
@AppId VARCHAR(50),
@Version VARCHAR(50),
@CreationDate DATETIME2(7)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO [dbo].[U2f]
(
[Id],
[UserId],
[KeyHandle],
[Challenge],
[AppId],
[Version],
[CreationDate]
)
VALUES
(
@Id,
@UserId,
@KeyHandle,
@Challenge,
@AppId,
@Version,
@CreationDate
)
END

View File

@ -0,0 +1,12 @@
CREATE PROCEDURE [dbo].[U2f_DeleteByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DELETE
FROM
[dbo].[U2f]
WHERE
[UserId] = @UserId
END

View File

@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[U2f_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
*
FROM
[dbo].[U2fView]
WHERE
[UserId] = @UserId
END

View File

@ -0,0 +1,11 @@
CREATE TABLE [dbo].[U2f] (
[Id] INT NOT NULL IDENTITY,
[UserId] UNIQUEIDENTIFIER NOT NULL,
[KeyHandle] VARCHAR (50) NOT NULL,
[Challenge] VARCHAR (50) NOT NULL,
[AppId] VARCHAR (50) NOT NULL,
[Version] VARCHAR (50) NOT NULL,
[CreationDate] DATETIME2 (7) NOT NULL,
CONSTRAINT [PK_U2f] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_U2f_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
);

View File

@ -0,0 +1,6 @@
CREATE VIEW [dbo].[U2fView]
AS
SELECT
*
FROM
[dbo].[U2f]