~bigbes/sr-ht-spec

ref: 0a32fd7a58ef967cab397053f9f3b59abd719375 sr-ht-spec/coreauth/coreauth.go -rw-r--r-- 1.6 KiB
0a32fd7a — Eugene Blikh logging: take the instance's log policy from ecore 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
// 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))
}