Skip to content

Commit 7d448fb

Browse files
authored
Add FGA API support (#208)
* Add FGA API support * Align model objects * Update Readme * delete * Update pom * Add integration tests and FGA schema for functional validation * lint * Try again * fix test
1 parent 8e98579 commit 7d448fb

14 files changed

Lines changed: 756 additions & 1 deletion

File tree

README.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ These sections show how to use the SDK to perform API management functions. Befo
7070
8. [Manage Flows](#manage-flows)
7171
9. [Manage JWTs](#manage-jwts)
7272
10. [Audit](#audit)
73-
11. [Manage Project](#manage-project)
73+
11. [FGA (Fine-Grained Authorization)](#fga-fine-grained-authorization)
74+
12. [Manage Project](#manage-project)
7475

7576
If you wish to run any of our code samples and play with them, check out our [Code Examples](#code-examples) section.
7677

@@ -1265,6 +1266,94 @@ try {
12651266
// Handle the error
12661267
}
12671268
```
1269+
1270+
### FGA (Fine-Grained Authorization)
1271+
1272+
You can manage fine-grained authorization schemas, relations, and resource metadata:
1273+
1274+
```java
1275+
// Create and manage authorization schemas
1276+
FGAService fs = descopeClient.getManagementServices().getFgaService();
1277+
1278+
String dsl = "model AuthZ 1.0\n" +
1279+
"type user\n" +
1280+
"type document\n" +
1281+
" relation owner: user\n" +
1282+
" relation editor: user\n" +
1283+
" relation viewer: user";
1284+
1285+
try {
1286+
FGASchema schema = new FGASchema(dsl);
1287+
fs.saveSchema(schema);
1288+
} catch (DescopeException de) {
1289+
// Handle the error
1290+
}
1291+
1292+
// Load the current authorization schema
1293+
try {
1294+
FGASchema schema = fs.loadSchema();
1295+
// Do something with schema.getDsl()
1296+
} catch (DescopeException de) {
1297+
// Handle the error
1298+
}
1299+
1300+
// Create relations between resources and users
1301+
List<FGARelation> relations = Arrays.asList(
1302+
new FGARelation("doc1", "document", "owner", "user123", "user"),
1303+
new FGARelation("doc1", "document", "viewer", "user456", "user")
1304+
);
1305+
1306+
try {
1307+
fs.createRelations(relations);
1308+
} catch (DescopeException de) {
1309+
// Handle the error
1310+
}
1311+
1312+
// Check if relations are allowed
1313+
try {
1314+
List<FGACheckResult> results = fs.check(relations);
1315+
for (FGACheckResult result : results) {
1316+
// Do something with result.isAllowed()
1317+
}
1318+
} catch (DescopeException de) {
1319+
// Handle the error
1320+
}
1321+
1322+
// Delete relations
1323+
try {
1324+
fs.deleteRelations(relations);
1325+
} catch (DescopeException de) {
1326+
// Handle the error
1327+
}
1328+
1329+
// Save resource metadata
1330+
List<FGAResourceDetails> resourceDetails = Arrays.asList(
1331+
new FGAResourceDetails("doc1", "document", "Important Document"),
1332+
new FGAResourceDetails("doc2", "document", "Public Document")
1333+
);
1334+
1335+
try {
1336+
fs.saveResourcesDetails(resourceDetails);
1337+
} catch (DescopeException de) {
1338+
// Handle the error
1339+
}
1340+
1341+
// Load resource metadata
1342+
List<FGAResourceIdentifier> identifiers = Arrays.asList(
1343+
new FGAResourceIdentifier("doc1", "document")
1344+
);
1345+
1346+
try {
1347+
List<FGAResourceDetails> details = fs.loadResourcesDetails(identifiers);
1348+
for (FGAResourceDetails detail : details) {
1349+
// Do something with detail.getDisplayName()
1350+
}
1351+
} catch (DescopeException de) {
1352+
// Handle the error
1353+
}
1354+
1355+
```
1356+
12681357
### Manage Project
12691358

12701359
You can change the project name, as well as to clone the current project to a new one.

src/main/java/com/descope/literals/Routes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ public static class ManagementEndPoints {
215215
public static final String MANAGEMENT_AUTHZ_RE_TARGET_ALL = "/v1/mgmt/authz/re/targetall";
216216
public static final String MANAGEMENT_AUTHZ_GET_MODIFIED = "/v1/mgmt/authz/getmodified";
217217

218+
// FGA (Fine-grained Authorization)
219+
public static final String MANAGEMENT_FGA_SAVE_SCHEMA = "/v1/mgmt/fga/schema";
220+
public static final String MANAGEMENT_FGA_LOAD_SCHEMA = "/v1/mgmt/fga/schema";
221+
public static final String MANAGEMENT_FGA_CREATE_RELATIONS = "/v1/mgmt/fga/relations";
222+
public static final String MANAGEMENT_FGA_DELETE_RELATIONS = "/v1/mgmt/fga/relations/delete";
223+
public static final String MANAGEMENT_FGA_CHECK = "/v1/mgmt/fga/check";
224+
public static final String MANAGEMENT_FGA_RESOURCES_LOAD = "/v1/mgmt/fga/resources/load";
225+
public static final String MANAGEMENT_FGA_RESOURCES_SAVE = "/v1/mgmt/fga/resources/save";
226+
218227
// Password settings
219228
public static final String MANAGEMENT_PASSWORD_SETTINGS = "/v1/mgmt/password/settings";
220229

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGACheckInfo {
13+
private boolean direct;
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGACheckResult {
13+
private boolean allowed;
14+
private FGARelation relation;
15+
private FGACheckInfo info;
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGARelation {
13+
private String resource;
14+
private String resourceType;
15+
private String relation;
16+
private String target;
17+
private String targetType;
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGAResourceDetails {
13+
private String resourceId;
14+
private String resourceType;
15+
private String displayName;
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGAResourceIdentifier {
13+
private String resourceId;
14+
private String resourceType;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.descope.model.fga;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class FGASchema {
13+
private String dsl;
14+
}

src/main/java/com/descope/model/mgmt/ManagementServices.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.descope.sdk.mgmt.AccessKeyService;
44
import com.descope.sdk.mgmt.AuditService;
55
import com.descope.sdk.mgmt.AuthzService;
6+
import com.descope.sdk.mgmt.FGAService;
67
import com.descope.sdk.mgmt.FlowService;
78
import com.descope.sdk.mgmt.GroupService;
89
import com.descope.sdk.mgmt.InboundAppsService;
@@ -35,6 +36,7 @@ public class ManagementServices {
3536
GroupService groupService;
3637
AuditService auditService;
3738
AuthzService authzService;
39+
FGAService fgaService;
3840
ProjectService projectService;
3941
PasswordSettingsService passwordSettingsService;
4042
OutboundAppsService outboundAppsService;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.descope.sdk.mgmt;
2+
3+
import com.descope.exception.DescopeException;
4+
import com.descope.model.fga.FGACheckResult;
5+
import com.descope.model.fga.FGARelation;
6+
import com.descope.model.fga.FGAResourceDetails;
7+
import com.descope.model.fga.FGAResourceIdentifier;
8+
import com.descope.model.fga.FGASchema;
9+
import java.util.List;
10+
11+
/**
12+
* Provides functions for managing Fine-Grained Authorization (FGA) in a project.
13+
* FGA allows for creating and managing schemas and relations using a Zanzibar-like model.
14+
*/
15+
public interface FGAService {
16+
17+
/**
18+
* Creates or updates an FGA schema for the project.
19+
* The schema is provided in the AuthZ 1.0 DSL format.
20+
*
21+
* @param schema the FGA schema containing the DSL definition
22+
* @throws DescopeException if the operation fails
23+
*/
24+
void saveSchema(FGASchema schema) throws DescopeException;
25+
26+
/**
27+
* Loads the current FGA schema for the project.
28+
*
29+
* @return the current FGA schema
30+
* @throws DescopeException if the operation fails
31+
*/
32+
FGASchema loadSchema() throws DescopeException;
33+
34+
/**
35+
* Creates new FGA relations (tuples) based on the existing schema.
36+
*
37+
* @param relations list of relations to create
38+
* @throws DescopeException if the operation fails
39+
*/
40+
void createRelations(List<FGARelation> relations) throws DescopeException;
41+
42+
/**
43+
* Deletes existing FGA relations (tuples).
44+
*
45+
* @param relations list of relations to delete
46+
* @throws DescopeException if the operation fails
47+
*/
48+
void deleteRelations(List<FGARelation> relations) throws DescopeException;
49+
50+
/**
51+
* Checks if the given FGA relations are satisfied.
52+
* This is a read-only operation that validates whether relations exist.
53+
*
54+
* @param relations list of relations to check
55+
* @return list of check results indicating whether each relation is allowed
56+
* @throws DescopeException if the operation fails
57+
*/
58+
List<FGACheckResult> check(List<FGARelation> relations) throws DescopeException;
59+
60+
/**
61+
* Loads detailed information for the given resource identifiers.
62+
*
63+
* @param resourceIdentifiers list of resource identifiers to load details for
64+
* @return list of resource details
65+
* @throws DescopeException if the operation fails
66+
*/
67+
List<FGAResourceDetails> loadResourcesDetails(List<FGAResourceIdentifier> resourceIdentifiers)
68+
throws DescopeException;
69+
70+
/**
71+
* Saves detailed information for the given resources.
72+
*
73+
* @param resourcesDetails list of resource details to save
74+
* @throws DescopeException if the operation fails
75+
*/
76+
void saveResourcesDetails(List<FGAResourceDetails> resourcesDetails) throws DescopeException;
77+
}

0 commit comments

Comments
 (0)