Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
295ecf7
Intial commit for adding support for other authentication strategies
mridang Apr 3, 2025
c8ef007
Intial commit for adding support for other authentication strategies
mridang Apr 7, 2025
654e8ea
Added some tests to ensure that the auth works
mridang Apr 7, 2025
316f0a8
Added some tests to ensure that the auth works
mridang Apr 7, 2025
7c020af
Hardened the test suite to ensure that all code paths are covered
mridang Apr 7, 2025
e437123
Fixed the builder method to ensure that the type erasure is not lost
mridang Apr 7, 2025
6d842b9
Renamed the builder class
mridang Apr 8, 2025
8edbd3a
Fixed the Pytest configuration to generate the LCOV report too
mridang Apr 9, 2025
7a46fac
Fixed the import to point to the correct authenticator as the dummy o…
mridang Apr 9, 2025
4b7d769
Updated the coverage configuration to not pollute the project root
mridang Apr 9, 2025
3587295
Updated the configuration to exlcuded auto-generated code from coverage
mridang Apr 9, 2025
418c35b
Added a test to ensure that the hostname is correct and that the auth…
mridang Apr 9, 2025
8c08944
Minor code cleanup
mridang Apr 9, 2025
25b1042
Updated the workflow to also run on pull-requests
mridang Apr 9, 2025
ae96bd9
Added more integration tests to ensure that this works as expected
mridang Apr 10, 2025
07616a4
Passed the token env vars to the test runner to that integration test…
mridang Apr 10, 2025
41a15fe
Added a method to build the webtoken authenticator from a key file
mridang Apr 11, 2025
1ae085e
Merge branch 'main' into improve-auth-support
mridang Apr 11, 2025
79ba19b
Removed the hardcoding of all the imports in the __init__.py as it is…
mridang Apr 12, 2025
99e9e09
Added tests to ensure that the JWT token based authentication is work…
mridang Apr 14, 2025
b0db237
Added tests to ensure that the client-credentials authentication is …
mridang Apr 14, 2025
87c09e3
Updated the Readme to include examples of the different auth methods.
mridang Apr 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
check-compatibility:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ jobs:

- name: Run Tests
run: poetry run pytest
env:
BASE_URL: ${{ secrets.BASE_URL }}
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
JWT_KEY: ${{ secrets.JWT_KEY }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
1 change: 1 addition & 0 deletions .openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ poetry.lock
requirements.txt
test-requirements.txt
tox.ini
test/*.py
138 changes: 113 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,133 @@ install dependencies.
Install the SDK by running one of the following commands:

```bash
composer require zitadel/client
pip install zitadel_client
```

### Authentication
## Authentication Methods

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

1. Private Key JWT Authentication
2. Client Credentials Grant
3. Personal Access Tokens (PATs)
#### 1. Private Key JWT Authentication

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

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

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

### Example
**Example:**

```python
import zitadel_client as zitadel
from zitadel_client.auth.web_token_authenticator import WebTokenAuthenticator

base_url = "https://example.zitadel.com"
key_file = "/path/to/jwt-key.json"

authenticator = WebTokenAuthenticator.from_json(base_url, key_file)
zitadel = zitadel.Zitadel(authenticator)

try:
response = zitadel.users.add_human_user({
"username": "john.doe",
"profile": {"givenName": "John", "familyName": "Doe"},
"email": {"email": "john@doe.com"}
})
print("User created:", response)
except Exception as e:
print("Error:", e)
```

#### 2. Client Credentials Grant

**What is it?**
This method uses a client ID and client secret to get a secure access token,
which is then used to authenticate.

**When should you use it?**
- **Simple and straightforward:** Good for server-to-server communication.
- **Trusted environments:** Use it when both servers are owned or trusted.

**How do you use it?**
1. Provide your client ID and client secret.
2. Build the authenticator

with zitadel.Zitadel("your-zitadel-base-url", 'your-valid-token') as client:
try:
response = client.users.add_human_user(
body=zitadel.V2AddHumanUserRequest(
username="john.doe",
profile=zitadel.V2SetHumanProfile(given_name="John", family_name="Doe"),
email=zitadel.V2SetHumanEmail(email="johndoe@doe.com")
)
)
print("User created:", response)
except Exception as e:
raise e
**Example:**

```python
import zitadel_client as zitadel
from zitadel_client.auth.client_credentials_authenticator import ClientCredentialsAuthenticator

base_url = "https://example.zitadel.com"
client_id = "your-client-id"
client_secret = "your-client-secret"

authenticator = ClientCredentialsAuthenticator.builder(base_url, client_id, client_secret).build()
zitadel = zitadel.Zitadel(authenticator)

try:
response = zitadel.users.add_human_user({
"username": "john.doe",
"profile": {"givenName": "John", "familyName": "Doe"},
"email": {"email": "john@doe.com"}
})
print("User created:", response)
except Exception as e:
print("Error:", e)
```

#### 3. Personal Access Tokens (PATs)

**What is it?**
A Personal Access Token (PAT) is a pre-generated token that you can use to
authenticate without exchanging credentials every time.

**When should you use it?**
- **Easy to use:** Great for development or testing scenarios.
- **Quick setup:** No need for dynamic token generation.

**How do you use it?**
1. Obtain a valid personal access token from your account.
2. Create the authenticator with: `PersonalAccessTokenAuthenticator`

**Example:**

```python
import zitadel_client as zitadel
from zitadel_client.auth.personal_access_token_authenticator import PersonalAccessTokenAuthenticator

base_url = "https://example.zitadel.com"
valid_token = "your-valid-token"

authenticator = PersonalAccessTokenAuthenticator(base_url, valid_token)
zitadel = zitadel.Zitadel(authenticator)

try:
response = zitadel.users.add_human_user({
"username": "john.doe",
"profile": {"givenName": "John", "familyName": "Doe"},
"email": {"email": "john@doe.com"}
})
print("User created:", response)
except Exception as e:
print("Error:", e)
```

---

Choose the authentication method that best suits your needs based on your
environment and security requirements. For more details, please refer to the
[Zitadel documentation on authenticating service users](https://zitadel.com/docs/guides/integrate/service-users/authenticate-service-users).

### Debugging
The SDK supports debug logging, which can be enabled for troubleshooting
and debugging purposes. You can enable debug logging by setting the `debug`
Expand Down
Loading