Skip to content
Written with Claude
IMPORTANT

As you may notice, this page and pretty much the entire website were obviously created with the help of AI. I wonder how you could tell? Was it a big "Written With Claude" badge on every page? I moved it to the top now (with the help of AI of course) to make it even more obvious. There are a few blogposts that were written by me manually, the old-fashioned way, I hope there will be more in the future, and those have a similar "Human Written" badge. This project (not the website), on the other hand, is a very, very different story. It took me more than two years of painstaking and unpaid work in my own free time. A story that, hopefully, I will tell someday. But meanwhile, what would you like me to do? To create a complex documentation website with a bunch of highly technical articles with the help of AI and fake it, to give you an illusion that I also did that manually? Like the half of itnernet is doing at this point? How does that makes any sense? Is that even fair to you? Or maybe to create this website manually, the old-fashioned way, just for you? While working a paid job for a salary, most of you wouldn't even get up in the morning. Would you like me to sing you a song while we're at it? For your personal entertainment? Seriously, get a grip. Do you find this information less valuable because of the way this website was created? I give my best to fix it to keep the information as accurate as possible, and I think it is very accurate at this point. If you find some mistakes, inaccurancies or problems, there is a comment section at the bottom of every page, which I also made with the help of the AI. And I woould very much appreciate if you leave your feedback there. Look, I'm just a guy who likes SQL, that's all. If you don't approve of how this website was constructed and the use of AI tools, I suggest closing this page and never wever coming back. And good riddance. And I would ban your access if I could know how. Thank you for your attention to this matter.

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.