Skip to content

Authentication Options

Basic authentication configuration for NpgsqlRest endpoints including login/logout handling and password settings.

Overview

json
{
  "NpgsqlRest": {
    "AuthenticationOptions": {
      "DefaultAuthenticationType": null,
      "StatusColumnName": "status",
      "SchemeColumnName": "scheme",
      "BodyColumnName": "body",
      "ResponseTypeColumnName": "application/json",
      "HashColumnName": "hash",
      "PasswordParameterNameContains": "pass",
      "DefaultUserIdClaimType": "user_id",
      "DefaultNameClaimType": "user_name",
      "DefaultRoleClaimType": "user_roles",
      "SerializeAuthEndpointsResponse": false,
      "ObfuscateAuthParameterLogValues": true,
      "PasswordVerificationFailedCommand": null,
      "PasswordVerificationSucceededCommand": null,
      "UseUserContext": false,
      "ContextKeyClaimsMapping": {
        "request.user_id": "user_id",
        "request.user_name": "user_name",
        "request.user_roles": "user_roles"
      },
      "ClaimsJsonContextKey": null,
      "IpAddressContextKey": "request.ip_address",
      "UseUserParameters": false,
      "ParameterNameClaimsMapping": {
        "_user_id": "user_id",
        "_user_name": "user_name",
        "_user_roles": "user_roles"
      },
      "ClaimsJsonParameterName": "_user_claims",
      "IpAddressParameterName": "_ip_address",
      "LoginPath": null,
      "LogoutPath": null,
      "BasicAuth": {
        "Enabled": false,
        "Realm": null,
        "Users": {},
        "SslRequirement": "Required",
        "UseDefaultPasswordHasher": true,
        "ChallengeCommand": null
      }
    }
  }
}

General Settings

SettingTypeDefaultDescription
DefaultAuthenticationTypestringnullAuthentication type for ClaimsIdentity. Auto-detected from database name if null and login endpoint exists.
SerializeAuthEndpointsResponseboolfalseWhen true, login endpoint returns all columns from the login routine as JSON in the response body (ignored for bearer token auth or when BodyColumnName is present).
ObfuscateAuthParameterLogValuesbooltrueObfuscate parameter values in logs for auth endpoints to protect credentials.

Login Response Columns

Column names used to read values from the login routine response.

SettingTypeDefaultDescription
StatusColumnNamestring"status"Column for success/failure. Boolean or numeric HTTP status code (200 = success).
SchemeColumnNamestring"scheme"Column for authentication scheme override.
BodyColumnNamestring"body"Column for response body message.
ResponseTypeColumnNamestring"application/json"Column for response content type.
HashColumnNamestring"hash"Column for password hash verification. See Password Verification.

Password Handling

These settings are part of the built-in password verification system. For detailed information on how password verification works, including examples and the built-in password hasher, see Password Verification in the login annotation documentation.

SettingTypeDefaultDescription
PasswordParameterNameContainsstring"pass"Identifies password parameter (first param containing this string). See Password Parameter Detection.
PasswordVerificationFailedCommandstringnullCommand executed on password verification failure.
PasswordVerificationSucceededCommandstringnullCommand executed on password verification success.

Password Verification Command Parameters

Both PasswordVerificationFailedCommand and PasswordVerificationSucceededCommand receive:

ParameterTypeDescription
$1textAuthentication scheme used for login.
$2textUser ID.
$3textUsername.

Default Claim Types

SettingTypeDefaultDescription
DefaultUserIdClaimTypestring"user_id"Claim type for user ID.
DefaultNameClaimTypestring"user_name"Claim type for username.
DefaultRoleClaimTypestring"user_roles"Claim type for user roles.

User Context Settings

Settings for automatically passing authenticated user claims to PostgreSQL via context variables.

