1
|
|
|
package org.apereo.cas.util; |
2
|
|
|
|
3
|
|
|
import org.apereo.inspektr.common.spi.PrincipalResolver; |
4
|
|
|
import org.pac4j.core.context.J2EContext; |
5
|
|
|
import org.pac4j.core.context.WebContext; |
6
|
|
|
import org.pac4j.core.profile.CommonProfile; |
7
|
|
|
import org.pac4j.core.profile.ProfileManager; |
8
|
|
|
import org.pac4j.core.profile.UserProfile; |
9
|
|
|
|
10
|
|
|
import javax.servlet.http.HttpServletRequest; |
11
|
|
|
import javax.servlet.http.HttpServletResponse; |
12
|
|
|
import java.util.Optional; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This is {@link Pac4jUtils}. |
16
|
|
|
* |
17
|
|
|
* @author Misagh Moayyed |
18
|
|
|
* @since 5.2.0 |
19
|
|
|
*/ |
20
|
|
|
public final class Pac4jUtils { |
21
|
|
|
private Pac4jUtils() {} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return the username of the authenticated user (based on pac4j security). |
25
|
|
|
* |
26
|
|
|
* @return the authenticated username. |
27
|
|
|
*/ |
28
|
|
|
public static String getPac4jAuthenticatedUsername() { |
29
|
|
|
final HttpServletRequest request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes(); |
30
|
|
|
final HttpServletResponse response = HttpRequestUtils.getHttpServletResponseFromRequestAttributes(); |
31
|
|
|
if (request != null && response != null) { |
32
|
|
|
final ProfileManager manager = getPac4jProfileManager(request, response); |
33
|
|
|
final Optional<UserProfile> profile = manager.get(true); |
34
|
|
|
if (profile != null && profile.isPresent()) { |
35
|
|
|
final String id = profile.get().getId(); |
36
|
|
|
if (id != null) { |
37
|
|
|
return id; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
return PrincipalResolver.UNKNOWN_USER; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets pac 4 j profile manager. |
46
|
|
|
* |
47
|
|
|
* @param request the request |
48
|
|
|
* @param response the response |
49
|
|
|
* @return the pac 4 j profile manager |
50
|
|
|
*/ |
51
|
|
|
public static ProfileManager getPac4jProfileManager(final HttpServletRequest request, final HttpServletResponse response) { |
52
|
|
|
final J2EContext context = getPac4jJ2EContext(request, response); |
53
|
|
|
return getPac4jProfileManager(context); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets pac4j profile manager. |
58
|
|
|
* |
59
|
|
|
* @param context the context |
60
|
|
|
* @return the pac4j profile manager |
61
|
|
|
*/ |
62
|
|
|
public static ProfileManager getPac4jProfileManager(final WebContext context) { |
63
|
|
|
return new ProfileManager(context); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets pac4j context. |
68
|
|
|
* |
69
|
|
|
* @param request the request |
70
|
|
|
* @param response the response |
71
|
|
|
* @return the context |
72
|
|
|
*/ |
73
|
|
|
public static J2EContext getPac4jJ2EContext(final HttpServletRequest request, final HttpServletResponse response) { |
74
|
|
|
return new J2EContext(request, response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets pac4j context. |
79
|
|
|
* |
80
|
|
|
* @return the pac4j context |
81
|
|
|
*/ |
82
|
|
|
public static J2EContext getPac4jJ2EContext() { |
83
|
|
|
return getPac4jJ2EContext(HttpRequestUtils.getHttpServletRequestFromRequestAttributes(), |
84
|
|
|
HttpRequestUtils.getHttpServletResponseFromRequestAttributes()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Finds the current client name from the context, using the PAC4J Profile Manager. It is assumed that the context has previously been |
89
|
|
|
* populated with the profile. |
90
|
|
|
* |
91
|
|
|
* @param webContext |
92
|
|
|
* A web context (request + response). |
93
|
|
|
* |
94
|
|
|
* @return The currently used client's name or {@code null} if there is no active profile. |
95
|
|
|
*/ |
96
|
|
|
public static String findCurrentClientName(final WebContext webContext) { |
97
|
|
|
@SuppressWarnings("unchecked") |
98
|
|
|
final ProfileManager<? extends CommonProfile> pm = getPac4jProfileManager(webContext); |
99
|
|
|
final Optional<? extends CommonProfile> profile = pm.get(true); |
100
|
|
|
return profile.map(CommonProfile::getClientName).orElse(null); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|