-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from aserto-dev/golang-resource-example
Add Go example of sending a resource context
- Loading branch information
Showing
1 changed file
with
42 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,15 @@ description: Aserto SDKs - Go - Creating a client and making authorization calls | |
|
||
```go | ||
import ( | ||
"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.WithAddr("localhost:8282"), | ||
aserto.WithInsecure(true), | ||
aserto.WithAddr("localhost:8282"), | ||
aserto.WithInsecure(true), | ||
) | ||
``` | ||
|
||
|
@@ -31,46 +31,58 @@ to perform an operation. | |
|
||
```go | ||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aserto-dev/go-aserto" | ||
"github.com/aserto-dev/go-aserto/az" | ||
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2" | ||
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api" | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"google.golang.org/protobuf/types/known/structpb" | ||
|
||
"github.com/aserto-dev/go-aserto" | ||
"github.com/aserto-dev/go-aserto/az" | ||
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2" | ||
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api" | ||
) | ||
|
||
... | ||
azClient, err := az.New( | ||
aserto.WithAddr("localhost:8282"), | ||
aserto.WithInsecure(true), | ||
aserto.WithAddr("localhost:8282"), | ||
aserto.WithInsecure(true), | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to create authorizer client: %v", err) | ||
log.Fatalf("failed to create authorizer client: %v", err) | ||
} | ||
defer azClient.Close() | ||
|
||
// 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(context.Background(), &authorizer.IsRequest{ | ||
PolicyContext: &api.PolicyContext{ | ||
Path: "peoplefinder.GET.api.users.__id", | ||
Decisions: []string{"allowed"}, | ||
}, | ||
IdentityContext: &api.IdentityContext{ | ||
Identity: "[email protected]", | ||
Type: api.IdentityType_IDENTITY_TYPE_SUB, | ||
}, | ||
PolicyContext: &api.PolicyContext{ | ||
Path: "peoplefinder.PUT.api.users.__id", | ||
Decisions: []string{"allowed"}, | ||
}, | ||
IdentityContext: &api.IdentityContext{ | ||
Identity: "[email protected]", | ||
Type: api.IdentityType_IDENTITY_TYPE_SUB, | ||
}, | ||
ResourceContext: resource, | ||
}) | ||
|
||
// 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") | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|