[OAuth2] Infrastructure for tracking token expiry time
[OAuth2] Infrastructure for tracking token expiry time
Hi Hackers,
Currently, during OAuth2 authentication, the ValidatorModuleResult structure allows a validator(extension) to return the authentication status and the authn_id.
However, we ignore the token expiry time (exp claim).
Once a token is validated, the backend has no record of when that token actually expires. A session can remain open indefinitely even if the underlying access token has expired shortly after the connection was established.
This patch adds the infrastructure to capture and store this expiration timestamp within the backend session state. It does not implement an enforcement policy (such as auto-termination).
Request a review.
Thanks & Best Regards,
Ajit
Currently, during OAuth2 authentication, the ValidatorModuleResult structure allows a validator(extension) to return the authentication status and the authn_id.
However, we ignore the token expiry time (exp claim).
Once a token is validated, the backend has no record of when that token actually expires. A session can remain open indefinitely even if the underlying access token has expired shortly after the connection was established.
This patch adds the infrastructure to capture and store this expiration timestamp within the backend session state. It does not implement an enforcement policy (such as auto-termination).
Request a review.
Thanks & Best Regards,
Ajit
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
VASUKI M <vasukianand0119@gmail.com>
Дата:
On Mon, Feb 16, 2026 at 3:44 PM Ajit Awekar <ajitpostgres@gmail.com> wrote:
Hi Hackers,
Currently, during OAuth2 authentication, the ValidatorModuleResult structure allows a validator(extension) to return the authentication status and the authn_id.
However, we ignore the token expiry time (exp claim).
Once a token is validated, the backend has no record of when that token actually expires. A session can remain open indefinitely even if the underlying access token has expired shortly after the connection was established.
This patch adds the infrastructure to capture and store this expiration timestamp within the backend session state. It does not implement an enforcement policy (such as auto-termination).
Thanks for working on this. Storing the token expiry in the backend session state makes sense as groundwork for future enforcement.
I had a couple of questions while reading the patch.
First, is Port always zero-initialized? If not, we might want to explicitly initialize the new expiry field to a known value. Right now it looks like we’re relying on zero to mean “not provided”, but since TimestampTz value 0 is a valid timestamp (Postgres epoch), I’m wondering whether it would be clearer to use an explicit invalid/sentinel value instead.
Also, in the case where the validator returns an expiry that is already in the past, should we reject the authentication immediately? Or is that expected to be fully handled inside the validator module?
Finally, do you have a particular enforcement model in mind for follow-up work (e.g., check at statement start, transaction boundaries, or via some timeout mechanism)? It would help to understand how you see this being used.
The change itself looks straightforward, just trying to clarify the intended semantics.
Best regards,
Vasuki M
C-DAC,Chennai.
C-DAC,Chennai.
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello This API looks simple for providers that use JWT access tokens, but what about providers that use opaque tokens and an introspection API to check validity instead? Some validators might not be able to provide anything meaningful without a periodic call to a "check validity now" method, and even some providers that use JWT access tokens support immediate revocation, where these periodic checks would be useful.
Re: [OAuth2] Infrastructure for tracking token expiry time
Thanks Vasuki and Zsolt for your reply and comments.
>> First, is Port always zero-initialized? If not, we might want to explicitly initialize the new expiry field to a known value. Right now it looks like we’re relying on zero to mean “not provided”, but since TimestampTz value 0 is a valid timestamp (Postgres epoch), I’m wondering whether it would be clearer to use an explicit invalid/sentinel value instead.
I agree. The attached patch value is now initialised to sentinel DT_NOBEGIN to indicate no expiry value has been provided yet.
>> Also, in the case where the validator returns an expiry that is already in the past, should we reject the authentication immediately? Or is that expected to be fully handled inside the validator module?
The design assumes that the Validator module will handle the immediate rejection of tokens already in the past. The expiry field is intended for the backend to manage session life after successful authentication
>> Finally, do you have a particular enforcement model in mind for follow-up work (e.g., check at statement start, transaction boundaries, or via some timeout mechanism)? It would help to understand how you see this being used.
Ideally we should check this at statement start.
>> This API looks simple for providers that use JWT access tokens, but
what about providers that use opaque tokens and an introspection API
to check validity instead?
what about providers that use opaque tokens and an introspection API
to check validity instead?
For providers using opaque tokens or introspection APIs where an 'exp' claim might be missing, the API remains compatible by allowing the validator to return DT_NOBEGIN.
Request a review.
Thanks & Best Regards,
Ajit
On Tue, 17 Feb 2026 at 01:10, Zsolt Parragi <zsolt.parragi@percona.com> wrote:
Hello
This API looks simple for providers that use JWT access tokens, but
what about providers that use opaque tokens and an introspection API
to check validity instead? Some validators might not be able to
provide anything meaningful without a periodic call to a "check
validity now" method, and even some providers that use JWT access
tokens support immediate revocation, where these periodic checks would
be useful.
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> For providers using opaque tokens or introspection APIs where an 'exp' claim might be missing, the API remains compatible by allowing the validator to return DT_NOBEGIN. I don't think this is a good answer: the OAuth validator API went to the trouble of introducing a generic infrastructure with the explicit goal to work any OAuth provider, and now this proposes a change that limits a new API to some of them, while it wouldn't be more difficult to propose a generic API that works for everything. Let me rephrase the question: why is this a better approach than introducing an additional validator callback method, expired_cb? * it returns if the current OAuth token is expired or not * if it's NULL, nothing happens, so there's an easy upgrade path for validator in PG19 * for JWT validators with a clear expiry date, all they have to do is to store the expiry date in a global variable and then check if we passed that time in the new callback * alternatively, this callback could return the current expected expiry date, and the calling code could check it, but I think that's overcomplicating And in both cases, I think handling of the value/callback should be part of the patch - only providing an API and then doing nothing with it would set wrong expectations.
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
VASUKI M <vasukianand0119@gmail.com>
Дата:
Hi All,
I see the concern about keeping the validator API generic and not implicitly favoring JWT-style providers.
The callback-based approach does seem more flexible, especially for opaque tokens or providers supporting revocation, where validity cannot be represented as a fixed timestamp.
Perhaps one possible direction could be to support both:
An optional expiry timestamp for simple/static cases.
An optional callback (e.g., expired_cb) for dynamic validation.
This would allow JWT-based validators to remain lightweight while enabling more complex providers to implement custom revalidation logic.
If enforcement is planned at statement start, integrating the callback mechanism in the same patch might also clarify the intended semantics.
Best regards,
Vasuki M
C-DAC,Chennai
I see the concern about keeping the validator API generic and not implicitly favoring JWT-style providers.
The callback-based approach does seem more flexible, especially for opaque tokens or providers supporting revocation, where validity cannot be represented as a fixed timestamp.
Perhaps one possible direction could be to support both:
An optional expiry timestamp for simple/static cases.
An optional callback (e.g., expired_cb) for dynamic validation.
This would allow JWT-based validators to remain lightweight while enabling more complex providers to implement custom revalidation logic.
If enforcement is planned at statement start, integrating the callback mechanism in the same patch might also clarify the intended semantics.
Best regards,
Vasuki M
C-DAC,Chennai
Re: [OAuth2] Infrastructure for tracking token expiry time
Hi Vasuki, Zsolt
Thanks a lot for your review comments and reply.I have updated the patch and below is summary of changes
- Adding a check_oauth_expiry() function called during command execution to verify token validity
- Terminating sessions with expired/revoked tokens before executing new commands.
- Supporting callback-based revocation checks
I have added a unit test case to validate that sessions are properly terminated when their OAuth tokens expire.
Request a review.
Thanks & Best Regards,
Ajit
On Tue, 17 Feb 2026 at 16:17, VASUKI M <vasukianand0119@gmail.com> wrote:
Hi All,
I see the concern about keeping the validator API generic and not implicitly favoring JWT-style providers.
The callback-based approach does seem more flexible, especially for opaque tokens or providers supporting revocation, where validity cannot be represented as a fixed timestamp.
Perhaps one possible direction could be to support both:
An optional expiry timestamp for simple/static cases.
An optional callback (e.g., expired_cb) for dynamic validation.
This would allow JWT-based validators to remain lightweight while enabling more complex providers to implement custom revalidation logic.
If enforcement is planned at statement start, integrating the callback mechanism in the same patch might also clarify the intended semantics.
Best regards,
Vasuki M
C-DAC,Chennai
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
> On 18 Feb 2026, at 09:38, Ajit Awekar wrote:
> I have added a unit test case to validate that sessions are properly terminated when their OAuth tokens expire.
+ /*
+ * If the current session was authenticated via OAuth, verify that the
+ * token has not expired or been revoked before executing the query.
+ */
+ if (MyClientConnectionInfo.auth_method == uaOAuth)
+ check_oauth_expiry(MyProcPort);
This seems too expensive and invasive in these codepaths and also not in line
with how other authentication methods are handled, we for example don't check
if a client certificate has been revoked in a similar manner. I don't think it
would be bad if we could detect expired credentials mid-flight but I think it
would need to be done smarter than sprinkling auth specific checks like this.
+ ereport(FATAL,
+ (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
+ errmsg("session expired: OAuth token is no longer valid")));
Token expiration is IMHO not a usecase for a FATAL error, if we want to
terminate a connection we can do it in a more graceful way.
+ /*
+ * Optional callback to check if the session is still valid.
+ * Returns true if the token is expired/revoked, false otherwise.
+ * If NULL, the backend assumes the session never expires.
+ * If provided, the validator can use this to limit session duration based on
+ * parameter value or based on it's custom logic.
+ */
+ bool (*expired_cb) (TimestampTz expiry);
+
+ /*
+ * The expiration time of the token (e.g., from the 'exp' claim) if
+ * provided. This value is passed as an argument to the expired_cb function
+ * above to determine if the session should terminate.
+ */
+ TimestampTz expiry;
} ValidatorModuleResult;
What's the point of having an expiry timestamp as well as an expiration check
callback? Wouldn't that open up for the caller to make its own judgment on
expiration which might conflict with expired_cb? Is token expiration really
always tied to a timestamp for all types of tokens?
Also, why is this defined in ValidatorModuleResult? If I interpret Zsolt's
comment upthread correctly it was meant to be placed in OAuthValidatorCallbacks
as a new callback - which I agree with would be better. The question of where
and when to invoke it remains, but whatever we build I think it should be auth
agnostic so that any auth can be hooked into it.
--
Daniel Gustafsson
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> Also, why is this defined in ValidatorModuleResult? If I interpret Zsolt's > comment upthread correctly it was meant to be placed in OAuthValidatorCallbacks > as a new callback - which I agree with would be better. The question of where > and when to invoke it remains, but whatever we build I think it should be auth > agnostic so that any auth can be hooked into it. +1 Yes, that's what I meant. Also agree with the unnecessary expiry timestamp with the callback - if we want to store some additional data for the checks, that should be a void*, but with the single threaded model there's no real need for it, the validator can store anything it needs in global variables. > 2. Terminating sessions with expired/revoked tokens before executing new > commands. > Token expiration is IMHO not a use case for a FATAL error, if we want to > terminate a connection we can do it in a more graceful way. There are different reasons for token expiration, one is a simple timeout where all we have to do is communicate to the client that we need a refresh (gracefully), and the other is where a token is immediately revoked because of a security incident, in which case immediate termination is a good practice. There was an earlier discussion about it in the password expiration thread[1], and the possible use of the GoAway[2] for it. Jacob suggested making it user configurable, which seems like a reasonable way to do it. I also wanted to work on this patch, but I didn't start working on it so far, because I wanted to see where those threads go, as the changes introduced in them could be useful for the oauth token refresh/disconnection mechanism. [1] : https://www.postgresql.org/message-id/flat/CAER375OvH3_ONmc-SgUFpA6gv_d6eNj2KdZktzo-f_uqNwwWNw%40mail.gmail.com [2] : https://www.postgresql.org/message-id/flat/DDPQ1RV5FE9U.I2WW34NGRD8Z%40jeltef.nl
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
> On 18 Feb 2026, at 13:04, Zsolt Parragi wrote: >> 2. Terminating sessions with expired/revoked tokens before executing new >> commands. > >> Token expiration is IMHO not a use case for a FATAL error, if we want to >> terminate a connection we can do it in a more graceful way. > > There are different reasons for token expiration, one is a simple > timeout where all we have to do is communicate to the client that we > need a refresh (gracefully), and the other is where a token is > immediately revoked because of a security incident, in which case > immediate termination is a good practice. I understand these cases and agree that there are different needs for messaging to the user for these cases, but I still think that neither should overload what FATAL error means. The mechanism used is however a secondary discussion, first thing to get in place is a design for how to handle mid-connection credential expiration. -- Daniel Gustafsson
Re: [OAuth2] Infrastructure for tracking token expiry time
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> but I still think that neither should overload > what FATAL error means I see, I misunderstood what you meant by graceful there. In this case, this is also a good comment for the password expiration thread, currently that also uses FATAL errors for terminating a connection when the password expires. What other option do you see? Something new for this use case like GoAway, and clients not understanding it simply get disconnected after some grace period? Or using the recently merged connectionWarning to send a warning to the client, and disconnect it shortly if it doesn't do anything to fix the situation? When I tested the password expiration patch I noticed that deleted users who still have remaining active connections currently get ERRORs for every statement that requires permission checks, so in this regard using ERROR/FATAL for the situation seemed fine to me - it's similar to what already happens in some edge cases with authentication.
Re: [OAuth2] Infrastructure for tracking token expiry time
Thanks a lot Daniel, Zslot, Vasuki for your review comments.
>The mechanism used is however a secondary discussion,
>first thing to get in place is a design for how to handle mid-connection
This patch introduces a generic credential validation framework that allows
us to periodically validate authentication credentials during active
database sessions. When enabled, this feature detects expired
credentials and terminates sessions that are no longer valid.
Added GUCs
Credential_validation.enabled = on // Enable or Disable Credential validation
Credential_validation.interval = 120 //Frequency in seconds of running credential validation
The callback mechanism works by:
- Defining a CredentialValidationCallback function pointer type
- Maintaining an array of validators indexed by authentication method
- Allowing other auth mechanisms to register validators via
RegisterCredentialValidator()
- Selecting the appropriate validator at runtime based on the session's
authentication method
The current implementation primarily supports password-based authentication methods, verifying that passwords haven't expired. It can be extended to any authentication method.
This patch is WIP. I am submitting it now to get early feedback on the overall design and approach.
>first thing to get in place is a design for how to handle mid-connection
>credential expiration.
This patch introduces a generic credential validation framework that allows
us to periodically validate authentication credentials during active
database sessions. When enabled, this feature detects expired
credentials and terminates sessions that are no longer valid.
Added GUCs
Credential_validation.enabled = on // Enable or Disable Credential validation
Credential_validation.interval = 120 //Frequency in seconds of running credential validation
The callback mechanism works by:
- Defining a CredentialValidationCallback function pointer type
- Maintaining an array of validators indexed by authentication method
- Allowing other auth mechanisms to register validators via
RegisterCredentialValidator()
- Selecting the appropriate validator at runtime based on the session's
authentication method
The current implementation primarily supports password-based authentication methods, verifying that passwords haven't expired. It can be extended to any authentication method.
This patch is WIP. I am submitting it now to get early feedback on the overall design and approach.
Thanks & Best Regards,
Ajit
On Wed, 18 Feb 2026 at 22:29, Zsolt Parragi <zsolt.parragi@percona.com> wrote:
> but I still think that neither should overload
> what FATAL error means
I see, I misunderstood what you meant by graceful there. In this case,
this is also a good comment for the password expiration thread,
currently that also uses FATAL errors for terminating a connection
when the password expires.
What other option do you see? Something new for this use case like
GoAway, and clients not understanding it simply get disconnected after
some grace period? Or using the recently merged connectionWarning to
send a warning to the client, and disconnect it shortly if it doesn't
do anything to fix the situation?
When I tested the password expiration patch I noticed that deleted
users who still have remaining active connections currently get ERRORs
for every statement that requires permission checks, so in this regard
using ERROR/FATAL for the situation seemed fine to me - it's similar
to what already happens in some edge cases with authentication.
Re: [OAuth2] Infrastructure for tracking token expiry time
Hi,
Please find the attached first version of the patch providing credential validation framework.
The credential validation framework provides a mechanism to continuously validate authentication credentials during an active session. This enables the server to periodically check credential validity and take appropriate action when credentials expire or become invalid.
Currently, Postgres validates credentials only at connection time. Once authenticated, a session remains active even if:
- A user's rolvaliduntil expiration time passes
- An OAuth bearer token expires
- A user's rolvaliduntil expiration time passes
- An OAuth bearer token expires
Proposed Solution
The patch introduces a credential validation framework that:
1. Periodically checks credential validity during active sessions
2. Terminates sessions when credentials expire or become invalid
Implementation
The framework consists of:
- Core infrastructure (auth-validate.c/h): Manages validation callbacks, dispatches validation checks based on authentication method
- Method implementations (auth-validate-methods.c/h): Contains validators for password-based auth (checks rolvaliduntil in pg_authid) and OAuth
(delegates to validator's expire_cb)
Validation is triggered during query execution in both simple and extended query protocol paths, using a time-based approach to limit overhead.
Configuration
Two new GUC parameters:
credential_validation.enabled = false # enable/disable validation
credential_validation.interval = 1 # check interval in minutes (1-60)
The patch introduces a credential validation framework that:
1. Periodically checks credential validity during active sessions
2. Terminates sessions when credentials expire or become invalid
Implementation
The framework consists of:
- Core infrastructure (auth-validate.c/h): Manages validation callbacks, dispatches validation checks based on authentication method
- Method implementations (auth-validate-methods.c/h): Contains validators for password-based auth (checks rolvaliduntil in pg_authid) and OAuth
(delegates to validator's expire_cb)
Validation is triggered during query execution in both simple and extended query protocol paths, using a time-based approach to limit overhead.
Configuration
Two new GUC parameters:
credential_validation.enabled = false # enable/disable validation
credential_validation.interval = 1 # check interval in minutes (1-60)
Extensibility
New authentication methods can be supported by:
1. Adding an enum value to CredentialValidationType
2. Implementing a validation callback
3. Registering it via RegisterCredentialValidator()
New authentication methods can be supported by:
1. Adding an enum value to CredentialValidationType
2. Implementing a validation callback
3. Registering it via RegisterCredentialValidator()
Should there be per-authentication-method enable/disable settings?
Thanks & Best Regards,
Ajit
On Fri, 20 Feb 2026 at 15:12, Ajit Awekar <ajitpostgres@gmail.com> wrote:
Thanks a lot Daniel, Zslot, Vasuki for your review comments.>The mechanism used is however a secondary discussion,
>first thing to get in place is a design for how to handle mid-connection>credential expiration.
This patch introduces a generic credential validation framework that allows
us to periodically validate authentication credentials during active
database sessions. When enabled, this feature detects expired
credentials and terminates sessions that are no longer valid.
Added GUCs
Credential_validation.enabled = on // Enable or Disable Credential validation
Credential_validation.interval = 120 //Frequency in seconds of running credential validation
The callback mechanism works by:
- Defining a CredentialValidationCallback function pointer type
- Maintaining an array of validators indexed by authentication method
- Allowing other auth mechanisms to register validators via
RegisterCredentialValidator()
- Selecting the appropriate validator at runtime based on the session's
authentication method
The current implementation primarily supports password-based authentication methods, verifying that passwords haven't expired. It can be extended to any authentication method.
This patch is WIP. I am submitting it now to get early feedback on the overall design and approach.
Thanks & Best Regards,AjitOn Wed, 18 Feb 2026 at 22:29, Zsolt Parragi <zsolt.parragi@percona.com> wrote:> but I still think that neither should overload
> what FATAL error means
I see, I misunderstood what you meant by graceful there. In this case,
this is also a good comment for the password expiration thread,
currently that also uses FATAL errors for terminating a connection
when the password expires.
What other option do you see? Something new for this use case like
GoAway, and clients not understanding it simply get disconnected after
some grace period? Or using the recently merged connectionWarning to
send a warning to the client, and disconnect it shortly if it doesn't
do anything to fix the situation?
When I tested the password expiration patch I noticed that deleted
users who still have remaining active connections currently get ERRORs
for every statement that requires permission checks, so in this regard
using ERROR/FATAL for the situation seemed fine to me - it's similar
to what already happens in some edge cases with authentication.
Re: [OAuth2] Infrastructure for tracking token expiry time
Hi All,
credentials during active sessions.
Currently, PostgreSQL validates credentials only at connection time. Once authenticated, a session remains valid even if the underlying
credentials expire (e.g., a user's rolvaliduntil passes, or an OAuth token expires). This can be problematic in environments with strict
security requirements where sessions should be terminated when credentials become invalid.
credentials expire (e.g., a user's rolvaliduntil passes, or an OAuth token expires). This can be problematic in environments with strict
security requirements where sessions should be terminated when credentials become invalid.
This patch introduces a timer-based credential validation mechanism that periodically checks whether a session's authentication credentials are
still valid. When credentials are found to be expired, the session is terminated with an appropriate error message
Key Components
1. Core Framework (auth-validate.c, auth-validate.h)
- Manages validation callbacks for different authentication methods
- Provides a timeout-based scheduler using CREDENTIAL_VALIDATION_TIMEOUT
- Handles validation status (CVS_VALID, CVS_EXPIRED, CVS_ERROR)
2. Validation Methods (auth-validate-methods.c)
- Password validation: checks rolvaliduntil in pg_authid
- OAuth validation: delegates to the validator module's new expire_cb callback
3. GUC Parameters
- credential_validation.enabled (boolean, default: false)
- credential_validation.interval (integer, 1-60 minutes, default: 1)
1. Core Framework (auth-validate.c, auth-validate.h)
- Manages validation callbacks for different authentication methods
- Provides a timeout-based scheduler using CREDENTIAL_VALIDATION_TIMEOUT
- Handles validation status (CVS_VALID, CVS_EXPIRED, CVS_ERROR)
2. Validation Methods (auth-validate-methods.c)
- Password validation: checks rolvaliduntil in pg_authid
- OAuth validation: delegates to the validator module's new expire_cb callback
3. GUC Parameters
- credential_validation.enabled (boolean, default: false)
- credential_validation.interval (integer, 1-60 minutes, default: 1)
OAuth Validator Changes
The patch extends the OAuth validator API with an optional expire_cb callback. To maintain backward compatibility, the magic version has been
bumped to PG_OAUTH_VALIDATOR_MAGIC_V2. Existing V1 validators continue to work; the server simply skips expiration checking for them.
The patch extends the OAuth validator API with an optional expire_cb callback. To maintain backward compatibility, the magic version has been
bumped to PG_OAUTH_VALIDATOR_MAGIC_V2. Existing V1 validators continue to work; the server simply skips expiration checking for them.
Example Configuration
credential_validation.enabled = on
credential_validation.interval = 5 # minutes
credential_validation.enabled = on
credential_validation.interval = 5 # minutes
Thanks & Best Regards,
Ajit
On Mon, 16 Mar 2026 at 19:27, Ajit Awekar <ajitpostgres@gmail.com> wrote:
Hi,Please find the attached first version of the patch providing credential validation framework.The credential validation framework provides a mechanism to continuously validate authentication credentials during an active session. This enables the server to periodically check credential validity and take appropriate action when credentials expire or become invalid.Currently, Postgres validates credentials only at connection time. Once authenticated, a session remains active even if:
- A user's rolvaliduntil expiration time passes
- An OAuth bearer token expiresProposed Solution
The patch introduces a credential validation framework that:
1. Periodically checks credential validity during active sessions
2. Terminates sessions when credentials expire or become invalid
Implementation
The framework consists of:
- Core infrastructure (auth-validate.c/h): Manages validation callbacks, dispatches validation checks based on authentication method
- Method implementations (auth-validate-methods.c/h): Contains validators for password-based auth (checks rolvaliduntil in pg_authid) and OAuth
(delegates to validator's expire_cb)
Validation is triggered during query execution in both simple and extended query protocol paths, using a time-based approach to limit overhead.
Configuration
Two new GUC parameters:
credential_validation.enabled = false # enable/disable validation
credential_validation.interval = 1 # check interval in minutes (1-60)Extensibility
New authentication methods can be supported by:
1. Adding an enum value to CredentialValidationType
2. Implementing a validation callback
3. Registering it via RegisterCredentialValidator()Should there be per-authentication-method enable/disable settings?Thanks & Best Regards,AjitOn Fri, 20 Feb 2026 at 15:12, Ajit Awekar <ajitpostgres@gmail.com> wrote:Thanks a lot Daniel, Zslot, Vasuki for your review comments.>The mechanism used is however a secondary discussion,
>first thing to get in place is a design for how to handle mid-connection>credential expiration.
This patch introduces a generic credential validation framework that allows
us to periodically validate authentication credentials during active
database sessions. When enabled, this feature detects expired
credentials and terminates sessions that are no longer valid.
Added GUCs
Credential_validation.enabled = on // Enable or Disable Credential validation
Credential_validation.interval = 120 //Frequency in seconds of running credential validation
The callback mechanism works by:
- Defining a CredentialValidationCallback function pointer type
- Maintaining an array of validators indexed by authentication method
- Allowing other auth mechanisms to register validators via
RegisterCredentialValidator()
- Selecting the appropriate validator at runtime based on the session's
authentication method
The current implementation primarily supports password-based authentication methods, verifying that passwords haven't expired. It can be extended to any authentication method.
This patch is WIP. I am submitting it now to get early feedback on the overall design and approach.
Thanks & Best Regards,AjitOn Wed, 18 Feb 2026 at 22:29, Zsolt Parragi <zsolt.parragi@percona.com> wrote:> but I still think that neither should overload
> what FATAL error means
I see, I misunderstood what you meant by graceful there. In this case,
this is also a good comment for the password expiration thread,
currently that also uses FATAL errors for terminating a connection
when the password expires.
What other option do you see? Something new for this use case like
GoAway, and clients not understanding it simply get disconnected after
some grace period? Or using the recently merged connectionWarning to
send a warning to the client, and disconnect it shortly if it doesn't
do anything to fix the situation?
When I tested the password expiration patch I noticed that deleted
users who still have remaining active connections currently get ERRORs
for every statement that requires permission checks, so in this regard
using ERROR/FATAL for the situation seemed fine to me - it's similar
to what already happens in some edge cases with authentication.
Re: [OAuth2] Infrastructure for tracking token expiry time
Hi All,
I've added additional test cases to src/test/authentication/t/008_continuous_validation.pl to improve coverage of the continuous credential
validation feature.
validation feature.
Updated the README with:
- Fixed interval parameter
- Added a Testing section documenting all 6 test cases
- Added instructions for running the tests
- Fixed interval parameter
- Added a Testing section documenting all 6 test cases
- Added instructions for running the tests
Thanks & Best Regards,
Ajit
On Fri, 27 Mar 2026 at 16:29, Ajit Awekar <ajitpostgres@gmail.com> wrote:
Hi All,I'd like to propose a patch that adds a credential validation framework to PostgreSQL, enabling periodic re-validation of authenticationcredentials during active sessions.Currently, PostgreSQL validates credentials only at connection time. Once authenticated, a session remains valid even if the underlying
credentials expire (e.g., a user's rolvaliduntil passes, or an OAuth token expires). This can be problematic in environments with strict
security requirements where sessions should be terminated when credentials become invalid.This patch introduces a timer-based credential validation mechanism that periodically checks whether a session's authentication credentials arestill valid. When credentials are found to be expired, the session is terminated with an appropriate error messageKey Components
1. Core Framework (auth-validate.c, auth-validate.h)
- Manages validation callbacks for different authentication methods
- Provides a timeout-based scheduler using CREDENTIAL_VALIDATION_TIMEOUT
- Handles validation status (CVS_VALID, CVS_EXPIRED, CVS_ERROR)
2. Validation Methods (auth-validate-methods.c)
- Password validation: checks rolvaliduntil in pg_authid
- OAuth validation: delegates to the validator module's new expire_cb callback
3. GUC Parameters
- credential_validation.enabled (boolean, default: false)
- credential_validation.interval (integer, 1-60 minutes, default: 1)OAuth Validator Changes
The patch extends the OAuth validator API with an optional expire_cb callback. To maintain backward compatibility, the magic version has been
bumped to PG_OAUTH_VALIDATOR_MAGIC_V2. Existing V1 validators continue to work; the server simply skips expiration checking for them.Example Configuration
credential_validation.enabled = on
credential_validation.interval = 5 # minutesThanks & Best Regards,AjitOn Mon, 16 Mar 2026 at 19:27, Ajit Awekar <ajitpostgres@gmail.com> wrote:Hi,Please find the attached first version of the patch providing credential validation framework.The credential validation framework provides a mechanism to continuously validate authentication credentials during an active session. This enables the server to periodically check credential validity and take appropriate action when credentials expire or become invalid.Currently, Postgres validates credentials only at connection time. Once authenticated, a session remains active even if:
- A user's rolvaliduntil expiration time passes
- An OAuth bearer token expiresProposed Solution
The patch introduces a credential validation framework that:
1. Periodically checks credential validity during active sessions
2. Terminates sessions when credentials expire or become invalid
Implementation
The framework consists of:
- Core infrastructure (auth-validate.c/h): Manages validation callbacks, dispatches validation checks based on authentication method
- Method implementations (auth-validate-methods.c/h): Contains validators for password-based auth (checks rolvaliduntil in pg_authid) and OAuth
(delegates to validator's expire_cb)
Validation is triggered during query execution in both simple and extended query protocol paths, using a time-based approach to limit overhead.
Configuration
Two new GUC parameters:
credential_validation.enabled = false # enable/disable validation
credential_validation.interval = 1 # check interval in minutes (1-60)Extensibility
New authentication methods can be supported by:
1. Adding an enum value to CredentialValidationType
2. Implementing a validation callback
3. Registering it via RegisterCredentialValidator()Should there be per-authentication-method enable/disable settings?Thanks & Best Regards,AjitOn Fri, 20 Feb 2026 at 15:12, Ajit Awekar <ajitpostgres@gmail.com> wrote:Thanks a lot Daniel, Zslot, Vasuki for your review comments.>The mechanism used is however a secondary discussion,
>first thing to get in place is a design for how to handle mid-connection>credential expiration.
This patch introduces a generic credential validation framework that allows
us to periodically validate authentication credentials during active
database sessions. When enabled, this feature detects expired
credentials and terminates sessions that are no longer valid.
Added GUCs
Credential_validation.enabled = on // Enable or Disable Credential validation
Credential_validation.interval = 120 //Frequency in seconds of running credential validation
The callback mechanism works by:
- Defining a CredentialValidationCallback function pointer type
- Maintaining an array of validators indexed by authentication method
- Allowing other auth mechanisms to register validators via
RegisterCredentialValidator()
- Selecting the appropriate validator at runtime based on the session's
authentication method
The current implementation primarily supports password-based authentication methods, verifying that passwords haven't expired. It can be extended to any authentication method.
This patch is WIP. I am submitting it now to get early feedback on the overall design and approach.
Thanks & Best Regards,AjitOn Wed, 18 Feb 2026 at 22:29, Zsolt Parragi <zsolt.parragi@percona.com> wrote:> but I still think that neither should overload
> what FATAL error means
I see, I misunderstood what you meant by graceful there. In this case,
this is also a good comment for the password expiration thread,
currently that also uses FATAL errors for terminating a connection
when the password expires.
What other option do you see? Something new for this use case like
GoAway, and clients not understanding it simply get disconnected after
some grace period? Or using the recently merged connectionWarning to
send a warning to the client, and disconnect it shortly if it doesn't
do anything to fix the situation?
When I tested the password expiration patch I noticed that deleted
users who still have remaining active connections currently get ERRORs
for every statement that requires permission checks, so in this regard
using ERROR/FATAL for the situation seemed fine to me - it's similar
to what already happens in some edge cases with authentication.