-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfga.go
45 lines (34 loc) · 1.07 KB
/
fga.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package authz
import (
"context"
"github.com/openfga/go-sdk/client"
)
var _ AuthzChecker = (*fga)(nil)
type fga struct {
client *client.OpenFgaClient
}
// NewFGA returns a new FGA authz checker
func NewFGA(c *client.OpenFgaClient) *fga {
return &fga{client: c}
}
// AuthzFGAUser is the subject.
type AuthzFGAUser = AuthzPrincipal
// AuthzFGARelation is the object.
type AuthzFGARelation = AuthzObject
// AuthzFGAAction is the action.
type AuthzFGAAction = AuthzAction
// Allowed returns true if the principal is allowed to perform the action on the user.
// Returns an error if the request fails.
// The principal is the object, the user is the subject, and the permission is the relation.
func (f *fga) Allowed(ctx context.Context, user AuthzFGAUser, relation AuthzFGARelation, object AuthzFGAAction) (bool, error) {
body := client.ClientCheckRequest{
User: user.String(),
Relation: relation.String(),
Object: object.String(),
}
allowed, err := f.client.Check(ctx).Body(body).Execute()
if err != nil {
return false, err
}
return allowed.GetAllowed(), nil
}