package main import ( "context" "crypto/rsa" "errors" "fmt" "io/ioutil" "log" "os" "strconv" "github.com/gofiber/fiber/v2" jwt_fiber "github.com/gofiber/jwt/v3" "github.com/golang-jwt/jwt/v4" fga "github.com/openfga/go-sdk" ) const PRIV_KEY_PATH_ENV = "PRIV_KEY_PATH" const PRIV_KEY_SIZE_ENV = "PRIV_KEY_SIZE" var ( privateKey *rsa.PrivateKey privateKeySize int ) func createStore() { var api_scheme = os.Getenv("FGA_API_SCHEME") var api_host = os.Getenv("FGA_API_HOST") fmt.Printf("Scheme: %s, Host: %s\n", api_scheme, api_host) // Set fga configuration conf, c_err := fga.NewConfiguration(fga.Configuration{ ApiScheme: api_scheme, ApiHost: api_host, }) // Check for errors if c_err != nil { log.Fatalf("Error during fga configuration creation") os.Exit(1) } // initialize client fga_client := fga.NewAPIClient(conf) store_resp, http_resp, r_err := fga_client.OpenFgaApi.CreateStore(context.Background()).Body( fga.CreateStoreRequest{ Name: fga.PtrString("StoreDemo"), }, ).Execute() if r_err != nil { log.Fatalf("Error creating store") os.Exit(1) } fmt.Println(store_resp) fmt.Println(http_resp) } func readRSAPrivKey(path string) (*rsa.PrivateKey, error) { file, errOpen := os.Open(os.Getenv(PRIV_KEY_PATH_ENV)) if errOpen != nil { log.Fatalf("Error while loading %s private key", PRIV_KEY_PATH_ENV) return nil, errors.New("Error while opening private key") } defer file.Close() keyData, errReading := ioutil.ReadAll(file) if errReading != nil { log.Fatalf("Error reading %s private key", PRIV_KEY_PATH_ENV) return nil, errors.New("Error while reading private key") } return jwt.ParseRSAPrivateKeyFromPEM(keyData) } func readDocument(c *fiber.Ctx) error { log.Println(c) // user := c.Locals("user").(*jwt.Token) // claims := user.Claims.(jwt.MapClaims) // name := claims["name"].(string) // return c.SendString(name + " read " + c.Params("document")) return nil } func runFiber(privateKey *rsa.PrivateKey) { app := fiber.New() log.Println(privateKey.Public()) app.Use(jwt_fiber.New(jwt_fiber.Config{ SigningMethod: "RS256", SigningKey: privateKey.Public(), })) app.Get("/read/:document", readDocument) app.Listen(":9999") } func main() { var err error = nil privateKeySize, err = strconv.Atoi(os.Getenv(PRIV_KEY_SIZE_ENV)) if err != nil { log.Fatalf("Error while geting private key size from %s environment variable", PRIV_KEY_SIZE_ENV) } privateKey, err = readRSAPrivKey(os.Getenv(PRIV_KEY_PATH_ENV)) if err != nil { log.Fatalf("Error while geting private key from %s environment variable", PRIV_KEY_PATH_ENV) } createStore() runFiber(privateKey) }