Skip to content

Commit

Permalink
feat: add authz store
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 31, 2024
1 parent 313a7c5 commit e4d1c13
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
76 changes: 76 additions & 0 deletions authz/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package authz

import (
"context"

"github.com/openfga/go-sdk/client"
)

// Store is an interface that provides methods for transactional operations on the authz database.
type Store[Tx any] interface {
// WriteTx starts a read write transaction.
WriteTx(context.Context, func(context.Context, Tx) error) error
}

// AuthzError is an error that occurred while executing a query.
type AuthzError struct {
// Op is the operation that caused the error.
Op string
// Err is the error that occurred.
Err error
}

// Error implements the error interface.
func (e *AuthzError) Error() string { return e.Op + ": " + e.Err.Error() }

// Unwrap implements the errors.Wrapper interface.
func (e *AuthzError) Unwrap() error { return e.Err }

// NewQueryError returns a new QueryError.
func NewQueryError(op string, err error) *AuthzError {
return &AuthzError{
Op: op,
Err: err,
}
}

type storeImpl[W any] struct {
tx StoreTxFactory[W]
client *client.OpenFgaClient
}

// StoreTxFactory is a function that creates a new instance of authz store.
type StoreTxFactory[Tx any] func(*client.OpenFgaClient) (Tx, error)

// NewStore returns a new instance of authz store.
func NewStore[Tx any](client *client.OpenFgaClient, tx StoreTxFactory[Tx]) (Store[Tx], error) {
return &storeImpl[Tx]{tx, client}, nil
}

// ReadWriteTx starts a read only transaction.
func (s *storeImpl[Tx]) WriteTx(ctx context.Context, fn func(context.Context, Tx) error) error {
t, err := s.tx(s.client)
if err != nil {
return err
}

if err := fn(ctx, t); err != nil {
return err
}

return nil
}

type defaultStoreTxImpl struct {
client *client.OpenFgaClient
}

// DefaultStoreTx is a default authz store transaction.
type DefaultStoreTx interface{}

// NewDefaultStoreTx returns a new instance of default authz store transaction.
func NewDefaultStoreTx() StoreTxFactory[DefaultStoreTx] {
return func(fga *client.OpenFgaClient) (DefaultStoreTx, error) {
return &defaultStoreTxImpl{client: fga}, nil
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/golang/mock v1.6.0
github.com/golangci/golangci-lint v1.59.1
github.com/openfga/go-sdk v0.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSo
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/jgautheron/goconst v1.7.1 h1:VpdAG7Ca7yvvJk5n8dMwQhfEZJh95kl/Hl9S1OI5Jkk=
github.com/jgautheron/goconst v1.7.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4=
github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs=
Expand Down Expand Up @@ -417,6 +419,8 @@ github.com/onsi/ginkgo/v2 v2.17.3 h1:oJcvKpIb7/8uLpDDtnQuf18xVnwKp8DTD7DQ6gTd/MU
github.com/onsi/ginkgo/v2 v2.17.3/go.mod h1:nP2DPOQoNsQmsVyv5rDA8JkXQoCs6goXIvr/PRJ1eCc=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/openfga/go-sdk v0.5.0 h1:1IuAu6Xf4eBxgc2AyMfosK7QzApxuZ5yi7jmFaftnl0=
github.com/openfga/go-sdk v0.5.0/go.mod h1:AoMnFlPw65sU/7O4xOPpCb2vXA8ZD9K9xp2hZjcvt4g=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
Expand Down

0 comments on commit e4d1c13

Please sign in to comment.