Metadata-Version: 2.5
Name: aiveth
Version: 0.23.0
Summary: Official Aiveth SDK for Python: verify ID, access, machine and AI agent tokens, verify webhooks, and manage your organization's people with the admin API.
Author: Aiveth team
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: aiveth,auth,authentication,jwt,oauth,oidc,sso,webhooks
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27
Requires-Dist: pyjwt[crypto]>=2.9
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# aiveth

Aiveth for Python (3.10 or later).

```python
from aiveth import Aiveth, verify_webhook

aiveth = Aiveth("https://aiveth.example.com", api_key=os.environ["AIVETH_API_KEY"])

claims = aiveth.tokens.verify_id_token(id_token, client_id="your-app-client-id")
user = aiveth.tokens.user_from_access_token(access_token)  # None once it has ended
machine = aiveth.tokens.verify_machine_token(token, org_id="your-organization-id")

event = verify_webhook(request.body, request.headers, os.environ["AIVETH_WEBHOOK_SECRET"])

people = aiveth.people.list()
aiveth.invitations.send("new.person@example.com")
waiting = aiveth.join_requests.list("waitlist")  # approve(id) emails an invitation
key = aiveth.app_keys.verify(key_from_the_request)  # valid, owner, permissions
report = aiveth.people.import_all(people_from_your_old_system)  # each with external_id
# report["people"]: every person's Aiveth id next to your old id, to link your records
asha = aiveth.people.find_by_external_id("user_2abc")
```

Every refusal is a `AivethError` with Aiveth's own words and `status`.

A webhook may arrive twice within its 5 minutes: keep the `webhook-id`s you've handled and skip repeats.

## Workspaces (your own customers inside your app)

```python
made = aiveth.workspaces.create("Acme", external_id="org_123", creator_id=user_id)
aiveth.workspaces.invite(made["workspace"]["id"], "teammate@acme.com", role="member")
```

## Check a token in your own API

Register your API in the console (Developers → Your APIs). Apps that ask for it get a signed access
token; your API checks it here, with no call to Aiveth:

```python
who = aiveth.tokens.verify_access_token(bearer, audience="https://api.acme.com", permissions=("invoices:read",))
# who["sub"], who["org_id"], who["role"], who["permissions"]
```

## Protect an MCP server (AI agents)

Register your MCP server in the console (AI agents → MCP servers) with its address and
permissions. Then, in your server:

```python
from aiveth import Aiveth, InsufficientScopeError, TokenVerificationError, challenge, protected_resource_metadata

MCP = "https://mcp.acme.com/mcp"
aiveth = Aiveth(base_url="https://accounts.aiveth.com")

# GET /.well-known/oauth-protected-resource/mcp
metadata = protected_resource_metadata(resource=MCP, base_url="https://accounts.aiveth.com", scopes=["files:read"])

# every MCP request
try:
    who = aiveth.tokens.verify_mcp_token(bearer, resource=MCP, scopes=("files:read",))
except InsufficientScopeError:
    status, header = 403, challenge(resource=MCP, scope=["files:read"], error="insufficient_scope")
except TokenVerificationError:
    status, header = 401, challenge(resource=MCP, scope=["files:read"])
```

A token is for ONE MCP server (its `aud`); it lasts 10 minutes and the agent renews it.

## Feature flags and the person's photo

```python
from aiveth import has_flag
user = aiveth.tokens.verify_id_token(id_token, client_id=CLIENT_ID)
if has_flag(user, "new-billing"):
    ...  # show the new billing page
user.get("picture")  # their photo's address on Aiveth (with the `profile` scope)
```

## A person's Google, Microsoft or GitHub token (connected accounts)

Turn on "Connected accounts" for the app in Aiveth, and let the person connect the account
(Account → Connected for apps). Then, on your server:

```python
google = aiveth.tokens.connected_token(access_token=access_token, connection="google",
                                        client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
google["access_token"]  # call Google as the person
```

## Roles on parts of your organization (fine-grained permissions)

Make the types of part (like `region` → `branch`) and the roles for them in the console (Parts &
access). Then your server registers each part as it makes it, gives roles on parts, and asks:

```python
aiveth.authorization.register(type="branch", external_id="mumbai", name="Mumbai",
                               parent_type="region", parent_external_id="west")
aiveth.authorization.give("branch", "mumbai", "branch_approver", person_id=ravi)
answer = aiveth.authorization.check(ravi, "claim:approve", "branch", "mumbai")
answer["allowed"], answer["via"]
```

A role on a part covers everything under it. The key needs `authorization:read` for the check,
`resources:write` to register parts and `authorization:write` to give roles.

## Consent (India's DPDP Act) from your app's own screens

Publish your consent notice in the console (Privacy). If your app asks on its own screen instead of
Aiveth's, record the answers, check before you use the data, and let people withdraw:

```python
aiveth.consents.record(person_id, notice_version, {"service": True, "news": False})
if aiveth.consents.allows(person_id, "news"):
    send_newsletter()
aiveth.consents.withdraw(person_id, "news")
receipt = aiveth.consents.receipt(person_id)["receipt"]  # signed, checkable with your JWKS
aiveth.consents.import_all(notice_version, earlier_consents)  # given before Aiveth
```

A yes with a time limit stops counting on its day, so always ask `allows` rather than keeping
the answer. The key needs `consent:read` to check and `consent:write` to record.

## Development and production

Each environment has its own keys: `aiveth_sk_test_…` and apps `okc_test_…` for your Development
environment, `aiveth_sk_live_…` and `okc_live_…` for production.

## A machine app's token, data to erase, AI agent actions (0.2.0)

```python
token = aiveth.tokens.machine_token(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope="reports:read")
for task in aiveth.erasures.list():
    ...  # erase it in your app
    aiveth.erasures.done(task["id"])
aiveth.agent_actions.report(token=agent_token, action="claims.approve", idempotency_key=request_id)
```
