|
1 | 1 | package io.permit.sdk.enforcement; |
2 | 2 |
|
3 | | -import com.google.common.primitives.Booleans; |
4 | | -import com.google.gson.Gson; |
5 | | -import io.permit.sdk.PermitConfig; |
6 | | -import io.permit.sdk.api.HttpLoggingInterceptor; |
7 | | -import io.permit.sdk.api.PermitApiError; |
8 | | -import io.permit.sdk.openapi.models.ConditionSetRuleRead; |
9 | | -import io.permit.sdk.util.Context; |
10 | | -import io.permit.sdk.util.ContextStore; |
11 | | - |
12 | 3 | import java.io.IOException; |
13 | 4 | import java.util.ArrayList; |
14 | 5 | import java.util.Arrays; |
15 | 6 | import java.util.HashMap; |
16 | 7 | import java.util.List; |
| 8 | +import java.util.Map; |
17 | 9 | import java.util.stream.Collectors; |
18 | 10 |
|
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import com.google.common.primitives.Booleans; |
| 15 | +import com.google.gson.Gson; |
| 16 | + |
| 17 | +import io.permit.sdk.PermitConfig; |
| 18 | +import io.permit.sdk.api.HttpLoggingInterceptor; |
| 19 | +import io.permit.sdk.api.PermitApiError; |
| 20 | +import io.permit.sdk.util.Context; |
| 21 | +import io.permit.sdk.util.ContextStore; |
19 | 22 | import okhttp3.MediaType; |
20 | 23 | import okhttp3.OkHttpClient; |
21 | 24 | import okhttp3.Request; |
22 | 25 | import okhttp3.RequestBody; |
23 | 26 | import okhttp3.Response; |
24 | 27 | import okhttp3.ResponseBody; |
25 | | -import org.slf4j.Logger; |
26 | | -import org.slf4j.LoggerFactory; |
27 | 28 |
|
28 | 29 | /** |
29 | 30 | * The {@code EnforcerInput} class represents the input data for the Permit PDP enforcement API. |
@@ -448,6 +449,63 @@ public UserPermissions getUserPermissions(GetUserPermissionsQuery input) throws |
448 | 449 | return result; |
449 | 450 | } |
450 | 451 |
|
| 452 | + |
| 453 | + @Override |
| 454 | + public UserPermissions getUserPermissionsFromOPA(GetUserPermissionsQuery input) throws IOException, PermitApiError { |
| 455 | + // request body |
| 456 | + Gson gson = new Gson(); |
| 457 | + |
| 458 | + // Inner map for the nested JSON |
| 459 | + Map<String, Object> innerMap = new HashMap<>(); |
| 460 | + innerMap.put("user", input.user); |
| 461 | + innerMap.put("tenants", input.tenants); |
| 462 | + innerMap.put("resource_types", input.resource_types); |
| 463 | + innerMap.put("resources", input.resources); |
| 464 | + innerMap.put("context", input.context); |
| 465 | + |
| 466 | + // Outer map wrapping the inner map |
| 467 | + Map<String, Object> outerMap = new HashMap<>(); |
| 468 | + outerMap.put("input", innerMap); |
| 469 | + |
| 470 | + // Serialize to JSON |
| 471 | + String requestBody = gson.toJson(outerMap); |
| 472 | + |
| 473 | + RequestBody body = RequestBody.create(requestBody, MediaType.parse("application/json")); |
| 474 | + String PERMISSIONS_PATH = "permit/user_permissions/permissions"; |
| 475 | + String url = String.format("%s/v1/data/%s", this.config.getOpaAddress(), PERMISSIONS_PATH); |
| 476 | + Request request = new Request.Builder() |
| 477 | + .url(url) |
| 478 | + .post(body) |
| 479 | + .addHeader("Content-Type", "application/json") |
| 480 | + .addHeader("Authorization", String.format("Bearer %s", this.config.getToken())) |
| 481 | + .addHeader("X-Permit-SDK-Version", String.format("java:%s", this.config.version)) |
| 482 | + .build(); |
| 483 | + |
| 484 | + String requestRepr = String.format( |
| 485 | + "permit.getUserPermissions(%s, %s, %s, %s)", |
| 486 | + input.user.toString(), |
| 487 | + input.tenants != null ? input.tenants.toString() : "null", |
| 488 | + input.resource_types != null ? input.resource_types.toString() : "null", |
| 489 | + input.resources != null ? input.resources.toString() : "null" |
| 490 | + ); |
| 491 | + |
| 492 | + UserPermissionsOpa result = this.callApiAndParseJson(request, requestRepr, UserPermissionsOpa.class); |
| 493 | + |
| 494 | + UserPermissions userPermissions = new UserPermissions(); |
| 495 | + userPermissions.putAll(result.getResult()); |
| 496 | + |
| 497 | + if (this.config.isDebugMode()) { |
| 498 | + logger.info(String.format( |
| 499 | + "%s => returned %d permissions on %d objects", |
| 500 | + requestRepr, |
| 501 | + userPermissions.values().stream().map(obj -> obj.permissions.size()).reduce(0, Integer::sum), |
| 502 | + userPermissions.keySet().size() |
| 503 | + )); |
| 504 | + } |
| 505 | + |
| 506 | + return userPermissions; |
| 507 | + } |
| 508 | + |
451 | 509 | private List<TenantDetails> getUserTenants(GetUserTenantsQuery input) throws IOException, PermitApiError { |
452 | 510 | // request body |
453 | 511 | Gson gson = new Gson(); |
|
0 commit comments