|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | + |
| 4 | +import openfga_sdk |
| 5 | +from openfga_sdk.client.models import ClientAssertion, ClientCheckRequest, ClientReadChangesRequest, ClientTuple, ClientWriteRequest |
| 6 | +from openfga_sdk.models import CreateStoreRequest, Metadata, ObjectRelation, RelationMetadata, TupleKey, TypeDefinition, Userset, Usersets, WriteAuthorizationModelRequest |
| 7 | +from openfga_sdk import ClientConfiguration, OpenFgaClient |
| 8 | +from openfga_sdk.credentials import CredentialConfiguration, Credentials |
| 9 | +import os |
| 10 | + |
| 11 | + |
| 12 | +async def main(): |
| 13 | + credentials = Credentials() |
| 14 | + if os.getenv("FGA_CLIENT_ID") is not None: |
| 15 | + credentials = Credentials( |
| 16 | + method='client_credentials', |
| 17 | + configuration=CredentialConfiguration( |
| 18 | + api_issuer=os.getenv('FGA_API_TOKEN_ISSUER'), |
| 19 | + api_audience=os.getenv('FGA_API_AUDIENCE'), |
| 20 | + client_id=os.getenv('FGA_CLIENT_ID'), |
| 21 | + client_secret=os.getenv('FGA_CLIENT_SECRET') |
| 22 | + ) |
| 23 | + ) |
| 24 | + |
| 25 | + if os.getenv('FGA_API_HOST') is not None: |
| 26 | + configuration = ClientConfiguration( |
| 27 | + api_host=os.getenv('FGA_API_HOST'), |
| 28 | + credentials=credentials |
| 29 | + ) |
| 30 | + else: |
| 31 | + configuration = ClientConfiguration( |
| 32 | + api_scheme='http', |
| 33 | + api_host='localhost:8080', |
| 34 | + credentials=credentials |
| 35 | + ) |
| 36 | + |
| 37 | + async with OpenFgaClient(configuration) as fga_client: |
| 38 | + # ListStores (before create) |
| 39 | + print('Listing Stores') |
| 40 | + response = await fga_client.list_stores() |
| 41 | + print(f"Stores Count: {len(response.stores)}") |
| 42 | + |
| 43 | + store_name = 'Test Store' |
| 44 | + |
| 45 | + # CreateStore (before create) |
| 46 | + print('Creating Test Store') |
| 47 | + body = CreateStoreRequest(name=store_name) |
| 48 | + response = await fga_client.create_store(body) |
| 49 | + print(f"Test Store ID: {response.id}") |
| 50 | + |
| 51 | + # Set the store ID |
| 52 | + fga_client.set_store_id(response.id) |
| 53 | + |
| 54 | + # ListStores (after create) |
| 55 | + print('Listing Stores') |
| 56 | + response = await fga_client.list_stores() |
| 57 | + print(f"Stores Count: {len(response.stores)}") |
| 58 | + |
| 59 | + # GetStore (after create) |
| 60 | + print('Getting Current Store') |
| 61 | + response = await fga_client.get_store() |
| 62 | + print(f"Current Store Name: {response.name}") |
| 63 | + |
| 64 | + # ReadAuthorizationModels (before write) |
| 65 | + print('Reading Authorization Models') |
| 66 | + response = await fga_client.read_authorization_models() |
| 67 | + print(f"Models Count: {len(response.authorization_models)}") |
| 68 | + |
| 69 | + # ReadLatestAuthorizationModel (before write) |
| 70 | + try: |
| 71 | + response = await fga_client.read_latest_authorization_model() |
| 72 | + if response.authorization_model is not None: |
| 73 | + print(f"Latest Authorization Model ID: {response.authorization_model.id}") |
| 74 | + except: |
| 75 | + print('Latest Authorization Model not found') |
| 76 | + |
| 77 | + # WriteAuthorizationModel |
| 78 | + print('Writing an Authorization Model') |
| 79 | + with open(os.path.join(os.path.dirname(__file__), 'auth-model.json')) as f: |
| 80 | + auth_model_request = json.load(f) |
| 81 | + response = await fga_client.write_authorization_model(auth_model_request) |
| 82 | + print(f"Authorization Model ID: {response.authorization_model_id}") |
| 83 | + |
| 84 | + # ReadAuthorizationModels (after write) |
| 85 | + print('Reading Authorization Models') |
| 86 | + response = await fga_client.read_authorization_models() |
| 87 | + print(f"Models Count: {len(response.authorization_models)}") |
| 88 | + |
| 89 | + # ReadLatestAuthorizationModel (after write) |
| 90 | + response = await fga_client.read_latest_authorization_model() |
| 91 | + if response.authorization_model is not None: |
| 92 | + print(f"Latest Authorization Model ID: {response.authorization_model.id}") |
| 93 | + |
| 94 | + auth_model_id = response.authorization_model.id |
| 95 | + |
| 96 | + # Write |
| 97 | + print('Writing Tuples') |
| 98 | + body = ClientWriteRequest( |
| 99 | + writes=[ |
| 100 | + ClientTuple( |
| 101 | + user='user:anne', |
| 102 | + relation='writer', |
| 103 | + object='document:roadmap', |
| 104 | + # condition=RelationshipCondition( |
| 105 | + # name='ViewCountLessThan200', |
| 106 | + # context=dict( |
| 107 | + # Name='Roadmap', |
| 108 | + # Type='Document', |
| 109 | + # ), |
| 110 | + # ), |
| 111 | + ), |
| 112 | + ], |
| 113 | + ) |
| 114 | + options = { |
| 115 | + # You can rely on the model id set in the configuration or override it for this specific request |
| 116 | + "authorization_model_id": auth_model_id |
| 117 | + } |
| 118 | + await fga_client.write(body, options) |
| 119 | + print('Done Writing Tuples') |
| 120 | + |
| 121 | + # Set the model ID |
| 122 | + fga_client.set_authorization_model_id(auth_model_id) |
| 123 | + |
| 124 | + # Read |
| 125 | + print('Reading Tuples') |
| 126 | + response = await fga_client.read(TupleKey(user='user:anne', object='document:')) |
| 127 | + print(f"Read Tuples: {response.tuples}") |
| 128 | + |
| 129 | + # ReadChanges |
| 130 | + print('Reading Tuple Changes') |
| 131 | + body = ClientReadChangesRequest('document') |
| 132 | + response = await fga_client.read_changes(body) |
| 133 | + print(f"Read Changes Tuples: {response.changes}") |
| 134 | + |
| 135 | + # Check |
| 136 | + print('Checking for access') |
| 137 | + response = await fga_client.check(ClientCheckRequest( |
| 138 | + user='user:anne', |
| 139 | + relation='reader', |
| 140 | + object='document:roadmap', |
| 141 | + )) |
| 142 | + print(f"Allowed: {response.allowed}") |
| 143 | + |
| 144 | + # Checking for access with context |
| 145 | + # TODO |
| 146 | + |
| 147 | + # WriteAssertions |
| 148 | + await fga_client.write_assertions([ |
| 149 | + ClientAssertion( |
| 150 | + user='user:carl', |
| 151 | + relation='writer', |
| 152 | + object='document:budget', |
| 153 | + expectation=True, |
| 154 | + ), |
| 155 | + ClientAssertion( |
| 156 | + user='user:anne', |
| 157 | + relation='reader', |
| 158 | + object='document:roadmap', |
| 159 | + expectation=False, |
| 160 | + ), |
| 161 | + ]) |
| 162 | + print('Assertions updated') |
| 163 | + |
| 164 | + # ReadAssertions |
| 165 | + print('Reading Assertions') |
| 166 | + response = await fga_client.read_assertions() |
| 167 | + print(f"Assertions: {response.assertions}") |
| 168 | + |
| 169 | + # DeleteStore |
| 170 | + print('Deleting Current Store') |
| 171 | + await fga_client.delete_store() |
| 172 | + print(f"Deleted Store: {store_name}") |
| 173 | + |
| 174 | + |
| 175 | +asyncio.run(main()) |
0 commit comments