|
| 1 | +--- |
| 2 | +id: go |
| 3 | +title: Go |
| 4 | +--- |
| 5 | + |
| 6 | +import CodeBlock from '@theme/CodeBlock' |
| 7 | +import { useLatestRelease } from '@site/src/hooks' |
| 8 | + |
| 9 | + |
| 10 | +Ory Keto exposes two APIs for integration |
| 11 | + |
| 12 | +- [gRPC](http://ory.sh/docs/keto/reference/proto-api) |
| 13 | +- [REST](http://ory.sh/docs/keto/reference/rest-api) |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +### Installation gRPC API |
| 18 | + |
| 19 | +```mdx-code-block |
| 20 | +
|
| 21 | +<CodeBlock className="language-shell">{`go get github.com/ory/keto/proto/ory/keto/acl/v1alpha1@${useLatestRelease('keto')}`}</CodeBlock> |
| 22 | +
|
| 23 | +``` |
| 24 | + |
| 25 | +### Installation REST API |
| 26 | + |
| 27 | +```mdx-code-block |
| 28 | +<CodeBlock className="language-shell">{`go get github.com/ory/keto-client-go@${useLatestRelease('keto')}`}</CodeBlock> |
| 29 | +``` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +### Configuration gRPC API |
| 34 | + |
| 35 | +```go |
| 36 | +package main |
| 37 | + |
| 38 | +import ( |
| 39 | + "google.golang.org/grpc" |
| 40 | + |
| 41 | + acl "github.com/ory/keto/proto/ory/keto/acl/v1alpha1" |
| 42 | +) |
| 43 | + |
| 44 | +func main() { |
| 45 | + conn, err := grpc.Dial("127.0.0.1:4467", grpc.WithInsecure()) |
| 46 | + if err != nil { |
| 47 | + panic("Encountered error: " + err.Error()) |
| 48 | + } |
| 49 | + |
| 50 | + writeClient := acl.NewWriteServiceClient(conn) |
| 51 | + |
| 52 | + // _, err = client.TransactRelationTuples(context.Background() ... |
| 53 | + conn, err = grpc.Dial("127.0.0.1:4466", grpc.WithInsecure()) |
| 54 | + |
| 55 | + readClient := acl.NewReadServiceClient(conn) |
| 56 | + // _, err = readClient.ListRelationTuples(context.Background()... |
| 57 | + |
| 58 | + conn, err = grpc.Dial("127.0.0.1:4466", grpc.WithInsecure()) |
| 59 | + checkClient := acl.NewCheckServiceClient(conn) |
| 60 | + // _, err = checkClient.Check(context.Background() ... |
| 61 | + |
| 62 | + conn, err = grpc.Dial("127.0.0.1:4466", gprc.WithInsecure()) |
| 63 | + expandClient := acl.NewExpandServiceClient(conn) |
| 64 | + // _, err = expandClient.Expand(context.Background() ... |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Configuration REST API |
| 69 | + |
| 70 | +```go |
| 71 | +package main |
| 72 | + |
| 73 | +import ( |
| 74 | + client "github.com/ory/keto-client-go" |
| 75 | +) |
| 76 | + |
| 77 | +func main() { |
| 78 | + configuration := client.NewConfiguration() |
| 79 | + configuration.Servers = []client.ServerConfiguration{ |
| 80 | + { |
| 81 | + URL: "http://127.0.0.1:4467", // Write API |
| 82 | + }, |
| 83 | + } |
| 84 | + writeClient := client.NewAPIClient(configuration) |
| 85 | + // resp, r, err := writeClient.WriteApi.CreateRelationTuple(context.Background())... |
| 86 | + configuration.Servers = []client.ServerConfiguration{ |
| 87 | + { |
| 88 | + URL: "http://127.0.0.1:4466", // Read API |
| 89 | + }, |
| 90 | + } |
| 91 | + readClient := client.NewAPIClient(configuration) |
| 92 | + |
| 93 | + // resp, r, err := readClient.ReadApi.GetCheck(context.Background()... |
| 94 | + // resp, r, err := readClient.ReadApi.GetExpand(context.Background()... |
| 95 | + // resp, r, err := readClient.ReadApi.GetRelationTuples(context.Background()... |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Making requests |
| 100 | + |
| 101 | +As an example, let's create the following relation tuple and check that `alice` has access to `my-first-blog-post` object |
| 102 | + |
| 103 | +``` |
| 104 | +blog_posts:my-first-blog-post#read@alice |
| 105 | +``` |
| 106 | + |
| 107 | +### Making requests with gRPC API |
| 108 | +```go |
| 109 | +package main |
| 110 | + |
| 111 | +import ( |
| 112 | + "context" |
| 113 | + "fmt" |
| 114 | + |
| 115 | + "google.golang.org/grpc" |
| 116 | + |
| 117 | + acl "github.com/ory/keto/proto/ory/keto/acl/v1alpha1" |
| 118 | +) |
| 119 | + |
| 120 | +func main() { |
| 121 | + conn, err := grpc.Dial("127.0.0.1:4467", grpc.WithInsecure()) |
| 122 | + if err != nil { |
| 123 | + panic("Encountered error: " + err.Error()) |
| 124 | + } |
| 125 | + |
| 126 | + client := acl.NewWriteServiceClient(conn) |
| 127 | + |
| 128 | + _, err = client.TransactRelationTuples(context.Background(), &acl.TransactRelationTuplesRequest{ |
| 129 | + RelationTupleDeltas: []*acl.RelationTupleDelta{ |
| 130 | + { |
| 131 | + Action: acl.RelationTupleDelta_INSERT, |
| 132 | + RelationTuple: &acl.RelationTuple{ |
| 133 | + Namespace: "blog_posts", |
| 134 | + Object: "my-first-blog-post", |
| 135 | + Relation: "read", |
| 136 | + Subject: acl.NewSubjectID("alice"), |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }) |
| 141 | + if err != nil { |
| 142 | + panic("Encountered error: " + err.Error()) |
| 143 | + } |
| 144 | + |
| 145 | + fmt.Println("Successfully created tuple") |
| 146 | + readConn, err := grpc.Dial("127.0.0.1:4466", grpc.WithInsecure()) |
| 147 | + if err != nil { |
| 148 | + panic("Encountered error: " + err.Error()) |
| 149 | + } |
| 150 | + checkClient := acl.NewCheckServiceClient(readConn) |
| 151 | + |
| 152 | + check, err := checkClient.Check(context.Background(), &acl.CheckRequest{ |
| 153 | + Namespace: "blog_posts", |
| 154 | + Object: "my-first-blog-post", |
| 155 | + Relation: "read", |
| 156 | + Subject: acl.NewSubjectID("alice"), |
| 157 | + }) |
| 158 | + |
| 159 | + if check.Allowed { |
| 160 | + fmt.Println("Alice has access to my-first-blog-post") |
| 161 | + } |
| 162 | + |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Making requests with REST API |
| 167 | +```go |
| 168 | +package main |
| 169 | + |
| 170 | +import ( |
| 171 | + "context" |
| 172 | + "fmt" |
| 173 | + "os" |
| 174 | + |
| 175 | + client "github.com/ory/keto-client-go" |
| 176 | +) |
| 177 | + |
| 178 | +func main() { |
| 179 | + relationQuery := *client.NewRelationQuery() |
| 180 | + relationQuery.SetNamespace("blog_posts") |
| 181 | + relationQuery.SetObject("my-first-blog-post") |
| 182 | + relationQuery.SetRelation("read") |
| 183 | + relationQuery.SetSubjectId("alice") |
| 184 | + configuration := client.NewConfiguration() |
| 185 | + configuration.Servers = []client.ServerConfiguration{ |
| 186 | + { |
| 187 | + URL: "http://127.0.0.1:4467", // Write API |
| 188 | + }, |
| 189 | + } |
| 190 | + writeClient := client.NewAPIClient(configuration) |
| 191 | + _, r, err := writeClient.WriteApi.CreateRelationTuple(context.Background()).RelationQuery(relationQuery).Execute() |
| 192 | + if err != nil { |
| 193 | + fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) |
| 194 | + panic("Encountered error: " + err.Error()) |
| 195 | + } |
| 196 | + |
| 197 | + fmt.Println("Successfully created tuple") |
| 198 | + configuration.Servers = []client.ServerConfiguration{ |
| 199 | + { |
| 200 | + URL: "http://127.0.0.1:4466", // Write API |
| 201 | + }, |
| 202 | + } |
| 203 | + readClient := client.NewAPIClient(configuration) |
| 204 | + |
| 205 | + check, r, err := readClient.ReadApi.GetCheck(context.Background()). |
| 206 | + Namespace("blog_posts"). |
| 207 | + Object("my-first-blog-post"). |
| 208 | + Relation("read"). |
| 209 | + SubjectId("alice"). |
| 210 | + Execute() |
| 211 | + if err != nil { |
| 212 | + fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) |
| 213 | + panic("Encountered error: " + err.Error()) |
| 214 | + } |
| 215 | + |
| 216 | + if check.Allowed { |
| 217 | + fmt.Println("Alice has access to my-first-blog-post") |
| 218 | + } |
| 219 | + |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +## More examples |
| 224 | + |
| 225 | +### More examples REST API |
| 226 | +- [Write API documentation](https://github.com/ory/keto-client-go/blob/master/docs/WriteApi.md) |
| 227 | +- [Read API documentation](https://github.com/ory/keto-client-go/blob/master/docs/ReadApi.md) |
| 228 | + |
| 229 | +### More examples gRPC API |
| 230 | + |
| 231 | +- [Protocol Buffer API](http://localhost:3001/docs/keto/reference/proto-api) |
0 commit comments