Skip to content

Commit a985fc3

Browse files
authored
Document the non-periodic password provider (#346)
1 parent 8e34863 commit a985fc3

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

conceptual/Npgsql/security.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ If a `Password` is not specified and your PostgreSQL is configured to request a
88

99
### Auth token rotation and dynamic password
1010

11-
In some cloud scenarios, logging into PostgreSQL is done with an auth token that is rotated every time interval (e.g. one hour). Starting with version 7.0, Npgsql has a built-in periodic password provider mechanism, which allows refreshing the password with zero effort:
11+
In some cloud scenarios, logging into PostgreSQL is done with an auth token that is rotated every time interval (e.g. one hour). Npgsql has a built-in periodic password provider mechanism, which allows using an up-to-date access token with zero effort:
1212

1313
```csharp
1414
var dataSourceBuilder = new NpgsqlDataSourceBuilder(...);
15+
dataSourceBuilder.UsePasswordProvider(
16+
passwordProvider: _ => throw new NotSupportedException(),
17+
passwordProviderAsync: (builder, token) => /* code to fetch the new access token */);
18+
await using var dataSource = dataSourceBuilder.Build();
19+
```
20+
21+
Every time a new physical connection needs to be opened to PostgreSQL, either the synchronous `passwordProvider` or the asynchronous `passwordProviderAsync` will be called (depending whether you used `Open()` or `OpenAsync()`). Since modern .NET applications are encouraged to always use synchronous I/O, it's good practice to simply throw in the synchronous password provider, as above.
22+
23+
Note that since the password provider is invoked *every* time a physical connection is opened, it shouldn't take too long; typically, this would call into a cloud provider API (e.g. Azure Managed Identity), which itself implements a caching mechanism. However, if no such caching is done and the code could take a while, you can instead instruct Npgsql to cache the auth token for a given amount of time:
24+
25+
```csharp
1526
dataSourceBuilder.UsePeriodicPasswordProvider(
1627
(settings, cancellationToken) => /* async code to fetch the new access token */,
1728
TimeSpan.FromMinutes(55), // Interval for refreshing the token
1829
TimeSpan.FromSeconds(5)); // Interval for retrying after a refresh failure
19-
await using var dataSource = dataSourceBuilder.Build();
2030
```
2131

22-
This API allows you to provide a minimal async code fragment for fetching the latest auth token, and have Npgsql take care of running it for you as needed.
23-
24-
If, instead, you prefer to manage this yourself, you can simply inject a new password at any time into a working data source:
32+
Finally, if you already have code running when the auth token changes, you can simply inject it manually at any time into a working data source:
2533

2634
```csharp
2735
dataSource.Password = <new password>;

0 commit comments

Comments
 (0)