Skip to content

Commit

Permalink
Copy Python quickstart.
Browse files Browse the repository at this point in the history
  • Loading branch information
samscott89 committed Sep 9, 2021
1 parent c87c005 commit 5883894
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 97 deletions.
1 change: 0 additions & 1 deletion expenses.polar

This file was deleted.

8 changes: 7 additions & 1 deletion go.mod
Expand Up @@ -2,4 +2,10 @@ module github.com/osohq/oso-go-quickstart

go 1.12

require github.com/osohq/go-oso v0.11.3
require (
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/gofiber/fiber/v2 v2.18.0
github.com/klauspost/compress v1.13.5 // indirect
github.com/osohq/go-oso v0.20.0-beta
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365 // indirect
)
21 changes: 21 additions & 0 deletions main.polar
@@ -0,0 +1,21 @@
# TODO: update for main
# actor User {}

resource Repository {
permissions = ["read", "push", "delete"];
roles = ["contributor", "maintainer", "admin"];

"read" if "contributor";
"push" if "maintainer";
"delete" if "admin";

"maintainer" if "admin";
"contributor" if "maintainer";
}

has_role(user: User, role_name: String, repository: Repository) if
role in user.Roles and
role matches { Role: role_name, RepoId: repository.Id };

allow(actor, action, resource) if
has_permission(actor, action, resource);
45 changes: 45 additions & 0 deletions models.go
@@ -0,0 +1,45 @@
package main

type Repository struct {
Id int
Name string
}

var reposDb = []Repository{
{Id: 0, Name: "gmail"}, {Id: 1, Name: "react"}, {Id: 2, Name: "oso"},
}

func GetRepositoryById(id int) Repository {
return reposDb[id]
}

type RepositoryRole struct {
Role string
RepoId int
}

type User struct {
Roles []RepositoryRole
}

var usersDb = map[string]User{
"larry": {
Roles: []RepositoryRole{
{Role: "admin", RepoId: 0},
},
},
"anne": {
Roles: []RepositoryRole{
{Role: "maintainer", RepoId: 1},
},
},
"graham": {
Roles: []RepositoryRole{
{Role: "contributor", RepoId: 2},
},
},
}

func GetCurrentUser() User {
return usersDb["larry"]
}
95 changes: 0 additions & 95 deletions quickstart.go

This file was deleted.

50 changes: 50 additions & 0 deletions server.go
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"reflect"
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/osohq/go-oso"
)

func main() {
app := fiber.New()
oso, err := oso.NewOso()
if err != nil {
fmt.Printf("Failed to set up Oso: %v", err)
return
}

if err := oso.RegisterClass(reflect.TypeOf(Repository{}), nil); err != nil {
fmt.Printf("Failed to start: %s", err)
return
}
if err := oso.RegisterClass(reflect.TypeOf(User{}), nil); err != nil {
fmt.Printf("Failed to start: %s", err)
return
}
if err := oso.LoadFile("main.polar"); err != nil {
fmt.Printf("Failed to start: %s", err)
return
}

app.Get("/repo/:repoId", func(c *fiber.Ctx) error {
repoId, err := strconv.Atoi(c.Params("repoId"))
if err != nil {
return c.SendStatus(400)
}
repository :=
allowed, err := oso.IsAllowed(GetCurrentUser(), "read", GetRepositoryById(repoId))
if err == nil && allowed {
return c.Status(200).SendString(fmt.Sprintf("<h1>A Repo</h1><p>Welcome to repo %s</p>", repository.Name))
} else {
return c.Status(404).SendString("<h1>Whoops!</h1><p>That repo was not found</p>")

}
})
if err := app.Listen(":5000"); err != nil {
fmt.Printf("Failed to start: %s", err)
}
}

0 comments on commit 5883894

Please sign in to comment.