SettingTypeDefaultDescription
UseUserContextboolfalseEnable setting authenticated user claims to context variables automatically. For proxy endpoints, when enabled, these values are also forwarded as HTTP headers to the upstream proxy.
ContextKeyClaimsMappingobjectSee belowMapping of context keys to user claim names. Keys are context variable names, values are user claim names.
ClaimsJsonContextKeystringnullContext key for all available user claims as JSON. When not null and user is authenticated, all claims are serialized to JSON and set to this context variable.
IpAddressContextKeystring"request.ip_address"Context key for IP address. When not null, IP address is set to this context variable when UseUserContext is enabled (even for unauthenticated users).

Default ContextKeyClaimsMapping

json
{
  "request.user_id": "user_id",
  "request.user_name": "user_name",
  "request.user_roles": "user_roles"
}

User Parameters Settings

Settings for automatically mapping authenticated user claims to function parameters.

SettingTypeDefaultDescription
UseUserParametersboolfalseEnable mapping authenticated user claims to parameters by name automatically. For proxy endpoints, when enabled, these values are also forwarded as query string parameters.
ParameterNameClaimsMappingobjectSee belowMapping of parameter names to user claim names. Keys are parameter names, values are user claim names.
ClaimsJsonParameterNamestring"_user_claims"Parameter name for all available user claims. When not null and user is authenticated, all claims are serialized to JSON and set to this parameter.
IpAddressParameterNamestring"_ip_address"Parameter name for IP address. When not null, IP address is set to this parameter when UseUserParameters is enabled (even for unauthenticated users).

Note: Claim values are always passed as text type. For multi-value claims (like roles), values are passed as text[]. PostgreSQL handles type coercion to your parameter types.

Default ParameterNameClaimsMapping

json
{
  "_user_id": "user_id",
  "_user_name": "user_name",
  "_user_roles": "user_roles"
}

Login and Logout Paths

SettingTypeDefaultDescription
LoginPathstringnullURL path for login endpoint. null disables login endpoint.
LogoutPathstringnullURL path for logout endpoint. null disables logout endpoint.

Login Command Convention

The login command must follow these conventions:

  • Return at least one record for successful authentication
  • No records returned = 401 Unauthorized
  • All columns become user claims (column name = claim type, value = claim value)

Special columns:

ColumnTypeDescription
statusbool/intSuccess indicator. Boolean or HTTP status code (200 = success).
schemetextAuthentication scheme override.
bodytextResponse body message.
hashtextPassword hash for verification.

Logout Command Convention

  • No return data = sign out default scheme
  • Returned values = scheme names to sign out (converted to string)

Basic Authentication

HTTP Basic Authentication settings. Expects Authorization: Basic base64(username:password) header.

json
{
  "NpgsqlRest": {
    "AuthenticationOptions": {
      "BasicAuth": {
        "Enabled": false,
        "Realm": null,
        "Users": {},
        "SslRequirement": "Required",
        "UseDefaultPasswordHasher": true,
        "ChallengeCommand": null
      }
    }
  }
}

For detailed configuration options, examples, and challenge command parameters, see Basic Auth Configuration.

Complete Example

Production configuration with login endpoint and user context:

json
{
  "NpgsqlRest": {
    "AuthenticationOptions": {
      "DefaultAuthenticationType": "MyApp",
      "StatusColumnName": "status",
      "SchemeColumnName": "scheme",
      "HashColumnName": "hash",
      "PasswordParameterNameContains": "password",
      "DefaultUserIdClaimType": "user_id",
      "DefaultNameClaimType": "user_name",
      "DefaultRoleClaimType": "user_roles",
      "ObfuscateAuthParameterLogValues": true,
      "UseUserContext": true,
      "ContextKeyClaimsMapping": {
        "request.user_id": "user_id",
        "request.user_name": "user_name",
        "request.user_roles": "user_roles"
      },
      "IpAddressContextKey": "request.ip_address",
      "UseUserParameters": true,
      "ParameterNameClaimsMapping": {
        "_user_id": "user_id",
        "_user_name": "user_name",
        "_user_roles": "user_roles"
      },
      "ClaimsJsonParameterName": "_user_claims",
      "IpAddressParameterName": "_ip_address",
      "LoginPath": "/api/auth/login",
      "LogoutPath": "/api/auth/logout"
    }
  }
}

Next Steps

Comments

Released under the MIT License.