2022-10-09 15:23:52 +02:00
|
|
|
package graph
|
|
|
|
|
|
|
|
|
|
import (
|
2022-12-17 14:36:42 +01:00
|
|
|
"context"
|
2023-04-27 07:09:10 +02:00
|
|
|
"fmt"
|
2025-04-12 10:43:40 +02:00
|
|
|
"log/slog"
|
2022-12-17 14:36:42 +01:00
|
|
|
|
2022-10-09 15:23:52 +02:00
|
|
|
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
|
|
|
|
|
|
2026-01-17 22:53:46 +01:00
|
|
|
"gitea.unbound.se/unboundsoftware/schemas/cache"
|
|
|
|
|
"gitea.unbound.se/unboundsoftware/schemas/middleware"
|
2022-10-09 15:23:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:generate go run github.com/99designs/gqlgen
|
2024-03-25 21:46:15 +01:00
|
|
|
//go:generate gofumpt -w .
|
2026-01-17 22:53:46 +01:00
|
|
|
//go:generate goimports -w -local gitea.unbound.se/unboundsoftware/schemas .
|
2022-10-09 15:23:52 +02:00
|
|
|
|
|
|
|
|
// This file will not be regenerated automatically.
|
|
|
|
|
//
|
|
|
|
|
// It serves as dependency injection for your app, add any dependencies you require here.
|
|
|
|
|
|
|
|
|
|
type Publisher interface {
|
2022-12-17 14:36:42 +01:00
|
|
|
Publish(ctx context.Context, event eventsourced.Event) error
|
2022-10-09 15:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Resolver struct {
|
2026-02-23 08:05:47 +01:00
|
|
|
EventStore eventsourced.EventStore
|
|
|
|
|
Publisher Publisher
|
|
|
|
|
Logger *slog.Logger
|
|
|
|
|
Cache *cache.Cache
|
|
|
|
|
PubSub *PubSub
|
|
|
|
|
CosmoGenerator *CosmoGenerator
|
|
|
|
|
Debouncer *Debouncer
|
2022-10-09 15:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-29 22:13:25 +02:00
|
|
|
func (r *Resolver) apiKeyCanAccessRef(ctx context.Context, ref string, publish bool) (string, error) {
|
|
|
|
|
key, err := middleware.ApiKeyFromContext(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
apiKey := r.Cache.ApiKeyByKey(key)
|
|
|
|
|
if publish && !apiKey.Publish {
|
|
|
|
|
return "", fmt.Errorf("provided API-key doesn't have publish privilege")
|
|
|
|
|
}
|
|
|
|
|
if !publish && !apiKey.Read {
|
|
|
|
|
return "", fmt.Errorf("provided API-key doesn't have read privilege")
|
|
|
|
|
}
|
|
|
|
|
for _, rr := range apiKey.Refs {
|
|
|
|
|
if rr == ref {
|
|
|
|
|
return apiKey.Name, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "", fmt.Errorf("provided API-key doesn't have the required privilege on the requested Schema Ref")
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 14:36:42 +01:00
|
|
|
func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) {
|
|
|
|
|
return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher))
|
2022-10-09 15:23:52 +02:00
|
|
|
}
|
2023-04-27 07:09:10 +02:00
|
|
|
|
|
|
|
|
func apiKeyId(orgId, name string) string {
|
|
|
|
|
return fmt.Sprintf("%s-%s", orgId, name)
|
|
|
|
|
}
|