SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH](
	[GUID] [bigint] IDENTITY(1,1) NOT NULL,
	[ACTIVE] [bit] NOT NULL,
	[DESCRIPTION] [nvarchar](250) NULL,
	[TYPE_ID] [tinyint] NOT NULL,
	[API_KEY] [nvarchar](150) NULL,
	[API_VALUE] [nvarchar](150) NULL,
	[API_KEY_ADD_TO_ID] [bit] NULL,
	[TOKEN] [nvarchar](150) NULL,
	[USERNAME] [nvarchar](100) NULL,
	[PASSWORD] [nvarchar](100) NULL,
	[DOMAIN] [nvarchar](50) NULL,
	[WORKSTATION] [nvarchar](50) NULL,
	[ADDED_WHO] [nvarchar](50) NOT NULL,
	[ADDED_WHEN] [datetime] NOT NULL,
	[CHANGED_WHO] [nvarchar](50) NULL,
	[CHANGED_WHEN] [datetime] NULL,
 CONSTRAINT [PK_TBREC_CFG_ENDPOINT_AUTH_GUID] PRIMARY KEY CLUSTERED 
(
	[GUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH] ADD  CONSTRAINT [DF_TBREC_CFG_ENDPOINT_AUTH_ACTIVE]  DEFAULT ((1)) FOR [ACTIVE]
GO

ALTER TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH] ADD  CONSTRAINT [DF_TBREC_CFG_ENDPOINT_AUTH_TYPE_ID]  DEFAULT ((0)) FOR [TYPE_ID]
GO

ALTER TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH] ADD  CONSTRAINT [DF_TBREC_CFG_ENDPOINT_AUTH_ADDED_WHO]  DEFAULT (suser_sname()) FOR [ADDED_WHO]
GO

ALTER TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH] ADD  CONSTRAINT [DF_TBREC_CFG_ENDPOINT_AUTH_ADDED_WHEN]  DEFAULT (sysdatetime()) FOR [ADDED_WHEN]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Primärschlüssel' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'GUID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ein-/Ausschalter' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'ACTIVE'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Beschreibung' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'DESCRIPTION'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Auth-Typ ("0 = No Auth", "1 = API Key", "2 = Bearer Token", "3 = JWT Bearer", "4 = Basic Auth", "5 = Digest Auth", "6 = OAuth 1.0", "7 = OAuth 2.0", "8 = AWS Signature", "9 = NTLM Auth")' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'TYPE_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'API-Key-Name/-Header' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'API_KEY'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'API-Key-Wert' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'API_VALUE'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Platzierung (0 = HEADER; 1 = QUERY)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'API_KEY_ADD_TO_ID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Bearer/JWT-Token' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'TOKEN'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Username Basic/Digest/NTLM' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'USERNAME'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Passwort Basic/Digest/NTLM' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'PASSWORD'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Domain fuer NTLM' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'DOMAIN'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Workstation fuer NTLM' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'WORKSTATION'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Angelegt von' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'ADDED_WHO'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Angelegt am' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'ADDED_WHEN'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Geaendert von' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'CHANGED_WHO'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Geaendert am' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TBREC_CFG_ENDPOINT_AUTH', @level2type=N'COLUMN',@level2name=N'CHANGED_WHEN'
GO

--------------------------------------------------------------------------------------

CREATE OR ALTER TRIGGER [dbo].[TBREC_CFG_ENDPOINT_AUTH_AFT_UPD] ON [dbo].[TBREC_CFG_ENDPOINT_AUTH]
FOR UPDATE
AS
	UPDATE [TBREC_CFG_ENDPOINT_AUTH] 
	SET    [CHANGED_WHEN] = (SYSDATETIME())
	FROM   [INSERTED]
	WHERE  [TBREC_CFG_ENDPOINT_AUTH].[GUID] = [INSERTED].[GUID]
GO

ALTER TABLE [dbo].[TBREC_CFG_ENDPOINT_AUTH] ENABLE TRIGGER [TBREC_CFG_ENDPOINT_AUTH_AFT_UPD]
GO