Skip to content

Commit 9503490

Browse files
committed
feat: postProcessAttributesContext should has reference to
ProfileRequestContext #172
1 parent 1eb0737 commit 9503490

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

shib-oxauth-authn/src/main/java/org/gluu/idp/consent/processor/GluuReleaseAttributesPostProcessor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected void doExecute(@Nonnull final ProfileRequestContext profileRequestCont
8282
}
8383
}
8484

85-
PostProcessAttributesContext context = buildContext(idpAttributeMap);
85+
PostProcessAttributesContext context = buildContext(profileRequestContext, idpAttributeMap);
8686

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

104-
private PostProcessAttributesContext buildContext(final Map<String,IdPAttribute> idpAttributeMap) {
105-
104+
private PostProcessAttributesContext buildContext(ProfileRequestContext profileRequestContext, final Map<String,IdPAttribute> idpAttributeMap) {
106105
PostProcessAttributesContext context = new PostProcessAttributesContext();
107-
106+
context.setProfileRequestContext(profileRequestContext);
108107
context.setAttributeReleaseAction(this);
109108
context.setIdpAttributeMap(idpAttributeMap);
110109

shib-oxauth-authn/src/main/java/org/gluu/idp/consent/processor/PostProcessAttributesContext.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.Serializable;
44
import java.util.Map;
55

6+
import org.opensaml.profile.context.ProfileRequestContext;
7+
68
import net.shibboleth.idp.attribute.IdPAttribute;
79

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

1618
private static final long serialVersionUID = 1822377169827670256L;
1719

20+
private ProfileRequestContext profileRequestContext;
1821
private GluuReleaseAttributesPostProcessor releaseAttributesPostProcessor;
1922
private Map<String,IdPAttribute> idpAttributeMap;
2023

2124

25+
public ProfileRequestContext getProfileRequestContext() {
26+
return profileRequestContext;
27+
}
28+
29+
public void setProfileRequestContext(ProfileRequestContext profileRequestContext) {
30+
this.profileRequestContext = profileRequestContext;
31+
}
32+
2233
public void setAttributeReleaseAction(GluuReleaseAttributesPostProcessor releaseAttributesPostProcessor) {
2334
this.releaseAttributesPostProcessor = releaseAttributesPostProcessor;
2435
}

shib-oxauth-authn/src/main/java/org/gluu/idp/externalauth/ShibOxAuthAuthServlet.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
import org.springframework.core.env.Environment;
5151
import org.springframework.web.context.WebApplicationContext;
5252
import org.springframework.web.context.support.WebApplicationContextUtils;
53+
import org.springframework.webflow.context.ExternalContextHolder;
54+
import org.springframework.webflow.context.servlet.ServletExternalContext;
55+
import org.springframework.webflow.engine.impl.FlowExecutionImpl;
56+
import org.springframework.webflow.execution.FlowExecutionFactory;
57+
import org.springframework.webflow.execution.FlowExecutionKey;
58+
import org.springframework.webflow.execution.repository.FlowExecutionRepository;
59+
import org.springframework.webflow.executor.FlowExecutionResult;
60+
import org.springframework.webflow.executor.FlowExecutorImpl;
5361

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

116124
@Override
117125
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
118-
try {
126+
if (!checkRequest(request, response)) {
127+
return;
128+
}
129+
130+
try {
131+
ExternalContextHolder.setExternalContext(new ServletExternalContext(request.getServletContext(), request, response));
119132

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

159172
// Get authentication key from request
160-
final String authenticationKey = ExternalAuthentication.startExternalAuthentication(externalRequest);
173+
final String flowExecutionKey = ExternalAuthentication.startExternalAuthentication(externalRequest);
161174

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

165178
// It's an authentication
166179
if (!authorizationResponse) {
167180
LOG.debug("Initiating oxAuth login redirect");
168-
startLoginRequest(request, response, authenticationKey, force);
181+
startLoginRequest(request, response, flowExecutionKey, force);
169182
return;
170183
}
171184

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

178191
// Re-init login page
179-
startLoginRequest(request, response, authenticationKey, force);
192+
startLoginRequest(request, response, flowExecutionKey, force);
180193
return;
181194
}
182195

183-
processAuthorizationResponse(request, response, authenticationKey);
196+
processAuthorizationResponse(request, response, flowExecutionKey);
184197

185198
} catch (final ExternalAuthenticationException ex) {
186-
LOG.warn("Error processing oxAuth authentication request", ex);
199+
LOG.error("Error processing oxAuth authentication request", ex);
187200
loadErrorPage(request, response);
188-
189201
} catch (final Exception ex) {
190202
LOG.error("Something unexpected happened", ex);
191203
request.setAttribute(ExternalAuthentication.AUTHENTICATION_ERROR_KEY, AuthnEventIds.AUTHN_EXCEPTION);
204+
} finally {
205+
ExternalContextHolder.setExternalContext(null);
192206
}
193207
}
194208

209+
private final boolean checkRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
210+
// Check whether a session is required.
211+
if (request.getSession(false) == null) {
212+
LOG.error("Pre-existing session required but none found");
213+
loadErrorPage(request, response);
214+
215+
return false;
216+
}
217+
218+
return true;
219+
}
220+
195221
private void processAuthorizationResponse(final HttpServletRequest request, final HttpServletResponse response, final String authenticationKey)
196222
throws ExternalAuthenticationException, IOException {
197223
try {
@@ -242,14 +268,14 @@ private void processAuthorizationResponse(final HttpServletRequest request, fina
242268
if(!idpAttributes.isEmpty()) {
243269
LOG.debug("Storing generated idp attributes");
244270
ProfileRequestContext prContext = ExternalAuthentication.getProfileRequestContext(authenticationKey, request);
245-
GluuScratchContext gluuScratchContext = prContext.getSubcontext(GluuScratchContext.class,true);
271+
GluuScratchContext gluuScratchContext = prContext.getSubcontext(GluuScratchContext.class, true);
246272
gluuScratchContext.setIdpAttributes(idpAttributes);
247273
}
248274

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

254280
if (authenticationContext != null) {
255281
String usedAcr = userProfile.getUsedAcr();

0 commit comments

Comments
 (0)