Admin login now uses OpenID Connect (PKCE, nonce, ID token verified against the provider JWKS) and only OIDC_ADMIN_SUB may log in. Micropub tokens are issued and verified by StarPunk itself, with a consent screen behind the OIDC session and a Connected Apps page to revoke them, so an external IndieAuth server (Gondulf) is no longer needed. Existing notes, media, tags, webmentions, author profile and sessions are unchanged. Migration 011 adds new tables only. BREAKING CHANGE: production requires OIDC_ISSUER, OIDC_CLIENT_ID, OIDC_CLIENT_SECRET and OIDC_ADMIN_SUB. The ADMIN_ME page must point Micropub apps at StarPunk's IndieAuth endpoints. See ADR-064. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
3.9 KiB
Python
131 lines
3.9 KiB
Python
"""
|
|
Authentication routes for StarPunk
|
|
|
|
Admin login form, OIDC start/callback, and logout.
|
|
The IndieAuth server endpoints for Micropub clients live in routes/indieauth.py.
|
|
"""
|
|
|
|
from flask import (
|
|
Blueprint,
|
|
current_app,
|
|
flash,
|
|
redirect,
|
|
render_template,
|
|
request,
|
|
session,
|
|
url_for,
|
|
)
|
|
|
|
from starpunk.auth import (
|
|
AuthError,
|
|
InvalidStateError,
|
|
OIDCError,
|
|
UnauthorizedError,
|
|
destroy_session,
|
|
handle_callback,
|
|
initiate_login,
|
|
require_auth,
|
|
verify_session,
|
|
)
|
|
|
|
# Create blueprint
|
|
bp = Blueprint("auth", __name__, url_prefix="/auth")
|
|
|
|
SESSION_COOKIE = "starpunk_session"
|
|
|
|
|
|
@bp.route("/login", methods=["GET"])
|
|
def login_form():
|
|
"""
|
|
Show the login page, or go to the dashboard if already logged in
|
|
|
|
Template: templates/admin/login.html
|
|
"""
|
|
session_token = request.cookies.get(SESSION_COOKIE)
|
|
if session_token and verify_session(session_token):
|
|
return redirect(session.pop("next", None) or url_for("admin.dashboard"))
|
|
|
|
return render_template("admin/login.html")
|
|
|
|
|
|
@bp.route("/login", methods=["POST"])
|
|
def login_initiate():
|
|
"""Start OIDC login and redirect to the identity provider"""
|
|
try:
|
|
return redirect(initiate_login(session.pop("next", None)))
|
|
except OIDCError as e:
|
|
current_app.logger.error(f"OIDC login could not start: {e}")
|
|
flash("The identity provider is unavailable. Please try again later.", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
|
|
|
|
@bp.route("/callback")
|
|
def callback():
|
|
"""
|
|
Handle the identity provider's redirect
|
|
|
|
Query parameters:
|
|
code, state: Authorization response
|
|
iss: Issuer identifier (RFC 9207)
|
|
error, error_description: Provider-reported failure
|
|
|
|
Sets:
|
|
session cookie (HttpOnly, Secure, SameSite=Lax)
|
|
"""
|
|
if request.args.get("error"):
|
|
current_app.logger.warning(
|
|
f"OIDC provider returned error: {request.args.get('error')} "
|
|
f"{request.args.get('error_description', '')}"
|
|
)
|
|
flash("Login was cancelled or refused by the identity provider", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
|
|
code = request.args.get("code")
|
|
state = request.args.get("state")
|
|
if not code or not state:
|
|
flash("Missing authentication parameters", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
|
|
try:
|
|
session_token, next_url = handle_callback(code, state, request.args.get("iss"))
|
|
except InvalidStateError:
|
|
flash("Login expired or was already used. Please try again.", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
except UnauthorizedError:
|
|
flash("Authentication failed: Not authorized as admin", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
except AuthError as e:
|
|
current_app.logger.error(f"OIDC login failed: {e}")
|
|
flash("Authentication failed. Please try again.", "error")
|
|
return redirect(url_for("auth.login_form"))
|
|
|
|
response = redirect(next_url or url_for("admin.dashboard"))
|
|
response.set_cookie(
|
|
SESSION_COOKIE,
|
|
session_token,
|
|
httponly=True,
|
|
secure=current_app.config.get("SITE_URL", "").startswith("https://"),
|
|
samesite="Lax",
|
|
max_age=current_app.config.get("SESSION_LIFETIME", 30) * 24 * 60 * 60,
|
|
)
|
|
if not next_url:
|
|
flash("Login successful!", "success")
|
|
return response
|
|
|
|
|
|
@bp.route("/logout", methods=["POST"])
|
|
@require_auth
|
|
def logout():
|
|
"""Destroy the session and clear the cookie"""
|
|
session_token = request.cookies.get(SESSION_COOKIE)
|
|
if session_token:
|
|
try:
|
|
destroy_session(session_token)
|
|
except Exception as e:
|
|
current_app.logger.error(f"Error destroying session: {e}")
|
|
|
|
response = redirect(url_for("public.index"))
|
|
response.delete_cookie(SESSION_COOKIE)
|
|
flash("Logged out successfully", "success")
|
|
return response
|