diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2/authorizationservice/AuthorizationService.java b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2/authorizationservice/AuthorizationService.java index a5a4906a0..94a0742c4 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2/authorizationservice/AuthorizationService.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2/authorizationservice/AuthorizationService.java @@ -75,7 +75,7 @@ public void init(Router router) throws Exception { if (isUseJWTForClientAuth()) { JWSSigner = new JWSSigner(PEMSupport.getInstance().parseKey(getSslParser().getKey().getPrivate().get(router.getResolverMap(), router.getBaseLocation())), - getSslParser().getKey().getCertificates().getFirst().get(router.getResolverMap(), router.getBaseLocation())); + getSslParser().getKey().getCertificates().get(0).get(router.getResolverMap(), router.getBaseLocation())); } setHttpClient(router.getHttpClientFactory().createClient(getHttpClientConfiguration())); diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2PermissionCheckerInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2PermissionCheckerInterceptor.java index 5e241499e..ce75d3a86 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2PermissionCheckerInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2PermissionCheckerInterceptor.java @@ -32,6 +32,8 @@ import java.util.List; import java.util.function.Function; +import static com.predic8.membrane.core.exchange.Exchange.OAUTH2; + @MCElement(name = "oauth2PermissionChecker") public class OAuth2PermissionCheckerInterceptor extends AbstractInterceptor { @@ -95,7 +97,7 @@ public void setField(String field) { @Override public Object evaluate(Exchange exc) { - Object oauth2prop = exc.getProperty("oauth2"); + Object oauth2prop = exc.getProperty(OAUTH2); if (oauth2prop == null) return null; return ((OAuth2AnswerParameters)oauth2prop).getUserinfo().get("groups"); diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2Resource2Interceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2Resource2Interceptor.java index deb24cce5..64dde4b7d 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2Resource2Interceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/oauth2client/OAuth2Resource2Interceptor.java @@ -27,6 +27,7 @@ import com.predic8.membrane.core.http.Response; import com.predic8.membrane.core.interceptor.AbstractInterceptorWithSession; import com.predic8.membrane.core.interceptor.Outcome; +import com.predic8.membrane.core.interceptor.oauth2.OAuth2AnswerParameters; import com.predic8.membrane.core.interceptor.oauth2.OAuth2Statistics; import com.predic8.membrane.core.interceptor.oauth2.ParamNames; import com.predic8.membrane.core.interceptor.oauth2.authorizationservice.AuthorizationService; @@ -44,8 +45,8 @@ import java.util.List; import java.util.Map; -import static com.predic8.membrane.core.http.Header.X_FORWARDED_HOST; -import static com.predic8.membrane.core.http.Header.X_FORWARDED_PROTO; +import static com.predic8.membrane.core.exchange.Exchange.OAUTH2; +import static com.predic8.membrane.core.http.Header.*; import static com.predic8.membrane.core.interceptor.oauth2client.rf.StateManager.generateNewState; import static com.predic8.membrane.core.interceptor.oauth2client.rf.OAuthUtils.isOAuth2RedirectRequest; import static com.predic8.membrane.core.interceptor.oauth2client.temp.OAuth2Constants.*; @@ -77,6 +78,7 @@ public class OAuth2Resource2Interceptor extends AbstractInterceptorWithSession { private String logoutUrl; private String afterLogoutUrl; private List loginParameters = new ArrayList<>(); + private boolean appendAccessTokenToRequest; @Override public void init() throws Exception { @@ -150,12 +152,14 @@ public final Outcome handleRequestInternal(Exchange exc) throws Exception { if (session.isVerified()) { applyBackendAuthorization(exc, session); statistics.successfulRequest(); + appendAccessTokenToRequest(exc); return Outcome.CONTINUE; } if (handleRequest(exc, session)) { if (exc.getResponse() == null && exc.getRequest() != null && session.isVerified() && session.hasOAuth2Answer()) { exc.setProperty(Exchange.OAUTH2, session.getOAuth2AnswerParameters()); + appendAccessTokenToRequest(exc); return Outcome.CONTINUE; } @@ -267,7 +271,7 @@ private void readBodyFromStreamIntoMemory(Exchange exc) { } private boolean handleRequest(Exchange exc, Session session) throws Exception { - String path = uriFactory.create(exc.getDestinations().getFirst()).getPath(); + String path = uriFactory.create(exc.getDestinations().get(0)).getPath(); if (path == null) { return false; @@ -294,6 +298,17 @@ private void doOriginalRequest(Exchange exc, AbstractExchange originalRequest) { exc.setOriginalHostHeader(xForwardedHost); } + private void appendAccessTokenToRequest(Exchange exc) { + if (!appendAccessTokenToRequest) + return; + if (exc.getProperty(OAUTH2) == null) + return; + OAuth2AnswerParameters params = (OAuth2AnswerParameters) exc.getProperty(OAUTH2); + if (params.getAccessToken() == null) + return; + exc.getRequest().getHeader().setValue(AUTHORIZATION, "Bearer " + params.getAccessToken()); + } + @Override public String getShortDescription() { return "Client of the oauth2 authentication process.\n" + statistics.toString(); @@ -403,4 +418,13 @@ public List getLoginParameters() { public void setLoginParameters(List loginParameters) { this.loginParameters = loginParameters; } + + public boolean isAppendAccessTokenToRequest() { + return appendAccessTokenToRequest; + } + + @MCAttribute + public void setAppendAccessTokenToRequest(boolean appendAccessTokenToRequest) { + this.appendAccessTokenToRequest = appendAccessTokenToRequest; + } } diff --git a/core/src/main/java/com/predic8/membrane/core/lang/spel/functions/BuiltInFunctions.java b/core/src/main/java/com/predic8/membrane/core/lang/spel/functions/BuiltInFunctions.java index fa71969fb..186c8899a 100644 --- a/core/src/main/java/com/predic8/membrane/core/lang/spel/functions/BuiltInFunctions.java +++ b/core/src/main/java/com/predic8/membrane/core/lang/spel/functions/BuiltInFunctions.java @@ -41,7 +41,8 @@ public class BuiltInFunctions { public static boolean isLoggedIn(String beanName, ExchangeEvaluationContext ctx) { try { - return ((AbstractInterceptorWithSession) requireNonNull(ctx.getBeanResolver()).resolve(ctx, beanName)) + + return ((AbstractInterceptorWithSession) requireNonNull(ctx.getExchange().getHandler().getTransport().getRouter().getBeanFactory()).getBean(beanName)) .getSessionManager().getSession(ctx.getExchange()).isVerified(); } catch (Exception e) { log.info("Failed to resolve bean with name '" + beanName + "'");