From 6aceaa84699f62c409a4ebc59b9b5b99583034b6 Mon Sep 17 00:00:00 2001 From: Oleksii Bielik Date: Tue, 23 Apr 2024 10:08:41 +0300 Subject: [PATCH] feat: Add EL support for the cache resource field --- pom.xml | 2 +- src/main/java/io/gravitee/policy/oauth2/Oauth2Policy.java | 5 ++++- .../java/io/gravitee/policy/v3/oauth2/Oauth2PolicyV3.java | 6 ++++++ src/main/resources/schemas/schema-form.json | 2 +- .../java/io/gravitee/policy/oauth2/Oauth2PolicyTest.java | 1 + 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index b42b1784..8a037262 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ io.gravitee.policy gravitee-policy-oauth2 - 3.0.4 + 3.0.5 Gravitee.io APIM - Policy - OAuth2 Check access token validity during request processing using token introspection diff --git a/src/main/java/io/gravitee/policy/oauth2/Oauth2Policy.java b/src/main/java/io/gravitee/policy/oauth2/Oauth2Policy.java index f6609ca8..1a07f49d 100644 --- a/src/main/java/io/gravitee/policy/oauth2/Oauth2Policy.java +++ b/src/main/java/io/gravitee/policy/oauth2/Oauth2Policy.java @@ -315,7 +315,10 @@ private Cache getPolicyTokenIntrospectionCache(HttpExecutionContext ctx) { if (oAuth2PolicyConfiguration.getOauthCacheResource() != null) { CacheResource cacheResource = ctx .getComponent(ResourceManager.class) - .getResource(oAuth2PolicyConfiguration.getOauthCacheResource(), CacheResource.class); + .getResource( + ctx.getTemplateEngine().getValue(oAuth2PolicyConfiguration.getOauthCacheResource(), String.class), + CacheResource.class + ); if (cacheResource != null) { return cacheResource.getCache(ctx); } diff --git a/src/main/java/io/gravitee/policy/v3/oauth2/Oauth2PolicyV3.java b/src/main/java/io/gravitee/policy/v3/oauth2/Oauth2PolicyV3.java index 3d888d89..d3e0bb5b 100644 --- a/src/main/java/io/gravitee/policy/v3/oauth2/Oauth2PolicyV3.java +++ b/src/main/java/io/gravitee/policy/v3/oauth2/Oauth2PolicyV3.java @@ -128,6 +128,12 @@ public void onRequest(Request request, Response response, ExecutionContext execu // Set access_token in context executionContext.setAttribute(CONTEXT_ATTRIBUTE_OAUTH_ACCESS_TOKEN, accessToken); + if (oAuth2PolicyConfiguration.getOauthCacheResource() != null) { + oAuth2PolicyConfiguration.setOauthCacheResource( + executionContext.getTemplateEngine().getValue(oAuth2PolicyConfiguration.getOauthCacheResource(), String.class) + ); + } + CacheResource cacheResource = executionContext .getComponent(ResourceManager.class) .getResource(oAuth2PolicyConfiguration.getOauthCacheResource(), CacheResource.class); diff --git a/src/main/resources/schemas/schema-form.json b/src/main/resources/schemas/schema-form.json index 793f9e79..f93621e7 100644 --- a/src/main/resources/schemas/schema-form.json +++ b/src/main/resources/schemas/schema-form.json @@ -22,7 +22,7 @@ }, "oauthCacheResource": { "title": "Cache resource", - "description": "Cache resource used to store the tokens.", + "description": "Cache resource used to store the tokens. Supports EL.", "type": "string", "x-schema-form": { "event": { diff --git a/src/test/java/io/gravitee/policy/oauth2/Oauth2PolicyTest.java b/src/test/java/io/gravitee/policy/oauth2/Oauth2PolicyTest.java index a493af45..22e8b8ef 100644 --- a/src/test/java/io/gravitee/policy/oauth2/Oauth2PolicyTest.java +++ b/src/test/java/io/gravitee/policy/oauth2/Oauth2PolicyTest.java @@ -435,6 +435,7 @@ void shouldPutIntrospectionToCache() throws IOException { private void prepareCacheResource() { when(configuration.getOauthCacheResource()).thenReturn(OAUTH_CACHE_RESOURCE); + when(templateEngine.getValue(OAUTH_CACHE_RESOURCE, String.class)).thenReturn(OAUTH_CACHE_RESOURCE); when(cacheResource.getCache(any(HttpExecutionContext.class))).thenReturn(cache); when(resourceManager.getResource(OAUTH_CACHE_RESOURCE, CacheResource.class)).thenReturn(cacheResource); }