Something went wrong. Try again.
forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148package xrpc
import ( "context" _ "embed" "encoding/json" "fmt" "log/slog" "net/http" "strings"
"github.com/bluesky-social/indigo/atproto/auth" "github.com/go-chi/chi/v5"
"tangled.sh/tangled.sh/core/api/tangled" "tangled.sh/tangled.sh/core/idresolver" "tangled.sh/tangled.sh/core/rbac" "tangled.sh/tangled.sh/core/spindle/config" "tangled.sh/tangled.sh/core/spindle/db" "tangled.sh/tangled.sh/core/spindle/engine" "tangled.sh/tangled.sh/core/spindle/secrets")
const ActorDid string = "ActorDid"
type Xrpc struct { Logger *slog.Logger Db *db.DB Enforcer *rbac.Enforcer Engine *engine.Engine Config *config.Config Resolver *idresolver.Resolver Vault secrets.Manager}
func (x *Xrpc) Router() http.Handler { r := chi.NewRouter()
r.With(x.VerifyServiceAuth).Post("/"+tangled.RepoAddSecretNSID, x.AddSecret) r.With(x.VerifyServiceAuth).Post("/"+tangled.RepoRemoveSecretNSID, x.RemoveSecret) r.With(x.VerifyServiceAuth).Get("/"+tangled.RepoListSecretsNSID, x.ListSecrets)
return r}
func (x *Xrpc) VerifyServiceAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { l := x.Logger.With("url", r.URL)
token := r.Header.Get("Authorization") token = strings.TrimPrefix(token, "Bearer ")
s := auth.ServiceAuthValidator{ Audience: x.Config.Server.Did().String(), Dir: x.Resolver.Directory(), }
did, err := s.Validate(r.Context(), token, nil) if err != nil { l.Error("signature verification failed", "err", err) writeError(w, AuthError(err), http.StatusForbidden) return }
r = r.WithContext( context.WithValue(r.Context(), ActorDid, did), )
next.ServeHTTP(w, r) })}
type XrpcError struct { Tag string `json:"error"` Message string `json:"message"`}
func NewXrpcError(opts ...ErrOpt) XrpcError { x := XrpcError{} for _, o := range opts { o(&x) }
return x}
type ErrOpt = func(xerr *XrpcError)
func WithTag(tag string) ErrOpt { return func(xerr *XrpcError) { xerr.Tag = tag }}
func WithMessage[S ~string](s S) ErrOpt { return func(xerr *XrpcError) { xerr.Message = string(s) }}
func WithError(e error) ErrOpt { return func(xerr *XrpcError) { xerr.Message = e.Error() }}
var MissingActorDidError = NewXrpcError( WithTag("MissingActorDid"), WithMessage("actor DID not supplied"),)
var AuthError = func(err error) XrpcError { return NewXrpcError( WithTag("Auth"), WithError(fmt.Errorf("signature verification failed: %w", err)), )}
var InvalidRepoError = func(r string) XrpcError { return NewXrpcError( WithTag("InvalidRepo"), WithError(fmt.Errorf("supplied at-uri is not a repo: %s", r)), )}
func GenericError(err error) XrpcError { return NewXrpcError( WithTag("Generic"), WithError(err), )}
var AccessControlError = func(d string) XrpcError { return NewXrpcError( WithTag("AccessControl"), WithError(fmt.Errorf("DID does not have sufficent access permissions for this operation: %s", d)), )}
// this is slightly different from http_util::write_error to follow the spec://// the json object returned must include an "error" and a "message"func writeError(w http.ResponseWriter, e XrpcError, status int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(e)}