---
title: Python with Authlib
summary: Sign users in to a Flask app with Tessera, using Authlib and PKCE.
section: quickstart
order: 5
---
# Python with Authlib

Sign users in to a Flask app with Tessera, using Authlib and PKCE.

**Requires:** Python 3.9 or later and Flask 3.

## 1. Create the app in Tessera

Sign in to the console at https://platform.kurthlabs.com/tessera with your invitation (see [your first sign-in](/docs/start/first-sign-in.md)), open your realm and add an app. Choose Python (Flask) and where it runs: this computer for now.

The console then shows what you need, each with a copy button:

- the issuer, `https://login.kurthlabs.com/realms/YOUR_REALM`
- the client ID
- the redirect URI, `http://localhost:8000/auth/callback`
- the client secret, **once**. Copy it now, or download the `.env` file. Tessera cannot show it again.

Put the values in `.env` as shown under Environment below, and keep that file out of git. Flask loads it when `python-dotenv` is installed.

If you have the `tessera` CLI (during early access it is by invitation), `tessera init` does this step: it registers the app, writes `.env` and prints the code for your stack. The console path above needs no download.

## 2. Add sign-in

```sh
pip install flask authlib requests python-dotenv
```

`app.py`

```python
import os
from authlib.integrations.flask_client import OAuth
from flask import Flask, redirect, session, url_for
from markupsafe import escape

app = Flask(__name__)
app.secret_key = os.environ["SESSION_SECRET"]
oauth = OAuth(app)
oauth.register(
    "tessera",
    client_id=os.environ["TESSERA_CLIENT_ID"], client_secret=os.environ["TESSERA_CLIENT_SECRET"],
    server_metadata_url=os.environ["TESSERA_ISSUER"] + "/.well-known/openid-configuration",
    client_kwargs={"scope": "openid profile email", "code_challenge_method": "S256"},
)

@app.route("/login")
def login():
    return oauth.tessera.authorize_redirect(url_for("callback", _external=True))

@app.route("/auth/callback")
def callback():
    session["user"] = oauth.tessera.authorize_access_token()["userinfo"]
    return redirect("/")

@app.route("/")
def index():
    email = (session.get("user") or {}).get("email")
    return f"Signed in as {escape(email)}" if email else "Not signed in"
```

Run `flask --app app run --port 8000` and open `/login`. Approve on Tessera and you come back to `/` showing "Signed in as" and your email.

## Environment

The console gives you the first three values (the downloaded `.env` file has them, and so does `tessera init`). Put them in `.env`:

```env
TESSERA_ISSUER=https://login.kurthlabs.com/realms/YOUR_REALM
TESSERA_CLIENT_ID=YOUR_CLIENT_ID
TESSERA_CLIENT_SECRET=YOUR_CLIENT_SECRET
SESSION_SECRET=YOUR_SESSION_SECRET
```

`SESSION_SECRET` is yours, not Tessera's: it signs your app's own session cookie. Generate one with `openssl rand -base64 32`.

The redirect URI registered for this app is `http://localhost:8000/auth/callback`. For production, add your production address to the app's redirect URIs in the console (the same path on `https://your-app.example`); the local one can stay.
