Skip to content

Commit

Permalink
Append access token to request (#842)
Browse files Browse the repository at this point in the history
* append access token to request

* fixed isLoggedIn beanFactory resolution
  • Loading branch information
rrayst authored Feb 6, 2024
1 parent 8325b7c commit d39631a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class OAuth2Resource2Interceptor extends AbstractInterceptorWithSession {
private String logoutUrl;
private String afterLogoutUrl;
private List<LoginParameter> loginParameters = new ArrayList<>();
private boolean appendAccessTokenToRequest;

@Override
public void init() throws Exception {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -403,4 +418,13 @@ public List<LoginParameter> getLoginParameters() {
public void setLoginParameters(List<LoginParameter> loginParameters) {
this.loginParameters = loginParameters;
}

public boolean isAppendAccessTokenToRequest() {
return appendAccessTokenToRequest;
}

@MCAttribute
public void setAppendAccessTokenToRequest(boolean appendAccessTokenToRequest) {
this.appendAccessTokenToRequest = appendAccessTokenToRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "'");
Expand Down

0 comments on commit d39631a

Please sign in to comment.