From 0432855b104bcca23087452d98a722b4b6423319 Mon Sep 17 00:00:00 2001 From: "J.R. Hill" Date: Wed, 27 Dec 2023 10:59:34 -0800 Subject: [PATCH] chore: flesh out example --- .../dev/openfga/sdk/example/Example1.java | 251 ++++++++++++------ .../src/main/resources/auth-model.json | 6 + 2 files changed, 174 insertions(+), 83 deletions(-) diff --git a/example/example1/src/main/java/dev/openfga/sdk/example/Example1.java b/example/example1/src/main/java/dev/openfga/sdk/example/Example1.java index 57a7f79..e822b66 100644 --- a/example/example1/src/main/java/dev/openfga/sdk/example/Example1.java +++ b/example/example1/src/main/java/dev/openfga/sdk/example/Example1.java @@ -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); + } } } diff --git a/example/example1/src/main/resources/auth-model.json b/example/example1/src/main/resources/auth-model.json index 2792171..e136165 100644 --- a/example/example1/src/main/resources/auth-model.json +++ b/example/example1/src/main/resources/auth-model.json @@ -59,6 +59,12 @@ "parameters": { "ViewCount": { "type_name": "TYPE_NAME_INT" + }, + "Type": { + "type_name": "TYPE_NAME_STRING" + }, + "Name": { + "type_name": "TYPE_NAME_STRING" } } }