~bigbes/sr-ht-dolt

ref: 27823bb627b12760176e7dfa2aaa3a0c8aae1fe0 sr-ht-dolt/authn/cookie.go -rw-r--r-- 2.9 KiB
27823bb6 — Eugene Blikh log: replace logrus with slog behind auxilia's scribe handler 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package authn

import (
	"encoding/json"
	"log/slog"
	"net/http"

	"go.bigb.es/auxilia/scribe"

	"sourcecraft.dev/bigbes/sr-ht-core/auth"
	"sourcecraft.dev/bigbes/sr-ht-core/crypto"
)

// CookieName is the unified-login cookie shared across every SourceHut service.
const CookieName = "sr.ht.unified-login.v1"

// OptionalCookieMiddleware reads the sr.ht.unified-login.v1 cookie and, when it
// is present and valid, attaches the resolved caller to the request context
// (retrievable with CallerFromContext). It NEVER rejects a request: a missing,
// malformed, undecryptable, or unresolvable cookie leaves the request
// anonymous. This is what allows public browsing and public clones to work
// without credentials — unlike core-go's auth.Middleware, which 401s any
// request lacking a cookie or Authorization header.
//
// It replicates the logic of core-go's unexported cookieAuth: decrypt the
// fernet-sealed cookie with crypto.DecryptWithoutExpiration, decode the
// {"name": ...} payload, and resolve the user via the meta backend (core-go's
// auth.LookupUser path). Suspended users are resolved normally; the suspension
// flag is carried on the caller (via AsCoreCaller) and gates writes at the
// access-control layer rather than being rejected here.
//
// Requires crypto.InitCrypto to have run (server.New does this at startup) and,
// for the user lookup, config.Middleware + database.Middleware installed
// upstream so the context carries the config and database.
func OptionalCookieMiddleware() func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if ac := resolveCookie(r); ac != nil {
				r = r.WithContext(WithCaller(r.Context(), ac))
			}
			next.ServeHTTP(w, r)
		})
	}
}

// resolveCookie returns the caller authenticated by the request's unified-login
// cookie, or nil if there is no cookie or it cannot be resolved for any reason.
// Every failure path returns nil (anonymous) — none is fatal.
func resolveCookie(r *http.Request) *auth.AuthContext {
	cookie, err := r.Cookie(CookieName)
	if err != nil {
		return nil // no cookie: anonymous
	}

	payload := crypto.DecryptWithoutExpiration([]byte(cookie.Value))
	if payload == nil {
		return nil // bad/forged/undecryptable cookie: anonymous
	}

	var authCookie auth.AuthCookie
	if err := json.Unmarshal(payload, &authCookie); err != nil {
		return nil // malformed payload: anonymous
	}
	if authCookie.Name == "" {
		return nil // no username in payload: anonymous
	}

	var ac auth.AuthContext
	if err := meta.LookupUser(r.Context(), authCookie.Name, &ac); err != nil {
		// meta/database unreachable or unknown user: degrade to anonymous
		// rather than failing the request (browsing must keep working).
		slog.WarnContext(r.Context(), "resolving the login cookie's user failed",
			"component", "authn", "username", authCookie.Name, scribe.Err(err))
		return nil
	}
	ac.AuthMethod = auth.AUTH_COOKIE
	return &ac
}