Skip to content

Commit c6c5c49

Browse files
authored
Merge pull request #4 from zitadel/improve-auth-support
Adding support for all Zitadel authentication strategies
2 parents c0ab98f + 87c09e3 commit c6c5c49

36 files changed

+2385
-1569
lines changed

.github/workflows/integration.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
branches:
66
- main
7+
pull_request:
8+
branches:
9+
- main
710

811
jobs:
912
check-compatibility:

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ jobs:
3232

3333
- name: Run Tests
3434
run: poetry run pytest
35+
env:
36+
BASE_URL: ${{ secrets.BASE_URL }}
37+
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
38+
JWT_KEY: ${{ secrets.JWT_KEY }}
39+
CLIENT_ID: ${{ secrets.CLIENT_ID }}
40+
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}

.openapi-generator-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ poetry.lock
1717
requirements.txt
1818
test-requirements.txt
1919
tox.ini
20+
test/*.py

README.md

Lines changed: 113 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,133 @@ install dependencies.
3737
Install the SDK by running one of the following commands:
3838

3939
```bash
40-
composer require zitadel/client
40+
pip install zitadel_client
4141
```
4242

43-
### Authentication
43+
## Authentication Methods
4444

45-
The SDK supports three authentication methods:
45+
Your SDK offers three ways to authenticate with Zitadel. Each method has its
46+
own benefits—choose the one that fits your situation best.
4647

47-
1. Private Key JWT Authentication
48-
2. Client Credentials Grant
49-
3. Personal Access Tokens (PATs)
48+
#### 1. Private Key JWT Authentication
5049

51-
For most service user scenarios in Zitadel, private key JWT authentication
52-
is the recommended choice due to its benefits in security, performance, and control.
53-
However, client credentials authentication might be considered in specific
54-
situations where simplicity and trust between servers are priorities.
50+
**What is it?**
51+
You use a JSON Web Token (JWT) that you sign with a private key stored in a
52+
JSON file. This process creates a secure token.
5553

56-
For more details on these authentication methods, please refer
57-
to the [Zitadel documentation on authenticating service users](https://zitadel.com/docs/guides/integrate/service-users/authenticate-service-users).
54+
**When should you use it?**
55+
- **Best for production:** It offers strong security.
56+
- **Advanced control:** You can adjust token settings like expiration.
5857

58+
**How do you use it?**
59+
1. Save your private key in a JSON file.
60+
2. Use the provided method to load this key and create a JWT-based
61+
authenticator.
5962

60-
### Example
63+
**Example:**
6164

6265
```python
6366
import zitadel_client as zitadel
67+
from zitadel_client.auth.web_token_authenticator import WebTokenAuthenticator
68+
69+
base_url = "https://example.zitadel.com"
70+
key_file = "/path/to/jwt-key.json"
71+
72+
authenticator = WebTokenAuthenticator.from_json(base_url, key_file)
73+
zitadel = zitadel.Zitadel(authenticator)
74+
75+
try:
76+
response = zitadel.users.add_human_user({
77+
"username": "john.doe",
78+
"profile": {"givenName": "John", "familyName": "Doe"},
79+
"email": {"email": "john@doe.com"}
80+
})
81+
print("User created:", response)
82+
except Exception as e:
83+
print("Error:", e)
84+
```
85+
86+
#### 2. Client Credentials Grant
87+
88+
**What is it?**
89+
This method uses a client ID and client secret to get a secure access token,
90+
which is then used to authenticate.
91+
92+
**When should you use it?**
93+
- **Simple and straightforward:** Good for server-to-server communication.
94+
- **Trusted environments:** Use it when both servers are owned or trusted.
95+
96+
**How do you use it?**
97+
1. Provide your client ID and client secret.
98+
2. Build the authenticator
6499

65-
with zitadel.Zitadel("your-zitadel-base-url", 'your-valid-token') as client:
66-
try:
67-
response = client.users.add_human_user(
68-
body=zitadel.V2AddHumanUserRequest(
69-
username="john.doe",
70-
profile=zitadel.V2SetHumanProfile(given_name="John", family_name="Doe"),
71-
email=zitadel.V2SetHumanEmail(email="johndoe@doe.com")
72-
)
73-
)
74-
print("User created:", response)
75-
except Exception as e:
76-
raise e
100+
**Example:**
101+
102+
```python
103+
import zitadel_client as zitadel
104+
from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator
105+
106+
base_url = "https://example.zitadel.com"
107+
client_id = "your-client-id"
108+
client_secret = "your-client-secret"
109+
110+
authenticator = ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()
111+
zitadel = zitadel.Zitadel(authenticator)
112+
113+
try:
114+
response = zitadel.users.add_human_user({
115+
"username": "john.doe",
116+
"profile": {"givenName": "John", "familyName": "Doe"},
117+
"email": {"email": "john@doe.com"}
118+
})
119+
print("User created:", response)
120+
except Exception as e:
121+
print("Error:", e)
77122
```
78123

124+
#### 3. Personal Access Tokens (PATs)
125+
126+
**What is it?**
127+
A Personal Access Token (PAT) is a pre-generated token that you can use to
128+
authenticate without exchanging credentials every time.
129+
130+
**When should you use it?**
131+
- **Easy to use:** Great for development or testing scenarios.
132+
- **Quick setup:** No need for dynamic token generation.
133+
134+
**How do you use it?**
135+
1. Obtain a valid personal access token from your account.
136+
2. Create the authenticator with: `PersonalAccessTokenAuthenticator`
137+
138+
**Example:**
139+
140+
```python
141+
import zitadel_client as zitadel
142+
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator
143+
144+
base_url = "https://example.zitadel.com"
145+
valid_token = "your-valid-token"
146+
147+
authenticator = PersonalAccessTokenAuthenticator(base_url, valid_token)
148+
zitadel = zitadel.Zitadel(authenticator)
149+
150+
try:
151+
response = zitadel.users.add_human_user({
152+
"username": "john.doe",
153+
"profile": {"givenName": "John", "familyName": "Doe"},
154+
"email": {"email": "john@doe.com"}
155+
})
156+
print("User created:", response)
157+
except Exception as e:
158+
print("Error:", e)
159+
```
160+
161+
---
162+
163+
Choose the authentication method that best suits your needs based on your
164+
environment and security requirements. For more details, please refer to the
165+
[Zitadel documentation on authenticating service users](https://zitadel.com/docs/guides/integrate/service-users/authenticate-service-users).
166+
79167
### Debugging
80168
The SDK supports debug logging, which can be enabled for troubleshooting
81169
and debugging purposes. You can enable debug logging by setting the `debug`

0 commit comments

Comments
 (0)