Skip to content

Commit e748b4e

Browse files
authored
Cleaned up the public API and updated the code examples (#12)
* Cleaned up the public API and updated the code examples * Updated the specificiation tests to use the public API * Updated the README
1 parent 1e0618e commit e748b4e

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

README.md

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,8 @@ JSON file. This process creates a secure token.
6969

7070
```python
7171
import zitadel_client as zitadel
72-
from zitadel_client.auth.web_token_authenticator import WebTokenAuthenticator
7372

74-
base_url = "https://example.zitadel.com"
75-
key_file = "/path/to/jwt-key.json"
76-
77-
authenticator = WebTokenAuthenticator.from_json(base_url, key_file)
78-
zitadel = zitadel.Zitadel(authenticator)
73+
zitadel = zitadel.Zitadel.with_private_key("https://example.us1.zitadel.cloud", "path/to/jwt-key.json")
7974

8075
try:
8176
response = zitadel.users.add_human_user({
@@ -108,14 +103,8 @@ which is then used to authenticate.
108103

109104
```python
110105
import zitadel_client as zitadel
111-
from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator
112-
113-
base_url = "https://example.zitadel.com"
114-
client_id = "your-client-id"
115-
client_secret = "your-client-secret"
116106

117-
authenticator = ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()
118-
zitadel = zitadel.Zitadel(authenticator)
107+
zitadel = zitadel.Zitadel.with_client_credentials("https://example.us1.zitadel.cloud", "id", "secret")
119108

120109
try:
121110
response = zitadel.users.add_human_user({
@@ -148,13 +137,8 @@ authenticate without exchanging credentials every time.
148137

149138
```python
150139
import zitadel_client as zitadel
151-
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
152-
153-
base_url = "https://example.zitadel.com"
154-
valid_token = "your-valid-token"
155140

156-
authenticator = PersonalAccessTokenAuthenticator(base_url, valid_token)
157-
zitadel = zitadel.Zitadel(authenticator)
141+
zitadel = zitadel.Zitadel.with_access_token("https://example.us1.zitadel.cloud", "token")
158142

159143
try:
160144
response = zitadel.users.add_human_user({

spec/sdk_test_using_client_credentials_authentication_spec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def base_url() -> str | None:
3030
@pytest.fixture
3131
def user_id(client_id: str, client_secret: str, base_url: str) -> str | None:
3232
"""Fixture to create a user and return their ID."""
33-
with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()) as client:
33+
with zitadel.Zitadel.with_client_credentials(base_url, client_id, client_secret) as client:
3434
try:
3535
response = client.users.add_human_user(
3636
body=zitadel.models.V2AddHumanUserRequest(
@@ -48,7 +48,7 @@ def test_should_deactivate_and_reactivate_user_with_valid_token(
4848
user_id: str, client_id: str, client_secret: str, base_url: str
4949
) -> None:
5050
"""Test to (de)activate the user with a valid token."""
51-
with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()) as client:
51+
with zitadel.Zitadel.with_client_credentials(base_url, client_id, client_secret) as client:
5252
try:
5353
deactivate_response = client.users.deactivate_user(user_id=user_id)
5454
assert deactivate_response is not None, "Deactivation response is None"
@@ -61,7 +61,7 @@ def test_should_deactivate_and_reactivate_user_with_valid_token(
6161

6262
def test_should_not_deactivate_or_reactivate_user_with_invalid_token(user_id: str, base_url: str) -> None:
6363
"""Test to attempt (de)activating the user with an invalid token."""
64-
with zitadel.Zitadel(ClientCredentialsAuthenticator.builder(base_url, "id", "secret").build()) as client:
64+
with zitadel.Zitadel.with_client_credentials(base_url, "id", "secret") as client:
6565
with pytest.raises(Exception, match="Failed to refresh token: invalid_client: client not found"):
6666
client.users.deactivate_user(user_id=user_id)
6767

spec/sdk_test_using_personal_access_token_authentication_spec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def base_url() -> str | None:
3131
@pytest.fixture
3232
def user_id(valid_token: str, base_url: str) -> str | None:
3333
"""Fixture to create a user and return their ID."""
34-
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, valid_token)) as client:
34+
with zitadel.Zitadel.with_access_token(base_url, valid_token) as client:
3535
try:
3636
response = client.users.add_human_user(
3737
body=zitadel.models.V2AddHumanUserRequest(
@@ -47,7 +47,7 @@ def user_id(valid_token: str, base_url: str) -> str | None:
4747

4848
def test_should_deactivate_and_reactivate_user_with_valid_token(user_id: str, valid_token: str, base_url: str) -> None:
4949
"""Test to (de)activate the user with a valid token."""
50-
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, valid_token)) as client:
50+
with zitadel.Zitadel.with_access_token(base_url, valid_token) as client:
5151
try:
5252
deactivate_response = client.users.deactivate_user(user_id=user_id)
5353
assert deactivate_response is not None, "Deactivation response is None"
@@ -60,7 +60,7 @@ def test_should_deactivate_and_reactivate_user_with_valid_token(user_id: str, va
6060

6161
def test_should_not_deactivate_or_reactivate_user_with_invalid_token(user_id: str, invalid_token: str, base_url: str) -> None:
6262
"""Test to attempt (de)activating the user with an invalid token."""
63-
with zitadel.Zitadel(PersonalAccessTokenAuthenticator(base_url, invalid_token)) as client:
63+
with zitadel.Zitadel.with_access_token(base_url, invalid_token) as client:
6464
with pytest.raises(UnauthorizedException):
6565
client.users.deactivate_user(user_id=user_id)
6666

spec/sdk_test_using_web_token_authentication_spec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def base_url() -> str | None:
2727
@pytest.fixture
2828
def user_id(key_file: str, base_url: str) -> str | None:
2929
"""Fixture to create a user and return their ID."""
30-
with zitadel.Zitadel(WebTokenAuthenticator.from_json(base_url, key_file)) as client:
30+
with zitadel.Zitadel.with_private_key(base_url, key_file) as client:
3131
try:
3232
response = client.users.add_human_user(
3333
body=zitadel.models.V2AddHumanUserRequest(
@@ -43,7 +43,7 @@ def user_id(key_file: str, base_url: str) -> str | None:
4343

4444
def test_should_deactivate_and_reactivate_user_with_valid_token(user_id: str, key_file: str, base_url: str) -> None:
4545
"""Test to (de)activate the user with a valid token."""
46-
with zitadel.Zitadel(WebTokenAuthenticator.from_json(base_url, key_file)) as client:
46+
with zitadel.Zitadel.with_private_key(base_url, key_file) as client:
4747
try:
4848
deactivate_response = client.users.deactivate_user(user_id=user_id)
4949
assert deactivate_response is not None, "Deactivation response is None"

zitadel_client/zitadel.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from zitadel_client.api.user_service_api import UserServiceApi
1111
from zitadel_client.api_client import ApiClient
1212
from zitadel_client.auth.authenticator import Authenticator
13+
from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator
14+
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
15+
from zitadel_client.auth.web_token_authenticator import WebTokenAuthenticator
1316
from zitadel_client.configuration import Configuration
1417

1518

@@ -93,3 +96,40 @@ def __exit__(
9396
traceback: The traceback of the exception, if an exception occurred.
9497
"""
9598
pass
99+
100+
@staticmethod
101+
def with_access_token(host: str, access_token: str) -> "Zitadel":
102+
"""
103+
Initialize the SDK with a Personal Access Token (PAT).
104+
105+
:param host: API URL (e.g., "https://api.zitadel.example.com").
106+
:param access_token: Personal Access Token for Bearer authentication.
107+
:return: Configured Zitadel client instance.
108+
:see: https://zitadel.com/docs/guides/integrate/service-users/personal-access-token
109+
"""
110+
return Zitadel(PersonalAccessTokenAuthenticator(host, access_token))
111+
112+
@staticmethod
113+
def with_client_credentials(host: str, client_id: str, client_secret: str) -> "Zitadel":
114+
"""
115+
Initialize the SDK using OAuth2 Client Credentials flow.
116+
117+
:param host: API URL.
118+
:param client_id: OAuth2 client identifier.
119+
:param client_secret: OAuth2 client secret.
120+
:return: Configured Zitadel client instance with token auto-refresh.
121+
:see: https://zitadel.com/docs/guides/integrate/service-users/client-credentials
122+
"""
123+
return Zitadel(ClientCredentialsAuthenticator.builder(host, client_id, client_secret).build())
124+
125+
@staticmethod
126+
def with_private_key(host: str, key_file: str) -> "Zitadel":
127+
"""
128+
Initialize the SDK via Private Key JWT assertion.
129+
130+
:param host: API URL.
131+
:param key_file: Path to service account JSON or PEM key file.
132+
:return: Configured Zitadel client instance using JWT assertion.
133+
:see: https://zitadel.com/docs/guides/integrate/service-users/private-key-jwt
134+
"""
135+
return Zitadel(WebTokenAuthenticator.from_json(host, key_file))

0 commit comments

Comments
 (0)