coverage~bigbes/sr-ht-spec64cae3afcoreauth/coreauth.go

Coverage
100.0% 4/4 statements
Δ
+0.0
Blob
79e5a85
Uncovered nothing — every instrumented line ran
1 // Package coreauth bridges spec.sr.ht's own principal model (authn.Principal)
2 // to core-go's auth.AuthContext, which the core-go webhook engine requires in
3 // context. spec keeps authn as its real authorization; this is a compatibility
4 // shim, nothing more.
5 //
6 // The owner (and an agent, which acts for the owner) map to AUTH_INTERNAL, not
7 // AUTH_COOKIE, for two reasons: INTERNAL bypasses core-go's @access scope
8 // checks (AuthContext.Access short-circuits for INTERNAL), and core-go's
9 // webhooks.NewAuthConfig REFUSES cookie auth outright but accepts INTERNAL — so
10 // mapping the owner to INTERNAL is what lets the single owner create webhooks
11 // at all. The agent identity that triggers an event is not represented here; it
12 // rides in the webhook payload (the proposal), and webhook management is
13 // owner-gated in the resolvers regardless.
14 package coreauth
15
16 import (
17 "context"
18
19 "sourcecraft.dev/bigbes/sr-ht-core/auth"
20
21 "sourcecraft.dev/bigbes/sr-ht-spec/authn"
22 )
23
24 // Derive maps a spec principal to a core-go AuthContext. See the package doc
25 // for why owner and agent both become AUTH_INTERNAL.
26 4 func Derive(p authn.Principal, ownerUserID int) *auth.AuthContext {
27 4 switch {
28 3 case p.IsOwner() || p.IsAgent():
29 3 return &auth.AuthContext{AuthMethod: auth.AUTH_INTERNAL, UserID: ownerUserID, Username: p.Owner}
30 1 default:
31 1 return &auth.AuthContext{AuthMethod: auth.AUTH_ANON_INTERNAL}
32 }
33 }
34
35 // Context installs a derived AuthContext for principal p onto ctx.
36 1 func Context(ctx context.Context, p authn.Principal, ownerUserID int) context.Context {
37 1 return auth.Context(ctx, Derive(p, ownerUserID))
38 1 }