Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go example of sending a resource context #206

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 42 additions & 30 deletions docs/software-development-kits/go/authorizer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ middleware.

```go
import (
"log"
"log"

"github.com/aserto-dev/go-aserto"
"github.com/aserto-dev/go-aserto/az"
"github.com/aserto-dev/go-aserto"
"github.com/aserto-dev/go-aserto/az"
)

...

azClient, err := az.New(
aserto.WithAPIKeyAuth("<Aserto authorizer API key"),
aserto.WithTenantID("<Aserto tenant ID>"),
aserto.WithAPIKeyAuth("<Aserto authorizer API key"),
aserto.WithTenantID("<Aserto tenant ID>"),
)
if err != nil {
log.Fatal("Failed to create authorizer client:", err)
log.Fatal("Failed to create authorizer client:", err)
}
defer azClient.Close()
```
Expand All @@ -40,44 +40,56 @@ to perform an operation.

```go
import (
"context"
"fmt"
"log"
"context"
"fmt"
"log"

"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2"
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api"
"google.golang.org/protobuf/types/known/structpb"

"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2"
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api"
)

...

ctx := context.Background()

// Information about the resource being accessed can be sent
// to the authorizer as a JSON object.
resource, err := structpb.NewStruct(map[string]any{
"id": "[email protected]",
})
if err != nil {
log.Fatalf("failed to create resource: %v", err)
}

result, err := azClient.Is(ctx, &authorizer.IsRequest{
IdentityContext: &api.IdentityContext{ // The user performing the operation.
Type: api.IdentityType_IDENTITY_TYPE_SUB,
Identity: "username",
},
PolicyContext: &api.PolicyContext{
Path: "peoplefinder.GET.users", // Policy module to evaluate.
Decisions: []string{"allowed"}, // Policy rules to evaluate.
},
PolicyInstance: &api.PolicyInstance {
Name: "<policy name>",
},
IdentityContext: &api.IdentityContext{ // The user performing the operation.
Type: api.IdentityType_IDENTITY_TYPE_SUB,
Identity: "username",
},
PolicyContext: &api.PolicyContext{
Path: "peoplefinder.PUT.api.users.__id", // Policy module to evaluate.
Decisions: []string{"allowed"}, // Policy rules to evaluate.
},
ResourceContext: resource,
PolicyInstance: &api.PolicyInstance {
Name: "<policy name>",
},
})
if err != nil {
log.Fatal("Failed to call authorizer:", err)
log.Fatal("Failed to call authorizer:", err)
}

// Check the authorizer's decision.
for _, decision := range result.Decisions {
if decision.Decision == "allowed" {
if decision.Is {
fmt.Println("Access granted")
} else {
fmt.Println("Access denied")
}
}
if decision.Decision == "allowed" {
if decision.Is {
fmt.Println("Access granted")
} else {
fmt.Println("Access denied")
}
}
}
```

Expand Down