forked from vfarcic/cncf-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openfga.go
52 lines (48 loc) · 1.1 KB
/
openfga.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
46
47
48
49
50
51
52
package main
import (
"context"
"fmt"
"log"
"os"
. "github.com/openfga/go-sdk/client"
)
func fgaCheck(user, relation string) (bool, error) {
fgaUrl := os.Getenv("FGA_API_URL")
if len(fgaUrl) == 0 {
return true, nil
}
client, err := getFgaClient(fgaUrl)
if err != nil {
return false, err
}
body := ClientCheckRequest{
User: "user:" + user,
Relation: relation,
Object: "document:silly-demo",
}
data, err := client.Check(context.Background()).Body(body).Execute()
if err != nil {
return false, err
}
return *data.Allowed, nil
}
func getFgaClient(url string) (*OpenFgaClient, error) {
log.Println("Creating client...")
storeID := os.Getenv("FGA_STORE_ID")
if len(storeID) == 0 {
return nil, fmt.Errorf("FGA_STORE_ID env. var. is missing")
}
modelID := os.Getenv("FGA_MODEL_ID")
if len(modelID) == 0 {
return nil, fmt.Errorf("FGA_MODEL_ID env. var. is missing")
}
client, err := NewSdkClient(&ClientConfiguration{
ApiUrl: url,
StoreId: storeID,
AuthorizationModelId: modelID,
})
if err != nil {
log.Fatal(err.Error())
}
return client, nil
}