Skip to content

Commit

Permalink
chore: restructure authz to authx
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 22, 2024
1 parent 7117b0a commit 9371a73
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
15 changes: 8 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ linters-settings:
# default is false: such cases aren't reported by default.
check-blank: false

# [deprecated] comma-separated list of pairs of the form pkg:regex
# the regex is used to ignore names within pkg. (default "fmt:.*").
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
ignore: fmt:.*,io/ioutil:^Read.*
# report about not checking of errors in assignments: `num, err := strconv.Atoi(numStr)`;
exclude-functions:
- fmt:.*
- io/ioutil:^Read.*

govet:
# report about shadowed variables
Expand Down Expand Up @@ -98,10 +98,12 @@ linters-settings:

linters:
enable:
- megacheck
- govet
- gocyclo
- gocritic
- gosimple
- staticcheck
- unused
- goconst
- goimports
- gofmt # We enable this as well as goimports for its simplify mode.
Expand All @@ -110,11 +112,10 @@ linters:
- unconvert
- misspell
- nakedret
- exportloopref
- copyloopvar
- gosec

disable:
- scopelint
- errcheck

presets:
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ fmt.Println(s) // "true"

There are functions to convert `int`, `string` and `bool` values.

## Operators

There is the implementation of various operators.

```go
// Is the ternary operator.
utilx.IfElse(cond, 100, 0)
```

## Databases

There are also more complex tools like the `Database` interface which enables to easliy implement database wrappers.
Expand Down Expand Up @@ -91,6 +100,37 @@ if err := s.Wait(); errors.As(err, &serverErr) {
}
```

## FGA with OpenFGA

There is also a package to work with the OpenFGA API.

```go
// Store is an interface that provides methods for transactional operations on the authz database.
type Store[Tx any] interface {
// Allowed checks if the user is allowed to perform the operation on the object.
Allowed(context.Context, User, Object, Relation) (bool, error)
// WriteTx starts a read write transaction.
WriteTx(context.Context, func(context.Context, Tx) error) error
}

// StoreTx is an interface that provides methods for transactional operations on the authz database.
type StoreTx interface {
// WriteTuple writes a tuple to the authz database.
WriteTuple(context.Context, User, Object, Relation) error
// DeleteTuple deletes a tuple from the authz database.
DeleteTuple(context.Context, User, Object, Relation) error
}
```

This can be used with the package.

```go
authzStore, err := authx.NewStore(fgaClient, authz.NewWriteTx())
if err != nil {
return err
}
```

## License

[MIT](/LICENSE)
15 changes: 14 additions & 1 deletion authz/builder.go → authx/fga/builder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authz
package fga

import (
"context"
Expand Down Expand Up @@ -46,6 +46,19 @@ type StoreTx interface {
DeleteTuple(context.Context, User, Object, Relation) error
}

// NoopStore is a store that does nothing.
type NoopStore struct{}

// Allowed checks if the user is allowed to perform the operation on the object.
func (n *NoopStore) Allowed(context.Context, User, Object, Relation) (bool, error) {
return true, nil
}

// WriteTx starts a read write transaction.
func (n *NoopStore) WriteTx(context.Context, func(context.Context, StoreTx) error) error {
return nil
}

// AuthzError is an error that occurred while executing a query.
type AuthzError struct {
// Op is the operation that caused the error.
Expand Down
2 changes: 0 additions & 2 deletions b64/b64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func TestBase64(t *testing.T) {
{"success", "hello", "aGVsbG8="},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 0 additions & 4 deletions cast/ptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ func TestPtr(t *testing.T) {
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand All @@ -50,8 +48,6 @@ func TestValue(t *testing.T) {
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

Expand Down
1 change: 0 additions & 1 deletion cast/ptrslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ func PtrSlice[T any](slice ...T) []*T {
ps := make([]*T, 0, len(slice))

for _, e := range slice {
e := e
ps = append(ps, &e)
}

Expand Down

0 comments on commit 9371a73

Please sign in to comment.