PlotDirector/PlotLine/Sql/238_NativeInstallationAuthorisation.sql

94 lines
6.1 KiB
Transact-SQL

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
IF OBJECT_ID('dbo.NativeCompanionInstallations','U') IS NULL
BEGIN
CREATE TABLE dbo.NativeCompanionInstallations(
InstallationID uniqueidentifier NOT NULL PRIMARY KEY,UserID int NOT NULL,
SecurityStamp uniqueidentifier NOT NULL,DeviceName nvarchar(80) NOT NULL,RetainImport bit NOT NULL,
RefreshHash binary(32) NOT NULL,PreviousHash binary(32) NULL,RotationID uniqueidentifier NULL,
ConnectionID uniqueidentifier NOT NULL,RefreshExpiresUtc datetimeoffset NOT NULL,
CreatedUtc datetimeoffset NOT NULL,LastRenewedUtc datetimeoffset NOT NULL,LastSeenUtc datetimeoffset NULL,
DisconnectedUtc datetimeoffset NULL,RevokedUtc datetimeoffset NULL,
CONSTRAINT FK_NativeCompanionInstallations_User FOREIGN KEY(UserID) REFERENCES dbo.AppUser(UserID) ON DELETE CASCADE);
CREATE INDEX IX_NativeCompanionInstallations_User ON dbo.NativeCompanionInstallations(UserID,CreatedUtc DESC);
END;
GO
CREATE OR ALTER PROCEDURE dbo.NativeCompanionInstallation
@Action varchar(16),@InstallationID uniqueidentifier=NULL,@UserID int=NULL,@Stamp uniqueidentifier=NULL,
@DeviceName nvarchar(80)=NULL,@RetainImport bit=0,@RefreshHash varbinary(32)=NULL,@NextHash varbinary(32)=NULL,
@RotationID uniqueidentifier=NULL,@ConnectionID uniqueidentifier=NULL,@Now datetimeoffset=NULL
AS
BEGIN
SET NOCOUNT ON;SET XACT_ABORT ON;
IF @Now IS NULL SET @Now=TODATETIMEOFFSET(SYSUTCDATETIME(),'+00:00');
DECLARE @PriorConnection uniqueidentifier=NULL;
IF @Action='create'
BEGIN
IF DATALENGTH(@RefreshHash)<>32 OR @RefreshHash IS NULL OR @InstallationID IS NULL OR @ConnectionID IS NULL THROW 51000,'Invalid installation',1;
BEGIN TRANSACTION;
IF NOT EXISTS(SELECT 1 FROM dbo.AppUser WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND SecurityStamp=@Stamp AND (IsLocked=0 OR LockoutEndUtc<=@Now)) THROW 51000,'Account approval changed',1;
IF (SELECT COUNT(*) FROM dbo.NativeCompanionInstallations WHERE UserID=@UserID AND RevokedUtc IS NULL AND RefreshExpiresUtc>@Now)>=10 THROW 51000,'Revoke an unused Companion installation before approving another',1;
INSERT dbo.NativeCompanionInstallations(InstallationID,UserID,SecurityStamp,DeviceName,RetainImport,RefreshHash,ConnectionID,RefreshExpiresUtc,CreatedUtc,LastRenewedUtc)
VALUES(@InstallationID,@UserID,@Stamp,LEFT(COALESCE(@DeviceName,N'Windows Companion'),80),@RetainImport,@RefreshHash,@ConnectionID,DATEADD(day,90,@Now),@Now,@Now);
COMMIT;
END
ELSE IF @Action='rotate'
BEGIN
IF @ConnectionID IS NULL OR @RotationID IS NULL OR @RefreshHash IS NULL OR @NextHash IS NULL OR DATALENGTH(@RefreshHash)<>32 OR DATALENGTH(@NextHash)<>32 RETURN;
BEGIN TRANSACTION;
SELECT @PriorConnection=i.ConnectionID FROM dbo.NativeCompanionInstallations i WITH(UPDLOCK,HOLDLOCK)
JOIN dbo.AppUser u ON u.UserID=i.UserID AND u.SecurityStamp=i.SecurityStamp
WHERE i.InstallationID=@InstallationID AND i.RevokedUtc IS NULL AND i.RefreshExpiresUtc>@Now AND (u.IsLocked=0 OR u.LockoutEndUtc<=@Now)
AND (i.RefreshHash=@RefreshHash OR (i.PreviousHash=@RefreshHash AND i.RotationID=@RotationID AND i.RefreshHash=@NextHash AND i.ConnectionID=@ConnectionID));
IF @PriorConnection IS NULL BEGIN COMMIT;RETURN;END;
-- A retry must prove possession of the same pending next secret, not merely an old token.
IF EXISTS(SELECT 1 FROM dbo.NativeCompanionInstallations WHERE InstallationID=@InstallationID AND RefreshHash=@RefreshHash)
BEGIN
IF @RefreshHash=@NextHash BEGIN COMMIT;RETURN;END;
UPDATE dbo.NativeCompanionInstallations SET PreviousHash=RefreshHash,RefreshHash=@NextHash,RotationID=@RotationID,ConnectionID=@ConnectionID,
RefreshExpiresUtc=DATEADD(day,90,@Now),LastRenewedUtc=@Now WHERE InstallationID=@InstallationID;
END;
COMMIT;
END
ELSE IF @Action='validate'
BEGIN
UPDATE i SET LastSeenUtc=CASE WHEN DisconnectedUtc IS NOT NULL OR LastSeenUtc IS NULL OR LastSeenUtc<DATEADD(second,-15,@Now) THEN @Now ELSE LastSeenUtc END,DisconnectedUtc=NULL
FROM dbo.NativeCompanionInstallations i JOIN dbo.AppUser u ON u.UserID=i.UserID AND u.SecurityStamp=i.SecurityStamp
WHERE i.InstallationID=@InstallationID AND i.UserID=@UserID AND i.SecurityStamp=@Stamp AND i.ConnectionID=@ConnectionID AND i.RevokedUtc IS NULL
AND i.RefreshExpiresUtc>@Now AND (u.IsLocked=0 OR u.LockoutEndUtc<=@Now)
AND (i.LastSeenUtc IS NULL OR i.LastSeenUtc<DATEADD(second,-15,@Now) OR i.DisconnectedUtc IS NOT NULL);
IF @@ROWCOUNT=1 SELECT @InstallationID;
ELSE SELECT i.InstallationID FROM dbo.NativeCompanionInstallations i JOIN dbo.AppUser u ON u.UserID=i.UserID AND u.SecurityStamp=i.SecurityStamp
WHERE i.InstallationID=@InstallationID AND i.UserID=@UserID AND i.SecurityStamp=@Stamp AND i.ConnectionID=@ConnectionID AND i.RevokedUtc IS NULL AND i.RefreshExpiresUtc>@Now AND (u.IsLocked=0 OR u.LockoutEndUtc<=@Now);
RETURN;
END
ELSE IF @Action='revoke-secret'
BEGIN
UPDATE dbo.NativeCompanionInstallations SET RevokedUtc=COALESCE(RevokedUtc,@Now) WHERE InstallationID=@InstallationID
AND (RefreshHash=@RefreshHash OR (PreviousHash=@RefreshHash AND RotationID=@RotationID AND RefreshHash=@NextHash));
IF @@ROWCOUNT=0 RETURN;
END
ELSE IF @Action='revoke-user'
BEGIN
UPDATE dbo.NativeCompanionInstallations SET RevokedUtc=COALESCE(RevokedUtc,@Now) WHERE InstallationID=@InstallationID AND UserID=@UserID;
IF @@ROWCOUNT=0 RETURN;
END
ELSE IF @Action='revoke-all'
BEGIN
UPDATE dbo.NativeCompanionInstallations SET RevokedUtc=COALESCE(RevokedUtc,@Now) WHERE UserID=@UserID;
END
ELSE IF @Action='disconnect'
BEGIN
UPDATE dbo.NativeCompanionInstallations SET DisconnectedUtc=@Now WHERE InstallationID=@InstallationID AND UserID=@UserID AND ConnectionID=@ConnectionID;
RETURN;
END
ELSE IF @Action<>'list' THROW 51000,'Unknown installation operation',1;
SELECT i.InstallationID,i.UserID,i.SecurityStamp,i.DeviceName,i.RetainImport,i.ConnectionID,i.RefreshExpiresUtc,i.CreatedUtc,i.LastRenewedUtc,i.LastSeenUtc,i.DisconnectedUtc,i.RevokedUtc,
COALESCE(NULLIF(u.DisplayName,N''),u.Email) AccountName,@PriorConnection PreviousConnectionID
FROM dbo.NativeCompanionInstallations i JOIN dbo.AppUser u ON u.UserID=i.UserID
WHERE (@InstallationID IS NULL OR i.InstallationID=@InstallationID) AND (@UserID IS NULL OR i.UserID=@UserID)
ORDER BY i.CreatedUtc DESC;
END;
GO