Skip to content

Commit

Permalink
feat: postProcessAttributesContext should has reference to
Browse files Browse the repository at this point in the history
ProfileRequestContext #172
  • Loading branch information
yurem committed Sep 26, 2023
1 parent 1eb0737 commit 9503490
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void doExecute(@Nonnull final ProfileRequestContext profileRequestCont
}
}

PostProcessAttributesContext context = buildContext(idpAttributeMap);
PostProcessAttributesContext context = buildContext(profileRequestContext, idpAttributeMap);

for (String attr : idpAttributeMap.keySet()) {
LOG.info("------------------------attr: {}", attr);
Expand All @@ -101,10 +101,9 @@ protected void doExecute(@Nonnull final ProfileRequestContext profileRequestCont
LOG.debug("Executed script method 'updateAttributes' with result {}", result);
}

private PostProcessAttributesContext buildContext(final Map<String,IdPAttribute> idpAttributeMap) {

private PostProcessAttributesContext buildContext(ProfileRequestContext profileRequestContext, final Map<String,IdPAttribute> idpAttributeMap) {
PostProcessAttributesContext context = new PostProcessAttributesContext();

context.setProfileRequestContext(profileRequestContext);
context.setAttributeReleaseAction(this);
context.setIdpAttributeMap(idpAttributeMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.Serializable;
import java.util.Map;

import org.opensaml.profile.context.ProfileRequestContext;

import net.shibboleth.idp.attribute.IdPAttribute;

/**
Expand All @@ -15,10 +17,19 @@ public class PostProcessAttributesContext implements Serializable {

private static final long serialVersionUID = 1822377169827670256L;

private ProfileRequestContext profileRequestContext;
private GluuReleaseAttributesPostProcessor releaseAttributesPostProcessor;
private Map<String,IdPAttribute> idpAttributeMap;


public ProfileRequestContext getProfileRequestContext() {
return profileRequestContext;
}

public void setProfileRequestContext(ProfileRequestContext profileRequestContext) {
this.profileRequestContext = profileRequestContext;
}

public void setAttributeReleaseAction(GluuReleaseAttributesPostProcessor releaseAttributesPostProcessor) {
this.releaseAttributesPostProcessor = releaseAttributesPostProcessor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
import org.springframework.core.env.Environment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.webflow.context.ExternalContextHolder;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.engine.impl.FlowExecutionImpl;
import org.springframework.webflow.execution.FlowExecutionFactory;
import org.springframework.webflow.execution.FlowExecutionKey;
import org.springframework.webflow.execution.repository.FlowExecutionRepository;
import org.springframework.webflow.executor.FlowExecutionResult;
import org.springframework.webflow.executor.FlowExecutorImpl;

import net.shibboleth.idp.attribute.IdPAttribute;
import net.shibboleth.idp.authn.AuthnEventIds;
Expand Down Expand Up @@ -115,7 +123,12 @@ public void init(final ServletConfig config) throws ServletException {

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
try {
if (!checkRequest(request, response)) {
return;
}

try {
ExternalContextHolder.setExternalContext(new ServletExternalContext(request.getServletContext(), request, response));

final String requestUrl = request.getRequestURL().toString();
LOG.trace("Get request to: '{}'", requestUrl);
Expand Down Expand Up @@ -157,15 +170,15 @@ public String getParameter(String name) {
}

// Get authentication key from request
final String authenticationKey = ExternalAuthentication.startExternalAuthentication(externalRequest);
final String flowExecutionKey = ExternalAuthentication.startExternalAuthentication(externalRequest);

// Get external authentication properties
final boolean force = Boolean.parseBoolean(request.getAttribute(ExternalAuthentication.FORCE_AUTHN_PARAM).toString());

// It's an authentication
if (!authorizationResponse) {
LOG.debug("Initiating oxAuth login redirect");
startLoginRequest(request, response, authenticationKey, force);
startLoginRequest(request, response, flowExecutionKey, force);
return;
}

Expand All @@ -176,22 +189,35 @@ public String getParameter(String name) {
LOG.error("The state in session and in request are not equals");

// Re-init login page
startLoginRequest(request, response, authenticationKey, force);
startLoginRequest(request, response, flowExecutionKey, force);
return;
}

processAuthorizationResponse(request, response, authenticationKey);
processAuthorizationResponse(request, response, flowExecutionKey);

} catch (final ExternalAuthenticationException ex) {
LOG.warn("Error processing oxAuth authentication request", ex);
LOG.error("Error processing oxAuth authentication request", ex);
loadErrorPage(request, response);

} catch (final Exception ex) {
LOG.error("Something unexpected happened", ex);
request.setAttribute(ExternalAuthentication.AUTHENTICATION_ERROR_KEY, AuthnEventIds.AUTHN_EXCEPTION);
} finally {
ExternalContextHolder.setExternalContext(null);
}
}

private final boolean checkRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
// Check whether a session is required.
if (request.getSession(false) == null) {
LOG.error("Pre-existing session required but none found");
loadErrorPage(request, response);

return false;
}

return true;
}

private void processAuthorizationResponse(final HttpServletRequest request, final HttpServletResponse response, final String authenticationKey)
throws ExternalAuthenticationException, IOException {
try {
Expand Down Expand Up @@ -242,14 +268,14 @@ private void processAuthorizationResponse(final HttpServletRequest request, fina
if(!idpAttributes.isEmpty()) {
LOG.debug("Storing generated idp attributes");
ProfileRequestContext prContext = ExternalAuthentication.getProfileRequestContext(authenticationKey, request);
GluuScratchContext gluuScratchContext = prContext.getSubcontext(GluuScratchContext.class,true);
GluuScratchContext gluuScratchContext = prContext.getSubcontext(GluuScratchContext.class, true);
gluuScratchContext.setIdpAttributes(idpAttributes);
}

LOG.debug("Created an IdP subject instance with principals for {} ", userProfile.getId());
final Set<Principal> userPrincipals = new HashSet<Principal>();
userPrincipals.add(new UsernamePrincipal(userProfile.getId()));
request.setAttribute(ExternalAuthentication.SUBJECT_KEY, new Subject(false, userPrincipals,Collections.emptySet(),Collections.emptySet()));
request.setAttribute(ExternalAuthentication.SUBJECT_KEY, new Subject(false, userPrincipals, Collections.emptySet(),Collections.emptySet()));

if (authenticationContext != null) {
String usedAcr = userProfile.getUsedAcr();
Expand Down

0 comments on commit 9503490

Please sign in to comment.