1 package org
.argeo
.cms
.auth
;
3 import java
.security
.AccessController
;
4 import java
.security
.Principal
;
5 import java
.security
.PrivilegedAction
;
6 import java
.security
.PrivilegedActionException
;
7 import java
.security
.PrivilegedExceptionAction
;
8 import java
.util
.HashSet
;
9 import java
.util
.Locale
;
11 import java
.util
.UUID
;
12 import java
.util
.concurrent
.Callable
;
13 import java
.util
.concurrent
.CompletionException
;
15 import javax
.security
.auth
.Subject
;
16 import javax
.security
.auth
.x500
.X500Principal
;
18 import org
.argeo
.api
.cms
.CmsConstants
;
19 import org
.argeo
.api
.cms
.CmsSession
;
20 import org
.argeo
.api
.cms
.CmsSessionId
;
21 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
22 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
23 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
24 import org
.osgi
.service
.useradmin
.Authorization
;
27 * Programmatic access to the currently authenticated user, within a CMS
30 public final class CurrentUser
{
36 * Technical username of the currently authenticated user.
38 * @return the authenticated username or null if not authenticated / anonymous
40 public static String
getUsername() {
41 return getUsername(currentSubject());
45 * Human readable name of the currently authenticated user (typically first name
48 public static String
getDisplayName() {
49 return getDisplayName(currentSubject());
52 /** Whether a user is currently authenticated. */
53 public static boolean isAnonymous() {
54 return isAnonymous(currentSubject());
57 /** Locale of the current user */
58 public final static Locale
locale() {
59 return locale(currentSubject());
62 /** Roles of the currently logged-in user */
63 public final static Set
<String
> roles() {
64 return roles(currentSubject());
67 /** Returns true if the current user is in the specified role */
68 public static boolean isInRole(String role
) {
69 Set
<String
> roles
= roles();
70 return roles
.contains(role
);
73 /** Executes as the current user */
74 public final static <T
> T
doAs(PrivilegedAction
<T
> action
) {
75 return Subject
.doAs(currentSubject(), action
);
78 /** Executes as the current user */
79 public final static <T
> T
tryAs(PrivilegedExceptionAction
<T
> action
) throws PrivilegedActionException
{
80 return Subject
.doAs(currentSubject(), action
);
87 public final static String
getUsername(Subject subject
) {
89 throw new IllegalArgumentException("Subject cannot be null");
90 if (subject
.getPrincipals(X500Principal
.class).size() != 1)
91 return CmsConstants
.ROLE_ANONYMOUS
;
92 Principal principal
= subject
.getPrincipals(X500Principal
.class).iterator().next();
93 return principal
.getName();
96 public final static String
getDisplayName(Subject subject
) {
97 return getAuthorization(subject
).toString();
100 public final static Set
<String
> roles(Subject subject
) {
101 Set
<String
> roles
= new HashSet
<String
>();
102 roles
.add(getUsername(subject
));
103 for (Principal group
: subject
.getPrincipals(ImpliedByPrincipal
.class)) {
104 roles
.add(group
.getName());
109 public final static Locale
locale(Subject subject
) {
110 Set
<Locale
> locales
= subject
.getPublicCredentials(Locale
.class);
111 if (locales
.isEmpty()) {
112 Locale defaultLocale
= CmsContextImpl
.getCmsContext().getDefaultLocale();
113 return defaultLocale
;
115 return locales
.iterator().next();
118 /** Whether this user is currently authenticated. */
119 public static boolean isAnonymous(Subject subject
) {
122 String username
= getUsername(subject
);
123 return username
== null || username
.equalsIgnoreCase(CmsConstants
.ROLE_ANONYMOUS
);
126 public static CmsSession
getCmsSession() {
127 Subject subject
= currentSubject();
128 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
129 return CmsContextImpl
.getCmsContext().getCmsSessionByUuid(cmsSessionId
.getUuid());
135 private static Subject
currentSubject() {
136 Subject subject
= getAccessControllerSubject();
139 throw new IllegalStateException("Cannot find related subject");
142 private static Subject
getAccessControllerSubject() {
143 return Subject
.getSubject(AccessController
.getContext());
146 private static Authorization
getAuthorization(Subject subject
) {
147 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
150 public static boolean logoutCmsSession(Subject subject
) {
152 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
153 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
156 CmsSessionImpl cmsSession
= CmsContextImpl
.getCmsContext().getCmsSessionByUuid(nodeSessionId
);
158 // FIXME logout all views
159 // TODO check why it is sometimes null
160 if (cmsSession
!= null)
162 // if (log.isDebugEnabled())
163 // log.debug("Logged out CMS session " + cmsSession.getUuid());
168 * PREPARE EVOLUTION OF JAVA APIs INTRODUCED IN JDK 18
169 * The following static methods will be added to Subject
171 public Subject
current() {
172 return currentSubject();
175 public static <T
> T
callAs(Subject subject
, Callable
<T
> action
) {
177 return Subject
.doAs(subject
, new PrivilegedExceptionAction
<T
>() {
180 public T
run() throws Exception
{
181 return action
.call();
185 } catch (PrivilegedActionException e
) {
186 throw new CompletionException("Failed to execute action for " + subject
, e
.getCause());
190 private CurrentUser() {