@@ -44,6 +44,22 @@ class EnforcerInput {
4444 }
4545}
4646
47+ class CheckUrlInput {
48+ public final User user ;
49+ public final String http_method ;
50+ public final String url ;
51+ public final String tenant ;
52+ public final HashMap <String , Object > context ;
53+
54+ CheckUrlInput (User user , String http_method , String url , String tenant , HashMap <String , Object > context ) {
55+ this .user = user ;
56+ this .http_method = http_method ;
57+ this .url = url ;
58+ this .tenant = tenant ;
59+ this .context = context ;
60+ }
61+ }
62+
4763/**
4864* The {@code OpaResult} class represents the result of a Permit enforcement check returned by the policy agent.
4965*/
@@ -118,6 +134,7 @@ public boolean check(User user, String action, Resource resource, Context contex
118134 .addHeader ("Content-Type" , "application/json" )
119135 .addHeader ("Authorization" , String .format ("Bearer %s" , this .config .getToken ()))
120136 .addHeader ("X-Permit-SDK-Version" , String .format ("java:%s" , this .config .version ))
137+ .addHeader ("X-Tenant-ID" , normalizedResource .getTenant ()) // sharding key
121138 .build ();
122139
123140 try (Response response = client .newCall (request ).execute ()) {
@@ -172,4 +189,76 @@ public boolean check(User user, String action, Resource resource, Context contex
172189 public boolean check (User user , String action , Resource resource ) throws IOException {
173190 return this .check (user , action , resource , new Context ());
174191 }
192+
193+ @ Override
194+ public boolean checkUrl (User user , String httpMethod , String url , String tenant , Context context ) throws IOException {
195+ CheckUrlInput input = new CheckUrlInput (
196+ user ,
197+ httpMethod ,
198+ url ,
199+ tenant ,
200+ context
201+ );
202+
203+ // request body
204+ Gson gson = new Gson ();
205+ String requestBody = gson .toJson (input );
206+ RequestBody body = RequestBody .create (requestBody , MediaType .parse ("application/json" ));
207+
208+ // create the request
209+ String apiUrl = String .format ("%s/allowed_url" , this .config .getPdpAddress ());
210+ Request request = new Request .Builder ()
211+ .url (apiUrl )
212+ .post (body )
213+ .addHeader ("Content-Type" , "application/json" )
214+ .addHeader ("Authorization" , String .format ("Bearer %s" , this .config .getToken ()))
215+ .addHeader ("X-Permit-SDK-Version" , String .format ("java:%s" , this .config .version ))
216+ .addHeader ("X-Tenant-ID" , tenant ) // sharding key
217+ .build ();
218+
219+ try (Response response = client .newCall (request ).execute ()) {
220+ if (!response .isSuccessful ()) {
221+ String errorMessage = String .format (
222+ "Error in permit.checkUrl(%s, %s, %s, %s): got unexpected status code %d" ,
223+ user .toString (),
224+ httpMethod ,
225+ url ,
226+ tenant ,
227+ response .code ()
228+ );
229+ logger .error (errorMessage );
230+ throw new IOException (errorMessage );
231+ }
232+ ResponseBody responseBody = response .body ();
233+ if (responseBody == null ) {
234+ String errorMessage = String .format (
235+ "Error in permit.check(%s, %s, %s, %s): got empty response" ,
236+ user ,
237+ httpMethod ,
238+ url ,
239+ tenant
240+ );
241+ logger .error (errorMessage );
242+ throw new IOException (errorMessage );
243+ }
244+ String responseString = responseBody .string ();
245+ OpaResult result = gson .fromJson (responseString , OpaResult .class );
246+ if (this .config .isDebugMode ()) {
247+ logger .info (String .format (
248+ "permit.check(%s, %s, %s, %s) = %s" ,
249+ user ,
250+ httpMethod ,
251+ url ,
252+ tenant ,
253+ result .allow .toString ()
254+ ));
255+ }
256+ return result .allow ;
257+ }
258+ }
259+
260+ @ Override
261+ public boolean checkUrl (User user , String httpMethod , String url , String tenant ) throws IOException {
262+ return this .checkUrl (user , httpMethod , url , tenant , new Context ());
263+ }
175264}
0 commit comments