@@ -217,6 +217,10 @@ the [`JWT` authentication type](https://trino.io/docs/current/security/jwt.html)
217217
218218# ## OAuth2 authentication
219219
220+ Make sure that the OAuth2 support is installed using `pip install trino[oauth]` .
221+
222+ # ### Interactive Browser authentication
223+
220224The `OAuth2Authentication` class can be used to connect to a Trino cluster configured with
221225the [OAuth2 authentication type ](https:// trino.io/ docs/ current/ security/ oauth2.html).
222226
@@ -248,14 +252,127 @@ The OAuth2 token will be cached either per `trino.auth.OAuth2Authentication` ins
248252 from trino.auth import OAuth2Authentication
249253
250254 engine = create_engine(
251- " trino://<username>@<host>:<port>/<catalog>" ,
255+ " trino://<username>@<host>:<port>/<catalog>" ,
252256 connect_args = {
253257 " auth" : OAuth2Authentication(),
254258 " http_scheme" : " https" ,
255259 }
256260 )
257261 ```
258262
263+ # ### Client Credentials authentication
264+
265+ ```python
266+ from trino.dbapi import connect
267+ from trino.auth import ClientCredentials
268+ from trino.oauth2.models import OidcConfig
269+
270+ auth = ClientCredentials(
271+ client_id = " <client_id>" ,
272+ client_secret = " <client_secret>" ,
273+ url_config = OidcConfig(
274+ token_endpoint = " <token_endpoint>" ,
275+ # other endpoints if needed
276+ ),
277+ scope = " <number of scopes>" , # optional
278+ audience = " <audience>" , # optional
279+ )
280+
281+ conn = connect(
282+ user = " <username>" ,
283+ auth = auth,
284+ http_scheme = " https" ,
285+ ...
286+ )
287+ ```
288+
289+ #### Device Code authentication
290+
291+ ``` python
292+ from trino.dbapi import connect
293+ from trino.auth import DeviceCode
294+ from trino.oauth2.models import OidcConfig
295+
296+ auth = DeviceCode(
297+ client_id = " <client_id>" ,
298+ url_config = OidcConfig(
299+ token_endpoint = " <token_endpoint>" ,
300+ device_authorization_endpoint = " <device_authorization_endpoint>" ,
301+ ),
302+ scope = " <scope>" , # optional
303+ audience = " <audience>" , # optional
304+ )
305+
306+ conn = connect(
307+ user = " <username>" ,
308+ auth = auth,
309+ http_scheme = " https" ,
310+ ...
311+ )
312+ ```
313+
314+ #### Authorization Code authentication
315+
316+ ``` python
317+ from trino.dbapi import connect
318+ from trino.auth import AuthorizationCode
319+ from trino.oauth2.models import OidcConfig
320+
321+ auth = AuthorizationCode(
322+ client_id = " <client_id>" ,
323+ client_secret = " <client_secret>" , # optional
324+ url_config = OidcConfig(
325+ token_endpoint = " <token_endpoint>" ,
326+ authorization_endpoint = " <authorization_endpoint>" ,
327+ ),
328+ scope = " <scope>" , # optional
329+ audience = " <audience>" , # optional
330+ )
331+
332+ conn = connect(
333+ user = " <username>" ,
334+ auth = auth,
335+ http_scheme = " https" ,
336+ ...
337+ )
338+ ```
339+
340+ ### Reference
341+
342+ For further details, please consult [ Trino documentation] ( https://trino.io/docs/current ) .
343+
344+ ### Secure Token Storage
345+
346+ By default all ClientCredentials, DeviceCode, AuthorizationCode JWT tokens are securely storaged
347+ using the keyrings.cryptfile feature of [ keyring library] ( https://pypi.org/project/keyring/ ) .
348+
349+ Tokens are stored encrypted at ~ /.local/share/python_keyring/cryptfile_pass.cfg
350+
351+ You can optionally use different keyring backends by supplying the ` PYTHON_KEYRING_BACKEND ` environment variable.
352+
353+ To use an encrypted file backend for credentials:
354+
355+ ``` bash
356+ export KEYRING_CRYPTFILE_PASSWORD=your_secure_password
357+ ```
358+
359+ Or you can pass the password directly (less secure):
360+
361+ ``` python
362+ conn = connect(
363+ host = " trino.example.com" ,
364+ port = 443 ,
365+ auth = DeviceCode(
366+ client_id = " <CLIENT_ID>" ,
367+ client_secret = " <CLIENT_SECRET>" ,
368+ url_config = OidcConfig(oidc_discovery_url = " https://sso.example.com/.well-known/openid-configuration" ),
369+ token_storage_password = " your_secure_password" # less secure
370+ ),
371+ http_scheme = " https"
372+ )
373+ ```
374+
375+
259376### Certificate authentication
260377
261378` CertificateAuthentication ` class can be used to connect to Trino cluster configured with [ certificate based authentication] ( https://trino.io/docs/current/security/certificate.html ) . ` CertificateAuthentication ` requires paths to a valid client certificate and private key.
0 commit comments