-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0bd5cf
commit 0432855
Showing
2 changed files
with
174 additions
and
83 deletions.
There are no files selected for viewing
251 changes: 168 additions & 83 deletions
251
example/example1/src/main/java/dev/openfga/sdk/example/Example1.java
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 |
---|---|---|
@@ -1,100 +1,185 @@ | ||
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.configuration.ClientConfiguration; | ||
import dev.openfga.sdk.api.configuration.ClientCredentials; | ||
import dev.openfga.sdk.api.configuration.Credentials; | ||
import dev.openfga.sdk.api.client.model.*; | ||
import dev.openfga.sdk.api.configuration.*; | ||
import dev.openfga.sdk.api.model.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
class Example1 { | ||
|
||
public static void main(String[] args) 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_TOKEN_ISSUER")) | ||
.clientId("FGA_CLIENT_ID") | ||
.clientSecret("FGA_CLIENT_SECRET")); | ||
} else { | ||
System.out.println("Proceeding with no credentials (expecting localhost)"); | ||
} | ||
|
||
var configuration = new ClientConfiguration() | ||
.apiUrl(System.getenv("FGA_API_URL")) // required, e.g. https://api.fga.example | ||
.storeId(System.getenv("FGA_STORE_ID")) // not needed when calling `CreateStore` or `ListStores` | ||
.authorizationModelId( | ||
System.getenv("FGA_AUTHORIZATION_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 | ||
public static void main(String[] args) { | ||
try { | ||
var latestAuthorizationModel = fgaClient | ||
.readLatestAuthorizationModel() | ||
.get(); // TODO: Should this really return null? Optional<...Response>? | ||
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_TOKEN_ISSUER")) | ||
.clientId("FGA_CLIENT_ID") | ||
.clientSecret("FGA_CLIENT_SECRET")); | ||
} else { | ||
System.out.println("Proceeding with no credentials (expecting localhost)"); | ||
} | ||
|
||
var configuration = new ClientConfiguration() | ||
.apiUrl(System.getenv("FGA_API_URL")) // required, e.g. https://api.fga.example | ||
.storeId(System.getenv("FGA_STORE_ID")) // not needed when calling `CreateStore` or `ListStores` | ||
.authorizationModelId( | ||
System.getenv("FGA_AUTHORIZATION_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 = Files.readString(Paths.get("src", "main", "resources", "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()); | ||
} catch (Exception e) { | ||
System.out.println("Latest Authorization Model not found"); | ||
} | ||
|
||
var mapper = new ObjectMapper().findAndRegisterModules(); | ||
|
||
// WriteAuthorizationModel | ||
var authModelJson = Files.readString(Paths.get("src", "main", "resources", "auth-model.json")); | ||
var authorizationModel = fgaClient | ||
.writeAuthorizationModel(mapper.readValue(authModelJson, WriteAuthorizationModelRequest.class)) | ||
.get(); | ||
System.out.println("Authorization Model ID " + authorizationModel.getAuthorizationModelId()); | ||
// 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:roadmap"))), | ||
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:roadmap")) | ||
.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:roadmap") | ||
// .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:roadmap") | ||
.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()); | ||
|
||
// 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()); | ||
|
||
System.out.println("MORE COMING SOON"); | ||
} catch (Exception e) { | ||
System.err.printf("ERROR: %s%n", e); | ||
} | ||
} | ||
} |
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