|
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.ProfileManager; |
|
7
|
|
|
import org.pac4j.core.profile.UserProfile; |
|
8
|
|
|
|
|
9
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
10
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
11
|
|
|
import java.util.Optional; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This is {@link Pac4jUtils}. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Misagh Moayyed |
|
17
|
|
|
* @since 5.2.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
public final class Pac4jUtils { |
|
20
|
|
|
private Pac4jUtils() {} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Return the username of the authenticated user (based on pac4j security). |
|
24
|
|
|
* |
|
25
|
|
|
* @return the authenticated username. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static String getPac4jAuthenticatedUsername() { |
|
28
|
|
|
final HttpServletRequest request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes(); |
|
29
|
|
|
final HttpServletResponse response = HttpRequestUtils.getHttpServletResponseFromRequestAttributes(); |
|
30
|
|
|
if (request != null && response != null) { |
|
31
|
|
|
final ProfileManager manager = getPac4jProfileManager(request, response); |
|
32
|
|
|
final Optional<UserProfile> profile = manager.get(true); |
|
33
|
|
|
if (profile != null && profile.isPresent()) { |
|
34
|
|
|
final String id = profile.get().getId(); |
|
35
|
|
|
if (id != null) { |
|
36
|
|
|
return id; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
return PrincipalResolver.UNKNOWN_USER; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets pac 4 j profile manager. |
|
45
|
|
|
* |
|
46
|
|
|
* @param request the request |
|
47
|
|
|
* @param response the response |
|
48
|
|
|
* @return the pac 4 j profile manager |
|
49
|
|
|
*/ |
|
50
|
|
|
public static ProfileManager getPac4jProfileManager(final HttpServletRequest request, final HttpServletResponse response) { |
|
51
|
|
|
final J2EContext context = getPac4jJ2EContext(request, response); |
|
52
|
|
|
return getPac4jProfileManager(context); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Gets pac4j profile manager. |
|
57
|
|
|
* |
|
58
|
|
|
* @param context the context |
|
59
|
|
|
* @return the pac4j profile manager |
|
60
|
|
|
*/ |
|
61
|
|
|
public static ProfileManager getPac4jProfileManager(final WebContext context) { |
|
62
|
|
|
return new ProfileManager(context); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Gets pac4j context. |
|
67
|
|
|
* |
|
68
|
|
|
* @param request the request |
|
69
|
|
|
* @param response the response |
|
70
|
|
|
* @return the context |
|
71
|
|
|
*/ |
|
72
|
|
|
public static J2EContext getPac4jJ2EContext(final HttpServletRequest request, final HttpServletResponse response) { |
|
73
|
|
|
return new J2EContext(request, response); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Gets pac4j context. |
|
78
|
|
|
* |
|
79
|
|
|
* @return the pac4j context |
|
80
|
|
|
*/ |
|
81
|
|
|
public static J2EContext getPac4jJ2EContext() { |
|
82
|
|
|
return getPac4jJ2EContext(HttpRequestUtils.getHttpServletRequestFromRequestAttributes(), HttpRequestUtils.getHttpServletResponseFromRequestAttributes()); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|