-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathExample1.java
More file actions
209 lines (183 loc) · 9.26 KB
/
Copy pathExample1.java
File metadata and controls
209 lines (183 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package dev.openfga.sdk.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.ClientAssertion;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.client.model.*;
import dev.openfga.sdk.api.configuration.*;
import dev.openfga.sdk.api.model.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
class Example1 {
public void run(String apiUrl) throws Exception {
var credentials = new Credentials();
if (System.getenv("FGA_CLIENT_ID") != null) {
credentials = new Credentials(new ClientCredentials()
.apiAudience(System.getenv("FGA_API_AUDIENCE"))
.apiTokenIssuer(System.getenv("FGA_API_TOKEN_ISSUER"))
.clientId(System.getenv("FGA_CLIENT_ID"))
.clientSecret(System.getenv("FGA_CLIENT_SECRET")));
} else {
System.out.println("Proceeding with no credentials (expecting localhost)");
}
var configuration = new ClientConfiguration()
.apiUrl(apiUrl) // required, e.g. https://api.fga.example
.storeId(System.getenv("FGA_STORE_ID")) // not needed when calling `CreateStore` or `ListStores`
.authorizationModelId(System.getenv("FGA_MODEL_ID")) // Optional, can be overridden per request
.credentials(credentials);
var fgaClient = new OpenFgaClient(configuration);
// ListStores
System.out.println("Listing Stores");
var stores1 = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores1.getStores().size());
// CreateStore
System.out.println("Creating Test Store");
var store = fgaClient
.createStore(new CreateStoreRequest().name("Test Store"))
.get();
System.out.println("Test Store ID: " + store.getId());
// Set the store id
fgaClient.setStoreId(store.getId());
// ListStores after Create
System.out.println("Listing Stores");
var stores = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores.getStores().size());
// GetStore
System.out.println("Getting Current Store");
var currentStore = fgaClient.getStore().get();
System.out.println("Current Store Name: " + currentStore.getName());
// ReadAuthorizationModels
System.out.println("Reading Authorization Models");
var models = fgaClient.readAuthorizationModels().get();
System.out.println("Models Count: " + models.getAuthorizationModels().size());
// ReadLatestAuthorizationModel
try {
var latestAuthorizationModel = fgaClient
.readLatestAuthorizationModel()
.get(); // TODO: Should this really return null? Optional<...Response>?
System.out.println("Latest Authorization Model ID "
+ latestAuthorizationModel.getAuthorizationModel().getId());
} catch (Exception e) {
System.out.println("Latest Authorization Model not found");
}
var mapper = new ObjectMapper().findAndRegisterModules();
// WriteAuthorizationModel
var authModelJson = loadResource("example1-auth-model.json");
var authorizationModel = fgaClient
.writeAuthorizationModel(mapper.readValue(authModelJson, WriteAuthorizationModelRequest.class))
.get();
System.out.println("Authorization Model ID " + authorizationModel.getAuthorizationModelId());
// ReadAuthorizationModels - after Write
System.out.println("Reading Authorization Models");
models = fgaClient.readAuthorizationModels().get();
System.out.println("Models Count: " + models.getAuthorizationModels().size());
// ReadLatestAuthorizationModel - after Write
var latestAuthorizationModel = fgaClient.readLatestAuthorizationModel().get();
System.out.println("Latest Authorization Model ID "
+ latestAuthorizationModel.getAuthorizationModel().getId());
// Set the model ID
fgaClient.setAuthorizationModelId(
latestAuthorizationModel.getAuthorizationModel().getId());
// Write
System.out.println("Writing Tuples");
fgaClient
.write(
new ClientWriteRequest()
.writes(List.of(
new ClientTupleKey()
.user("user:anne")
.relation("writer")
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"),
new ClientTupleKey()
.user("user:anne")
.relation("writer")
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"), // duplicate
new ClientTupleKey()
.user("user:anne")
.relation("owner")
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a") // different
// relation
)),
new ClientWriteOptions()
.disableTransactions(true)
.authorizationModelId(authorizationModel.getAuthorizationModelId()))
.get();
System.out.println("Done Writing Tuples");
// Read
System.out.println("Reading Tuples");
var readTuples = fgaClient.read(new ClientReadRequest()).get();
System.out.println("Read Tuples" + mapper.writeValueAsString(readTuples));
// ReadChanges
System.out.println("Reading Tuple Changess");
var readChangesTuples =
fgaClient.readChanges(new ClientReadChangesRequest()).get();
System.out.println("Read Changes Tuples" + mapper.writeValueAsString(readChangesTuples));
// Check
System.out.println("Checking for access");
try {
var failingCheckResponse = fgaClient
.check(new ClientCheckRequest()
.user("user:anne")
.relation("reader")
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a"))
.get();
System.out.println("Allowed: " + failingCheckResponse.getAllowed());
} catch (Exception e) {
System.out.println("Failed due to: " + e.getMessage());
}
// Checking for access with context
// TODO: Add ClientCheckRequest.context
// System.out.println("Checking for access with context");
// var checkResponse = fgaClient
// .check(new ClientCheckRequest()
// .user("user:anne")
// .relation("reader")
// ._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
// .context(Map.of("ViewCount", 100)))
// .get();
// System.out.println("Allowed: " + checkResponse.getAllowed());
// WriteAssertions
fgaClient
.writeAssertions(List.of(
new ClientAssertion()
.user("user:carl")
.relation("writer")
._object("document:budget")
.expectation(true),
new ClientAssertion()
.user("user:anne")
.relation("reader")
._object("document:0192ab2a-d83f-756d-9397-c5ed9f3cb69a")
.expectation(false)))
.get();
System.out.println("Assertions updated");
// ReadAssertions
System.out.println("Reading Assertions");
var assertions = fgaClient.readAssertions().get();
System.out.println("Assertions " + mapper.writeValueAsString(assertions));
// DeleteStore
System.out.println("Deleting Current Store");
fgaClient.deleteStore().get();
System.out.println("Deleted Store: " + currentStore.getName());
}
public static void main(String[] args) {
System.out.println("=== Example 1 (Java) ===");
try {
new Example1().run(System.getenv("FGA_API_URL"));
} catch (Exception e) {
System.err.printf("ERROR: %s%n", e);
}
}
String module = "main"; // Used only for integration testing
// Small helper function to load resource files relative to this class.
private String loadResource(String filename) {
try {
var filepath = Paths.get("src", module, "resources", filename);
return Files.readString(filepath, StandardCharsets.UTF_8);
} catch (IOException cause) {
throw new RuntimeException("Unable to load resource: " + filename, cause);
}
}
}