Skip to content

Data Protection

Data protection settings control encryption and decryption for authentication cookies and antiforgery tokens.

Overview

json
{
  "DataProtection": {
    "Enabled": true,
    "CustomApplicationName": null,
    "DefaultKeyLifetimeDays": 90,
    "Storage": "Default",
    "FileSystemPath": "./data-protection-keys",
    "GetAllElementsCommand": "select get_data_protection_keys()",
    "StoreElementCommand": "call store_data_protection_keys($1,$2)",
    "EncryptionAlgorithm": null,
    "ValidationAlgorithm": null
  }
}

Settings Reference

SettingTypeDefaultDescription
EnabledbooltrueEnable data protection.
CustomApplicationNamestringnullApplication name for encryption scope. Uses ApplicationName if null. Different names cannot decrypt each other's data.
DefaultKeyLifetimeDaysint90Number of days before keys are rotated.
Storagestring"Default"Key storage location: "Default", "FileSystem", or "Database".
FileSystemPathstring"./data-protection-keys"Path for file system storage.
GetAllElementsCommandstring"select get_data_protection_keys()"Database command to retrieve keys. Returns rows with single text column.
StoreElementCommandstring"call store_data_protection_keys($1,$2)"Database command to store keys. Receives name and data as text parameters.
EncryptionAlgorithmstringnullEncryption algorithm. Uses default if null.
ValidationAlgorithmstringnullValidation algorithm. Uses default if null.

Storage Options

Default Storage

json
{
  "DataProtection": {
    "Storage": "Default"
  }
}

Uses the platform's default key storage location.

Linux Users

On Linux, Default storage does not persist keys. When keys are lost on restart, encrypted tokens (authentication cookies) will stop working. Linux deployments should use FileSystem or Database storage.

File System Storage

json
{
  "DataProtection": {
    "Storage": "FileSystem",
    "FileSystemPath": "/var/lib/npgsqlrest/keys"
  }
}

Stores keys in the specified directory.

Docker

When running in Docker, ensure FileSystemPath points to a Docker volume to persist keys across container restarts.

Database Storage

json
{
  "DataProtection": {
    "Storage": "Database",
    "GetAllElementsCommand": "select get_data_protection_keys()",
    "StoreElementCommand": "call store_data_protection_keys($1,$2)"
  }
}

Stores keys in the PostgreSQL database using custom functions.

Encryption Algorithms

Configure the encryption algorithm for data protection keys:

ValueDescription
nullUse default algorithm
AES_128_CBCAES 128-bit CBC mode
AES_192_CBCAES 192-bit CBC mode
AES_256_CBCAES 256-bit CBC mode
AES_128_GCMAES 128-bit GCM mode
AES_192_GCMAES 192-bit GCM mode
AES_256_GCMAES 256-bit GCM mode
json
{
  "DataProtection": {
    "EncryptionAlgorithm": "AES_256_GCM"
  }
}

Validation Algorithms

Configure the validation algorithm for data protection keys:

ValueDescription
nullUse default algorithm
HMACSHA256HMAC SHA-256
HMACSHA512HMAC SHA-512
json
{
  "DataProtection": {
    "ValidationAlgorithm": "HMACSHA512"
  }
}

Application Name Scope

The CustomApplicationName determines the encryption scope. Applications with different names cannot decrypt each other's data:

json
{
  "DataProtection": {
    "CustomApplicationName": "my-app-production"
  }
}

If null, uses the top-level ApplicationName setting.

Complete Example

Production configuration with database storage:

json
{
  "DataProtection": {
    "Enabled": true,
    "CustomApplicationName": null,
    "DefaultKeyLifetimeDays": 90,
    "Storage": "Database",
    "GetAllElementsCommand": "select get_data_protection_keys()",
    "StoreElementCommand": "call store_data_protection_keys($1,$2)",
    "EncryptionAlgorithm": "AES_256_GCM",
    "ValidationAlgorithm": "HMACSHA512"
  }
}

Production configuration with file system storage (Docker):

json
{
  "DataProtection": {
    "Enabled": true,
    "DefaultKeyLifetimeDays": 90,
    "Storage": "FileSystem",
    "FileSystemPath": "/app/keys"
  }
}

Next Steps

Released under the MIT License.