Metadata-Version: 2.1
Name: zeytic
Version: 0.2.1
Summary: ZeyticAuth Python SDK.
Keywords: zeytic,auth,user,authentication,authorization
Author-Email: ZeyticAuth <contact@zeytic.com>
License: MIT
Project-URL: homepage, https://zeytic.com/
Project-URL: repository, https://github.com/ZeyticAuth/python
Project-URL: documentation, https://github.com/ZeyticAuth/python/tree/master/docs
Requires-Python: <4.0,>=3.8
Requires-Dist: aiohttp<4.0.0,>=3.8.5
Requires-Dist: pydantic<3.0.0,>=2.1.1
Requires-Dist: pyjwt[crypto]<3.0.0,>=2.8.0
Description-Content-Type: text/markdown

# ZeyticAuth Python SDK

[![ZeyticAuth](https://img.shields.io/badge/for-zeytic-7958ff)][Website]
[![Stable Version](https://img.shields.io/pypi/v/zeytic?label=stable)][PyPI Releases]
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/zeytic)][PyPI]
[![PyPI - License](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2FZeyticAuth%2Fpython%2Fmaster%2Fpyproject.toml)](https://github.com/ZeyticAuth/python)
[![Discord](https://img.shields.io/discord/965845662535147551?color=5865f2&logo=discord&label=discord)][Discord]

## Prerequisites

- Python 3.8 or higher
- A [ZeyticAuth Cloud][Website] account or a self-hosted ZeyticAuth
- A ZeyticAuth traditional web application created

If you don't have the ZeyticAuth application created, please follow the [⚡ Get started](https://docs.zeytic.com/docs/tutorials/get-started/) guide to create one.

## Installation
```bash
pip install zeytic # or `poetry add zeytic` or whatever you use
```

## Tutorial

See [tutorial](./docs/tutorial.md) for a quick start.

## API reference

See [API reference](./docs/api.md) for more details.

## Run the sample

There's a Flask sample in the [samples](./samples) directory. The sample has been tested with Python 3.8.17.

### Install dependencies

This repo uses [PDM](https://github.com/pdm-project/pdm) as the package manager. To install the dependencies, run the following command in the root directory of the repo (not in the `samples` directory):

```bash
pdm install
```

### Configure environment variables

To run the sample, you need to set the following environment variables:

```bash
APP_SECRET_KEY=your-secret-key # This is for Flask
ZEYTIC_ENDPOINT=http://your-zeytic-endpoint.com
ZEYTIC_APP_ID=your-zeytic-app-id
ZEYTIC_APP_SECRET=your-zeytic-app-secret
ZEYTIC_REDIRECT_URI=http://127.0.0.1:5000/sign-in-callback
ZEYTIC_POST_LOGOUT_REDIRECT_URI=http://127.0.0.1:5000/
```

Replace the values with your own.

For `ZEYTIC_REDIRECT_URI` and `ZEYTIC_POST_LOGOUT_REDIRECT_URI`, you should:

1. Go to your ZeyticAuth Console and add the URIs to the application's settings accordingly.
2. Update the domain and port to match your local environment if necessary.

> [!Note]
> The sample project also support dotenv. You can create a `.env` file in the root directory of the sample project and add the environment variables there.

### Run the sample

In the root directory of the repo, run the following command:

```bash
pdm run flask
```

The script can be found in the `pyproject.toml` file.

### Fetch user information

Call `client.getIdTokenClaims()` to get the basic user info. For a more detailed user info, you can call `client.fetchUserInfo()`.

For details on fetching user info, see the [Get user information](https://docs.zeytic.com/sdk/python/#get-user-information).

### Route protection

You have many ways to accomplish this.

**Directly check the user's authentication status**

You can call `client.isAuthenticated()` to check if the user is authenticated and can proceed with the request.

**Use a decorator**

You can create a decorator like `@authenticated()` to protect your routes. A sample decorator can be found at [samples/authenticated.py](./samples/authenticated.py).

For instance, an API may throw a 401 error if the user is not authenticated:

```python
from flask import g, jsonify

@app.route("/api/protected")
@authenticated()
def protected():
    print(g.user) # The `@authenticated()` decorator sets the user object in the `g` object
    return jsonify({"message": "This is a protected route"})
```

Or, you can redirect the user to the sign-in page:

```python
from flask import g, jsonify

@app.route("/protected")
@authenticated(shouldRedirect=True)
def protected():
    return "This is a protected route"
```

See the [flask.py](./samples/flask.py) file for more details.

## Resources

- [ZeyticAuth website][Website]
- [ZeyticAuth documentation](https://docs.zeytic.com/)
- [Join Discord][Discord]

[Website]: https://zeytic.com/
[PyPI]: https://pypi.org/project/zeytic/
[PyPI Releases]: https://pypi.org/project/zeytic/#history
[Discord]: https://discord.gg/vRvwuwgpVX
