// Package coreauth bridges spec.sr.ht's own principal model (authn.Principal)
// to core-go's auth.AuthContext, which the core-go webhook engine requires in
// context. spec keeps authn as its real authorization; this is a compatibility
// shim, nothing more.
//
// The owner (and an agent, which acts for the owner) map to AUTH_INTERNAL, not
// AUTH_COOKIE, for two reasons: INTERNAL bypasses core-go's @access scope
// checks (AuthContext.Access short-circuits for INTERNAL), and core-go's
// webhooks.NewAuthConfig REFUSES cookie auth outright but accepts INTERNAL — so
// mapping the owner to INTERNAL is what lets the single owner create webhooks
// at all. The agent identity that triggers an event is not represented here; it
// rides in the webhook payload (the proposal), and webhook management is
// owner-gated in the resolvers regardless.
package coreauth
import (
"context"
"sourcecraft.dev/bigbes/sr-ht-core/auth"
"sourcecraft.dev/bigbes/sr-ht-spec/authn"
)
// Derive maps a spec principal to a core-go AuthContext. See the package doc
// for why owner and agent both become AUTH_INTERNAL.
func Derive(p authn.Principal, ownerUserID int) *auth.AuthContext {
switch {
case p.IsOwner() || p.IsAgent():
return &auth.AuthContext{AuthMethod: auth.AUTH_INTERNAL, UserID: ownerUserID, Username: p.Owner}
default:
return &auth.AuthContext{AuthMethod: auth.AUTH_ANON_INTERNAL}
}
}
// Context installs a derived AuthContext for principal p onto ctx.
func Context(ctx context.Context, p authn.Principal, ownerUserID int) context.Context {
return auth.Context(ctx, Derive(p, ownerUserID))
}