Skip to main content

kik-oidc

OpenID Connect (OIDC) authentication for Kik-OS Django projects. kik-oidc uses Authlib for the authorization-code flow and provides login, callback and logout views, user provisioning, and session token refresh.

Requirements

  • Python 3.12 or later
  • Django 4.2 or later, before 5.3
  • Kik-OS with its Django components
  • An OIDC provider that supports discovery, ID tokens and UserInfo

Install the package in your Django project's environment:

uv add kik-oidc

Django configuration

Add kik_oidc to INSTALLED_APPS. Its application config registers the Authlib client named oidc.

Configure the client using your provider's issuer, client ID and client secret. Load secrets from your deployment's secret store rather than committing them to settings.

import os

INSTALLED_APPS = [
    # Your existing applications...
    "kik_oidc",
]

OIDC_HTTP_TIMEOUT = 10
OIDC_RP_SCOPES = "openid email profile"

AUTHLIB_OAUTH_CLIENTS = {
    "oidc": {
        "client_id": os.environ["OIDC_CLIENT_ID"],
        "client_secret": os.environ["OIDC_CLIENT_SECRET"],
        "server_metadata_url": (
            os.environ["OIDC_ISSUER"].rstrip("/")
            + "/.well-known/openid-configuration"
        ),
        "client_kwargs": {
            "scope": OIDC_RP_SCOPES,
            "default_timeout": OIDC_HTTP_TIMEOUT,
        },
    },
}

AUTHENTICATION_BACKENDS = [
    "kik_oidc.auth.SsoAuthenticationBackend",
    # Optional: retain local authentication for administrators.
    "django.contrib.auth.backends.ModelBackend",
]

MIDDLEWARE.insert(
    MIDDLEWARE.index(
        "django.contrib.auth.middleware.AuthenticationMiddleware"
    ) + 1,
    "kik_oidc.middleware.OidcSessionMiddleware",
)

LOGIN_URL = "/oidc/login"
LOGIN_REDIRECT_URL = "/"

# A stable, secret key used to encrypt OIDC tokens in the database.
KIK_OS_SECRET_KEY = os.environ["KIK_OS_SECRET_KEY"]

OidcSessionMiddleware must come after AuthenticationMiddleware. Do not remove SessionMiddleware or Django's session authentication machinery: OIDC credentials are tied to Django sessions.

To enable PKCE with S256, add the following to client_kwargs:

"code_challenge_method": "S256",

Use PKCE only when your provider supports it.

URLs

Include the OIDC URLs and use the package's logout view. The following example keeps local login and logout available when OIDC is disabled:

from django.conf import settings
from django.contrib.auth import views as auth_views
from django.urls import include, path

from kik_oidc.views import logout

urlpatterns = [
    path("login/", auth_views.LoginView.as_view(), name="login"),
    path("logout/", logout, name="logout"),
    path("oidc/", include("kik_oidc.urls")),
    # Your existing URLs...
]

When OIDC is the only authentication method, use kik_oidc.views.login for /login/ as well, or set LOGIN_URL to /oidc/login as shown above. Mounting the URLs at /oidc/ provides:

URL Purpose
/oidc/login Start the authorization-code flow
/oidc/callback Handle the provider callback
/oidc/logged-out Local post-logout page

Register these callback URLs with the provider, using the public origin of your application:

https://app.example.org/oidc/callback
https://app.example.org/oidc/logged-out

The callback URI is generated with request.build_absolute_uri(). Configure Django's host and proxy settings correctly when running behind a reverse proxy, including ALLOWED_HOSTS, SECURE_PROXY_SSL_HEADER and HTTPS redirects as appropriate.

Log out with a POST request, for example:

<form method="post" action="{% url 'logout' %}">
  {% csrf_token %}
  <button type="submit">Sign out</button>
</form>

GET requests to the OIDC logout view are rejected. If the provider supports end-session, logout includes an id_token_hint and a post-logout redirect.

