OAuth Implementation Guide

rforssen.net – *Practical guide on how to implement authentication correctly*

This document explains how to actually build applications using the two authentication strategies defined in the OAuth Architecture document:

1️⃣ In-App Authentication
2️⃣ Edge Authentication

🟦 A. In-App Authentication

Mixed public content + authenticated personalization

This is used when an application should load normally for anyone, but behave differently based on login state and role.

Examples:

1️⃣ Backend Requirements

You already have the correct backend foundation:

EndpointDescription
/auth/login/googleStart login
/auth/login/google/callbackFinish login
/auth/meCheck login + get user info
/auth/logoutEnd session

/auth/me Contract

If NOT authenticated:

{
  "authenticated": false
}

If authenticated:

{
  "authenticated": true,
  "user": {
    "email": "...",
    "name": "...",
    "sub": "google-id"
  },
  "role": "me | family | public"
}

2️⃣ Frontend Implementation Pattern

Every page using in-app authentication follows this pattern:

On page load:
1️⃣ fetchAuth() → calls /auth/me
2️⃣ Store auth state
3️⃣ renderAuthBadge() updates UI
4️⃣ Role determines what content is visible
5️⃣ Backend still protects restricted operations

Your media app already implements this extremely well (using fetchAuth(), initAuth(), BroadcastChannel, popup login, etc.). This is your platform standard.

3️⃣ Personalizing / Filtering Content

A mixed page normally behaves like this:

Filtering works with classes and logic like:

// If not logged in
hideElementsWithClass("onlyMe");
hideElementsWithClass("familyOnly");

// If Roland
show everything
setRole("me");

// If Family
show public + family
Frontend filtering = UX convenience.
Backend authorization = real security.

4️⃣ Backend Security Enforcement

Even if UI hides features, the backend always decides access.

OperationAuth Requirement
Delete videome only
Trigger FFmpeg jobme only
View private family mediame + family
View public contentanyone

5️⃣ Cookie Security

Ensure Flask session configuration:

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Lax"

6️⃣ Redirect Safety

Do NOT allow arbitrary redirect destinations.

Only approve whitelisted domains when handling: ?redirect=

7️⃣ Implementation Checklist

If all above are true → In-App Auth is correctly implemented.

🟥 B. Edge Authentication

Hard gate – block all access unless authenticated

Use this when nothing should be visible unless the user is authenticated. Not even the UI. No partial access. No leaks.

Perfect for:

Conceptual Flow

User Request → Traefik Ingress → oauth2-proxy → Application

If authenticated → forward
If not → block

Who Handles Authentication?

This means the protected application never sees anonymous traffic.

Edge Authentication Guarantees

Implementation Requirements

Optional Identity Headers

If app wants to know who logged in:

X-Auth-User
X-Auth-Email
X-Auth-Verified

App may:

Behavior Rules

When NOT to Use Edge Auth

Edge Auth is for:
Infrastructure. Admin. Power Tools. Critical Systems.

Strategy Summary

NeedUse
Public + private experienceIn-App Auth
Roles + personalizationIn-App Auth
Family or guest usabilityIn-App Auth
Admin toolsEdge Auth
phpMyAdminEdge Auth
Cluster dashboardsEdge Auth
Any “dangerous” systemEdge Auth

Platform Philosophy

Platform / Admin / Infrastructure
➡ Edge Authentication
Family & User Applications
➡ In-App Authentication