Skip to content

Commit

Permalink
Merge pull request #6 from dodobrands/improve-sql
Browse files Browse the repository at this point in the history
Improve sql
  • Loading branch information
vanbukin committed Feb 9, 2024
2 parents 3090ec1 + 27c3fe9 commit 0a49da4
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Polly" Version="8.2.1" />
<PackageReference Include="Polly" Version="8.3.0" />
</ItemGroup>

</Project>
12 changes: 9 additions & 3 deletions src/WebAuthn.Net.Storage.MySql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ CREATE TABLE `CredentialRecords`
) CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;

CREATE UNIQUE INDEX `IX_CredentialRecords_RpId_UserHandle_CredentialId` ON `CredentialRecords`
CREATE UNIQUE INDEX `IX_CredentialRecords_UserHandle_CredentialId_RpId` ON `CredentialRecords`
(
`RpId`,
`UserHandle`,
`CredentialId`
`CredentialId`,
`RpId`
);

CREATE UNIQUE INDEX `IX_CredentialRecords_CredentialId_RpId` ON `CredentialRecords`
(
`CredentialId`,
`RpId`
);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public DefaultMySqlCredentialStorage(ITimeProvider timeProvider)
ArgumentNullException.ThrowIfNull(context);
cancellationToken.ThrowIfCancellationRequested();
var dbPublicKeysEnumerable = await context.Connection.QueryAsync<MySqlPublicKeyCredentialDescriptor>(new(@"
SELECT `Type`, `CredentialId`, `Transports`, `CreatedAtUnixTime` FROM `CredentialRecords`
WHERE `RpId` = @rpId AND `UserHandle` = @userHandle;",
SELECT `Type`, `CredentialId`, `Transports`, `CreatedAtUnixTime`
FROM `CredentialRecords`
WHERE `UserHandle` = @userHandle AND `RpId` = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -93,7 +94,7 @@ public DefaultMySqlCredentialStorage(ITimeProvider timeProvider)
var exisingId = await context.Connection.QuerySingleOrDefaultAsync<byte[]?>(new(@"
SELECT `Id`
FROM `CredentialRecords`
WHERE `RpId` = @rpId AND `UserHandle` = @userHandle AND `CredentialId` = @credentialId;",
WHERE `UserHandle` = @userHandle AND `CredentialId` = @credentialId AND `RpId` = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -168,10 +169,9 @@ public DefaultMySqlCredentialStorage(ITimeProvider timeProvider)
cancellationToken.ThrowIfCancellationRequested();
var existingCount = await context.Connection.ExecuteScalarAsync<long>(new(
@"
SELECT COUNT(`Id`) FROM `CredentialRecords`
WHERE
`RpId` = @rpId
AND `CredentialId` = @credentialId;",
SELECT COUNT(`CredentialId`)
FROM `CredentialRecords`
WHERE `CredentialId` = @credentialId AND `RpId` = @rpId;",
new
{
rpId = credential.RpId,
Expand Down Expand Up @@ -290,11 +290,9 @@ public DefaultMySqlCredentialStorage(ITimeProvider timeProvider)
cancellationToken.ThrowIfCancellationRequested();
var recordIdToUpdate = await context.Connection.QuerySingleOrDefaultAsync<byte[]?>(new(
@"
SELECT `Id` FROM `CredentialRecords`
WHERE
`RpId` = @rpId
AND `UserHandle` = @userHandle
AND `CredentialId` = @credentialId;",
SELECT `Id`
FROM `CredentialRecords`
WHERE `UserHandle` = @userHandle AND `CredentialId` = @credentialId AND `RpId` = @rpId;",
new
{
rpId = credential.RpId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="MySqlConnector" Version="2.3.4" />
<PackageReference Include="MySqlConnector" Version="2.3.5" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion src/WebAuthn.Net.Storage.PostgreSql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ CREATE TABLE "CredentialRecords" (
CONSTRAINT "PK_CredentialRecords" PRIMARY KEY ("Id")
);
CREATE UNIQUE INDEX "IX_CredentialRecords_RpId_UserHandle_CredentialId" ON "CredentialRecords" ("RpId", "UserHandle", "CredentialId");
CREATE UNIQUE INDEX "IX_CredentialRecords_UserHandle_CredentialId_RpId" ON "CredentialRecords" ("UserHandle", "CredentialId", "RpId");
CREATE UNIQUE INDEX "IX_CredentialRecords_CredentialId_RpId" ON "CredentialRecords" ("CredentialId", "RpId");
```

## Local dev environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public DefaultPostgreSqlCredentialStorage(ITimeProvider timeProvider)
ArgumentNullException.ThrowIfNull(context);
cancellationToken.ThrowIfCancellationRequested();
var dbPublicKeysEnumerable = await context.Connection.QueryAsync<PostgreSqlPublicKeyCredentialDescriptor>(new(@"
SELECT ""Type"", ""CredentialId"", ""Transports"", ""CreatedAtUnixTime"" FROM ""CredentialRecords""
WHERE ""RpId"" = @rpId AND ""UserHandle"" = @userHandle;",
SELECT ""Type"", ""CredentialId"", ""Transports"", ""CreatedAtUnixTime""
FROM ""CredentialRecords""
WHERE ""UserHandle"" = @userHandle AND ""RpId"" = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -94,7 +95,7 @@ public DefaultPostgreSqlCredentialStorage(ITimeProvider timeProvider)
var exisingId = await context.Connection.QuerySingleOrDefaultAsync<Guid?>(new(@"
SELECT ""Id""
FROM ""CredentialRecords""
WHERE ""RpId"" = @rpId AND ""UserHandle"" = @userHandle AND ""CredentialId"" = @credentialId;",
WHERE ""UserHandle"" = @userHandle AND ""CredentialId"" = @credentialId AND ""RpId"" = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -169,10 +170,9 @@ public DefaultPostgreSqlCredentialStorage(ITimeProvider timeProvider)
cancellationToken.ThrowIfCancellationRequested();
var existingCount = await context.Connection.ExecuteScalarAsync<long>(new(
@"
SELECT COUNT(""Id"") FROM ""CredentialRecords""
WHERE
""RpId"" = @rpId
AND ""CredentialId"" = @credentialId;",
SELECT COUNT(""CredentialId"")
FROM ""CredentialRecords""
WHERE ""CredentialId"" = @credentialId AND ""RpId"" = @rpId;",
new
{
rpId = credential.RpId,
Expand Down Expand Up @@ -291,11 +291,9 @@ public DefaultPostgreSqlCredentialStorage(ITimeProvider timeProvider)
cancellationToken.ThrowIfCancellationRequested();
var recordIdToUpdate = await context.Connection.QuerySingleOrDefaultAsync<Guid?>(new(
@"
SELECT ""Id"" FROM ""CredentialRecords""
WHERE
""RpId"" = @rpId
AND ""UserHandle"" = @userHandle
AND ""CredentialId"" = @credentialId;",
SELECT ""Id""
FROM ""CredentialRecords""
WHERE ""UserHandle"" = @userHandle AND ""CredentialId"" = @credentialId AND ""RpId"" = @rpId;",
new
{
rpId = credential.RpId,
Expand Down
54 changes: 28 additions & 26 deletions src/WebAuthn.Net.Storage.SqlServer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,36 @@ As the library is intended to be integrated into existing applications, they may
```tsql
CREATE TABLE [CredentialRecords]
(
[Id] uniqueidentifier NOT NULL,
[RpId] nvarchar(256) NOT NULL,
[UserHandle] varbinary(128) NOT NULL,
[CredentialId] varbinary(1024) NOT NULL,
[Type] int NOT NULL,
[Kty] int NOT NULL,
[Alg] int NOT NULL,
[Ec2Crv] int NULL,
[Ec2X] varbinary(256) NULL,
[Ec2Y] varbinary(256) NULL,
[RsaModulusN] varbinary(1024) NULL,
[RsaExponentE] varbinary(32) NULL,
[OkpCrv] int NULL,
[OkpX] varbinary(32) NULL,
[SignCount] bigint NOT NULL,
[Transports] nvarchar(max) NOT NULL,
[UvInitialized] bit NOT NULL,
[BackupEligible] bit NOT NULL,
[BackupState] bit NOT NULL,
[AttestationObject] varbinary(max) NULL,
[AttestationClientDataJson] varbinary(max) NULL,
[Description] nvarchar(200) NULL,
[CreatedAtUnixTime] bigint NOT NULL,
[UpdatedAtUnixTime] bigint NOT NULL,
[Id] uniqueidentifier NOT NULL,
[RpId] nvarchar(256) NOT NULL,
[UserHandle] varbinary(128) NOT NULL,
[CredentialId] varbinary(1024) NOT NULL,
[Type] int NOT NULL,
[Kty] int NOT NULL,
[Alg] int NOT NULL,
[Ec2Crv] int NULL,
[Ec2X] varbinary(256) NULL,
[Ec2Y] varbinary(256) NULL,
[RsaModulusN] varbinary(1024) NULL,
[RsaExponentE] varbinary(32) NULL,
[OkpCrv] int NULL,
[OkpX] varbinary(32) NULL,
[SignCount] bigint NOT NULL,
[Transports] nvarchar(max) NOT NULL,
[UvInitialized] bit NOT NULL,
[BackupEligible] bit NOT NULL,
[BackupState] bit NOT NULL,
[AttestationObject] varbinary(max) NULL,
[AttestationClientDataJson] varbinary(max) NULL,
[Description] nvarchar(200) NULL,
[CreatedAtUnixTime] bigint NOT NULL,
[UpdatedAtUnixTime] bigint NOT NULL,
CONSTRAINT [PK_CredentialRecords] PRIMARY KEY ([Id])
);
ALTER TABLE [CredentialRecords] ADD CONSTRAINT [Transports should be formatted as JSON] CHECK (ISJSON(Transports)=1);
CREATE UNIQUE INDEX [IX_CredentialRecords_RpId_UserHandle_CredentialId] ON [CredentialRecords] ([RpId], [UserHandle], [CredentialId]);
ALTER TABLE [CredentialRecords]
ADD CONSTRAINT [Transports should be formatted as JSON] CHECK (ISJSON(Transports) = 1);
CREATE UNIQUE INDEX [IX_CredentialRecords_UserHandle_CredentialId_RpId] ON [CredentialRecords] ([UserHandle], [CredentialId], [RpId]);
CREATE UNIQUE INDEX [IX_CredentialRecords_CredentialId_RpId] ON [CredentialRecords] ([CredentialId], [RpId]);
```

## Local dev environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public DefaultSqlServerCredentialStorage(ITimeProvider timeProvider)
ArgumentNullException.ThrowIfNull(context);
cancellationToken.ThrowIfCancellationRequested();
var dbPublicKeysEnumerable = await context.Connection.QueryAsync<SqlServerPublicKeyCredentialDescriptor>(new(@"
SELECT Type, CredentialId, Transports, CreatedAtUnixTime FROM CredentialRecords
WHERE RpId = @rpId AND UserHandle = @userHandle;",
SELECT Type, CredentialId, Transports, CreatedAtUnixTime
FROM CredentialRecords
WHERE UserHandle = @userHandle AND RpId = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -92,7 +93,7 @@ public DefaultSqlServerCredentialStorage(ITimeProvider timeProvider)
var exisingId = await context.Connection.QuerySingleOrDefaultAsync<Guid?>(new(@"
SELECT Id
FROM CredentialRecords
WHERE RpId = @rpId AND UserHandle = @userHandle AND CredentialId = @credentialId;",
WHERE UserHandle = @userHandle AND CredentialId = @credentialId AND RpId = @rpId;",
new
{
rpId,
Expand Down Expand Up @@ -166,10 +167,9 @@ FROM CredentialRecords
cancellationToken.ThrowIfCancellationRequested();
var existingCount = await context.Connection.ExecuteScalarAsync<long>(new(
@"
SELECT COUNT(Id) FROM CredentialRecords
WHERE
RpId = @rpId
AND CredentialId = @credentialId;",
SELECT COUNT(CredentialId)
FROM CredentialRecords
WHERE CredentialId = @credentialId AND RpId = @rpId;",
new
{
rpId = credential.RpId,
Expand Down Expand Up @@ -288,11 +288,9 @@ FROM CredentialRecords
cancellationToken.ThrowIfCancellationRequested();
var recordIdToUpdate = await context.Connection.QuerySingleOrDefaultAsync<Guid?>(new(
@"
SELECT Id FROM CredentialRecords
WHERE
RpId = @rpId
AND UserHandle = @userHandle
AND CredentialId = @credentialId;",
SELECT Id
FROM CredentialRecords
WHERE UserHandle = @userHandle AND CredentialId = @credentialId AND RpId = @rpId;",
new
{
rpId = credential.RpId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.4" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions tests/WebAuthn.Net.Tests.Unit/WebAuthn.Net.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.10.0">
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 0a49da4

Please sign in to comment.