Provider claims

The requested scopes must produce a verified ID token containing iss and sub, and a UserInfo response whose sub matches the ID token. The organization claim is required in UserInfo.

The standard openid email profile scopes normally provide the identity claims below. Configure your provider to add organization as a custom UserInfo claim; it is not supplied by all providers by default.

Claim Use
iss, sub Stable OIDC identity
organization Kik-OS hub slug
email User email address
given_name, family_name User first and last name
name SSO profile full name

A user is identified by the unique (iss, sub) pair, not by username or email address. New users receive an unusable password and a deterministic OIDC username derived from the issuer and subject. Returning users have non-empty changed profile claims updated.

Hubs and memberships

organization must be a valid lowercase hub slug. The user must be active, the hub must be active, and the user must be an accepted member.

With Kik-OS schema tenancy (KIK_TENANCY = "schemas"), a missing valid hub is created and provisioned, and the user receives an ordinary accepted membership. Existing membership roles and states are preserved. Ambiguous schema names and reserved slugs are rejected.

With single-hub tenancy (KIK_TENANCY = "single"), the hub must already exist. An unknown organization is rejected rather than creating a hub.

Do not use the first OIDC login as the only way to grant administrative access: newly provisioned users receive ordinary memberships.

Sessions and token storage

The session middleware checks OIDC credentials on authenticated requests and refreshes tokens shortly before access-token expiry. Refresh and ID tokens are encrypted in the database; access tokens are not persisted. Sessions still expire at their normal Django expiry, even if the provider allows longer-lived refresh tokens.

Keep KIK_OS_SECRET_KEY stable and secret. Losing all configured decryption keys makes stored tokens unusable. A project can instead set KIK_OS_SECRET_BOX to a dotted path for a Kik-OS-compatible encryption module.

Paths beginning with /oidc/ or /logout/ are excluded from session refresh checks by default. Set OIDC_PUBLIC_PATHS to override this tuple. If your Kik-OS project uses sign-in gating or schema-aware hub middleware, also allow the OIDC endpoints through those layers, for example:

KIK_EXTRA_OPEN_PATHS = ("/oidc",)
KIK_CENTRAL_PATHS = ("/oidc/", "/logout/")

Use a scheduled call to kik_oidc.sessions.cleanup() if you want to remove credentials associated with expired Django sessions.

Development

Run the test suite and linter with:

uv run --frozen pytest
uv run ruff check tests

The test suite uses SQLite and mocked provider responses; no external OIDC provider is needed.

Releases are managed with Release Please. Merging its release pull request creates a GitHub release and publishes the package to PyPI through trusted publishing.

Metadata

Release files for kik-oidc 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kik-oidc 0.2.0
File Size Uploaded Provenance Actions
kik_oidc-0.2.0.tar.gz 11.1 kB With provenance Details for kik_oidc-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kik-oidc 0.2.0
File Interpreter ABI Platform Actions
kik_oidc-0.2.0-py3-none-any.whl Python 3 none any Details for kik_oidc-0.2.0-py3-none-any.whl

Total release size: 25.3 kB

Release files / kik_oidc-0.2.0.tar.gz

Download URL kik_oidc-0.2.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
015cfee7e0a9ba7c98bbbda1b2d294327dec6420f83682cda64f65a3ceefc377
BLAKE2b-256 checksum
How to use checksums
6f4970f3db614c275c6f299feb09f520d8e5835181f71df340ecb2885bf752bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / kik_oidc-0.2.0-py3-none-any.whl

Download URL kik_oidc-0.2.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf3e5bf9387a10ecbd509b9592b93665108b5e54cb8c63717249a87124c647d5
BLAKE2b-256 checksum
How to use checksums
0f4df024613138310d791a2fb20c85c9cd11406a527de77718b1715d65043d13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for kik-oidc 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kik_oidc-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page