58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/apex/log"
|
|
"gitlab.com/unboundsoftware/eventsourced/eventsourced"
|
|
|
|
"gitlab.com/unboundsoftware/schemas/cache"
|
|
"gitlab.com/unboundsoftware/schemas/middleware"
|
|
)
|
|
|
|
//go:generate go run github.com/99designs/gqlgen
|
|
|
|
// This file will not be regenerated automatically.
|
|
//
|
|
// It serves as dependency injection for your app, add any dependencies you require here.
|
|
|
|
type Publisher interface {
|
|
Publish(ctx context.Context, event eventsourced.Event) error
|
|
}
|
|
|
|
type Resolver struct {
|
|
EventStore eventsourced.EventStore
|
|
Publisher Publisher
|
|
Logger log.Interface
|
|
Cache *cache.Cache
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
func (r *Resolver) handler(ctx context.Context, aggregate eventsourced.Aggregate) (eventsourced.CommandHandler, error) {
|
|
return eventsourced.NewHandler(ctx, aggregate, r.EventStore, eventsourced.WithEventPublisher(r.Publisher))
|
|
}
|
|
|
|
func apiKeyId(orgId, name string) string {
|
|
return fmt.Sprintf("%s-%s", orgId, name)
|
|
}
|