Skip to content

Commit f24ae46

Browse files
committed
Describe all authentication methods in docs
1 parent d36e407 commit f24ae46

File tree

3 files changed

+140
-6
lines changed

3 files changed

+140
-6
lines changed

docs/examples.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ Examples
44
.. toctree::
55
:maxdepth: 3
66

7-
examples/basic_example
7+
examples/basic_example
8+
examples/authentication

docs/examples/authentication.rst

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
Authentication
2+
==============
3+
4+
There are several ways to authenticate through YDB Python SDK.
5+
6+
Anonymous Credentials
7+
---------------------
8+
9+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/anonymous-credentials>`_.
10+
11+
.. code-block:: python
12+
13+
driver = ydb.Driver(
14+
endpoint=os.getenv("YDB_ENDPOINT"),
15+
database=os.getenv("YDB_DATABASE"),
16+
credentials=ydb.AnonymousCredentials(),
17+
)
18+
19+
20+
Access Token Credentials
21+
------------------------
22+
23+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/access-token-credentials>`_.
24+
25+
.. code-block:: python
26+
27+
driver = ydb.Driver(
28+
endpoint=os.getenv("YDB_ENDPOINT"),
29+
database=os.getenv("YDB_DATABASE"),
30+
credentials=ydb.AccessTokenCredentials(os.getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
31+
)
32+
33+
34+
Static Credentials (Legacy)
35+
---------------------------
36+
This method is legacy, use UserPasswordCredentials instead.
37+
38+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/static-credentials>`_.
39+
40+
41+
.. code-block:: python
42+
43+
driver_config = ydb.DriverConfig(
44+
endpoint=endpoint,
45+
database=database,
46+
)
47+
creds = ydb.StaticCredentials(
48+
driver_config=driver_config,
49+
user=user,
50+
password=password,
51+
)
52+
53+
driver = ydb.Driver(
54+
driver_config=driver_config,
55+
credentials=creds,
56+
)
57+
58+
59+
User Password Credentials
60+
-------------------------
61+
62+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/static-credentials>`_.
63+
64+
.. code-block:: python
65+
66+
driver_config = ydb.DriverConfig(
67+
endpoint=endpoint,
68+
database=database,
69+
credentials=ydb.UserPasswordCredentials(
70+
user=user,
71+
password=password,
72+
endpoint=endpoint,
73+
database=database,
74+
),
75+
)
76+
77+
driver = ydb.Driver(driver_config=driver_config)
78+
79+
80+
Service Accaount Credentials
81+
----------------------------
82+
83+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/service-account-credentials>`_.
84+
85+
.. code-block:: python
86+
87+
driver = ydb.Driver(
88+
endpoint=os.getenv("YDB_ENDPOINT"),
89+
database=os.getenv("YDB_DATABASE"),
90+
credentials=ydb.iam.ServiceAccountCredentials.from_file(
91+
os.getenv("SA_KEY_FILE"),
92+
),
93+
)
94+
95+
96+
OAuth 2.0 Token Exchange Credentials
97+
------------------------------------
98+
99+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/oauth2-token-exchange-credentials>`_.
100+
101+
.. code-block:: python
102+
103+
driver = ydb.Driver(
104+
endpoint=args.endpoint,
105+
database=args.database,
106+
root_certificates=ydb.load_ydb_root_certificate(),
107+
credentials=ydb.oauth2_token_exchange.Oauth2TokenExchangeCredentials(
108+
token_endpoint=args.token_endpoint,
109+
audience=args.audience,
110+
subject_token_source=ydb.oauth2_token_exchange.JwtTokenSource(
111+
signing_method="RS256",
112+
private_key_file=args.private_key_file,
113+
key_id=args.key_id,
114+
issuer=args.issuer,
115+
subject=args.subject,
116+
audience=args.audience,
117+
),
118+
),
119+
)
120+
121+
122+
Metadata Credentials
123+
--------------------
124+
125+
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/metadata-credentials>`_.
126+
127+
.. code-block:: python
128+
129+
driver = ydb.Driver(
130+
endpoint=os.getenv("YDB_ENDPOINT"),
131+
database=os.getenv("YDB_DATABASE"),
132+
credentials=ydb.iam.MetadataUrlCredentials(),
133+
)

examples/static-credentials/example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def auth_with_static_credentials_old(endpoint: str, database: str, user: str, pa
3131

3232
def auth_with_static_credentials_new(endpoint: str, database: str, user: str, password: str):
3333
driver_config = ydb.DriverConfig(
34-
endpoint,
35-
database,
34+
endpoint=endpoint,
35+
database=database,
3636
)
3737
creds = ydb.StaticCredentials(
38-
driver_config,
39-
user,
40-
password,
38+
driver_config=driver_config,
39+
user=user,
40+
password=password,
4141
)
4242

4343
with ydb.Driver(driver_config=driver_config, credentials=creds) as driver:

0 commit comments

Comments
 (0)