Test Failed
Pull Request — master (#3063)
by
unknown
16:18
created

org.apereo.cas.support.pac4j.web.flow.SingleLogoutPreparationAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 42
c 1
b 0
f 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
B doExecute(RequestContext) 0 26 4
A SingleLogoutPreparationAction(CookieRetrievingCookieGenerator,ProfileService) 0 5 1
1
package org.apereo.cas.support.pac4j.web.flow;
2
3
import javax.servlet.http.HttpServletRequest;
4
import javax.servlet.http.HttpServletResponse;
5
6
import org.apereo.cas.util.Pac4jUtils;
7
import org.apereo.cas.web.support.CookieRetrievingCookieGenerator;
8
import org.apereo.cas.web.support.WebUtils;
9
import org.pac4j.core.context.J2EContext;
10
import org.pac4j.core.context.WebContext;
11
import org.pac4j.core.profile.CommonProfile;
12
import org.pac4j.core.profile.ProfileManager;
13
import org.pac4j.core.profile.service.ProfileService;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import org.springframework.webflow.action.AbstractAction;
17
import org.springframework.webflow.execution.Event;
18
import org.springframework.webflow.execution.RequestContext;
19
20
21
/**
22
 * The purpose of this action is to prepare the PAC4J Profile Manager for Single Logout.
23
 * 
24
 * The Profile Manager keeps the profiles in request + session but the session has already been destroyed. This action should restore the
25
 * profile from a long term storage - {@link ProfileService} and populate the PAC4J Profile Manager with it.
26
 * 
27
 * This action should be called from the Logout web flow.
28
 * 
29
 * @author jkacer
30
 * 
31
 * @since 5.2.0
32
 */
33
public class SingleLogoutPreparationAction extends AbstractAction {
34
35
    private final Logger logger2 = LoggerFactory.getLogger(SingleLogoutPreparationAction.class);
36
37
    private final ProfileService<? extends CommonProfile> profileService;
38
    private final CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator;
39
40
41
    public SingleLogoutPreparationAction(final CookieRetrievingCookieGenerator ticketGrantingTicketCookieGenerator,
42
            final ProfileService<? extends CommonProfile> profileService) {
43
        super();
44
        this.profileService = profileService;
45
        this.ticketGrantingTicketCookieGenerator = ticketGrantingTicketCookieGenerator;
46
    }
47
48
49
    @SuppressWarnings({ "rawtypes", "unchecked" })
50
    @Override
51
    protected Event doExecute(final RequestContext rc) throws Exception {
52
        // Get the TGT first. For logout, we need to get the cookie's value, most likely the TGT will not be in the scope anymore.
53
        String tgtId = WebUtils.getTicketGrantingTicketId(rc);
54
        final HttpServletRequest request = WebUtils.getHttpServletRequestFromExternalWebflowContext(rc);
55
        if (tgtId == null) {
56
            tgtId = ticketGrantingTicketCookieGenerator.retrieveCookieValue(request);
57
        }
58
59
        // Retrieve the user profile previously stored to the long-term storage in PAC4J ClientAction.
60
        final CommonProfile profile = (tgtId == null) ? null : profileService.findById(tgtId);
61
62
        // And save the profile into the PAC4J Profile Manager for this request + session.
63
        if (profile != null) {
64
            final HttpServletResponse response = WebUtils.getHttpServletResponseFromExternalWebflowContext(rc);
65
            final WebContext webContext = new J2EContext(request, response);
66
            final ProfileManager pm = Pac4jUtils.getPac4jProfileManager(webContext);
67
            pm.save(true, profile, false);
68
            logger2.debug("User profile restored from a long-term storage and saved in PAC4J Profile Manager.");
69
        } else {
70
            logger2.debug("No user profile restored from a long-term storage. SAML Single Logout may not work properly."
71
                    + " This is normal for non-SAML clients.");
72
        }
73
74
        return success();
75
    }
76
77
78
}
79