public final class WikiSession extends Object implements WikiEventListener
Represents a long-running wiki session, with an associated user Principal,
user Subject, and authentication status. This class is initialized with
minimal, default-deny values: authentication is set to false
,
and the user principal is set to null
.
The WikiSession class allows callers to:
isAnonymous()
and isAuthenticated()
getLoginPrincipal()
,
getUserPrincipal()
and getPrincipals()
addMessage(String)
, getMessages(String)
and clearMessages(String)
To keep track of the Principals each user posseses, each WikiSession stores a JAAS Subject. Various login processes add or remove Principals when users authenticate or log out.
WikiSession implements the WikiEventListener
interface and listens for group add/change/delete events fired by
event sources the WikiSession is registered with. Normally,
AuthenticationManager
registers each WikiSession
with the GroupManager
so it can catch group events. Thus, when a user is added to a
Group
, a corresponding
GroupPrincipal
is injected into
the Subject's Principal set. Likewise, when the user is removed from
the Group or the Group is deleted, the GroupPrincipal is removed
from the Subject. The effect that this strategy produces is extremely
beneficial: when someone adds a user to a wiki group, that user
immediately gains the privileges associated with that
group; he or she does not need to re-authenticate.
In addition to methods for examining individual WikiSession
objects, this class also contains a number of static methods for
managing WikiSessions for an entire wiki. These methods allow callers
to find, query and remove WikiSession objects, and
to obtain a list of the current wiki session users.
WikiSession encloses a protected static class, SessionMonitor
,
to keep track of WikiSessions registered with each wiki.
Modifier and Type | Field and Description |
---|---|
static String |
ANONYMOUS
An anonymous user's session status.
|
static String |
ASSERTED
An asserted user's session status.
|
static String |
AUTHENTICATED
An authenticated user's session status.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(WikiEvent event)
Listens for WikiEvents generated by source objects such as the
GroupManager.
|
void |
addMessage(String message)
Adds a message to the generic list of messages associated with the
session.
|
void |
addMessage(String topic,
String message)
Adds a message to the specific set of messages associated with the
session.
|
void |
clearMessages()
Clears all messages associated with this session.
|
void |
clearMessages(String topic)
Clears all messages associated with a session topic.
|
static Object |
doPrivileged(WikiSession session,
PrivilegedAction<?> action)
Wrapper for
Subject.doAsPrivileged(Subject, java.security.PrivilegedExceptionAction, java.security.AccessControlContext)
that executes an action with the privileges posssessed by a
WikiSession's Subject. |
Locale |
getLocale()
Returns a cached Locale object for this user.
|
Principal |
getLoginPrincipal()
Returns the Principal used to log in to an authenticated session.
|
String[] |
getMessages()
Returns all generic messages associated with this session.
|
String[] |
getMessages(String topic)
Returns all messages associated with a session topic.
|
Principal[] |
getPrincipals()
Returns all user Principals associated with this session.
|
Principal[] |
getRoles()
Returns an array of Principal objects that represents the groups and
roles that the user associated with a WikiSession possesses.
|
String |
getStatus()
Returns the status of the wiki session as a text string.
|
Principal |
getUserPrincipal()
Returns the primary user Principal associated with this session.
|
static WikiSession |
getWikiSession(WikiEngine engine,
javax.servlet.http.HttpServletRequest request)
Static factory method that returns the WikiSession object associated with
the current HTTP request.
|
static WikiSession |
guestSession(WikiEngine engine)
Static factory method that creates a new "guest" session containing a single
user Principal
WikiPrincipal.GUEST ,
plus the role principals Role.ALL and
Role.ANONYMOUS . |
boolean |
hasPrincipal(Principal principal)
Returns
true if the WikiSession's Subject
possess a supplied Principal. |
protected void |
injectGroupPrincipals()
Injects GroupPrincipal objects into the user's Principal set based on the
groups the user belongs to.
|
protected void |
injectUserProfilePrincipals()
Adds Principal objects to the Subject that correspond to the
logged-in user's profile attributes for the wiki name, full name
and login name.
|
void |
invalidate()
Invalidates the WikiSession and resets its Subject's
Principals to the equivalent of a "guest session".
|
boolean |
isAnonymous()
Determines whether the current session is anonymous.
|
boolean |
isAsserted()
Returns
true if the user is considered asserted via
a session cookie; that is, the Subject contains the Principal
Role.ASSERTED. |
boolean |
isAuthenticated()
Returns the authentication status of the user's session.
|
protected boolean |
isInGroup(Group group)
Returns
true if one of this WikiSession's user Principals
can be shown to belong to a particular wiki group. |
protected static boolean |
isIPV4Address(String name)
Verifies whether a String represents an IPv4 address.
|
static void |
removeWikiSession(WikiEngine engine,
javax.servlet.http.HttpServletRequest request)
Removes the wiki session associated with the user's HTTP request
from the cache of wiki sessions, typically as part of a logout
process.
|
static int |
sessions(WikiEngine engine)
Returns the total number of active wiki sessions for a
particular wiki.
|
static Principal[] |
userPrincipals(WikiEngine engine)
Returns Principals representing the current users known
to a particular wiki.
|
public static final String ANONYMOUS
public static final String ASSERTED
public static final String AUTHENTICATED
protected boolean isInGroup(Group group)
true
if one of this WikiSession's user Principals
can be shown to belong to a particular wiki group. If the user is
not authenticated, this method will always return false
.group
- the group to testpublic boolean isAsserted()
true
if the user is considered asserted via
a session cookie; that is, the Subject contains the Principal
Role.ASSERTED.true
if the user is assertedpublic boolean isAuthenticated()
true
if the user is authenticatedpublic boolean isAnonymous()
Determines whether the current session is anonymous. This will be true if any of these conditions are true:
Role.ANONYMOUS
WikiPrincipal.GUEST
getUserPrincipal()
evaluates
to an IP address.The criteria above are listed in the order in which they are evaluated.
public Principal getLoginPrincipal()
Returns the Principal used to log in to an authenticated session. The
login principal is determined by examining the Subject's Principal set
for PrincipalWrappers or WikiPrincipals with type designator
LOGIN_NAME
; the first one found is the login principal.
If one is not found, this method returns the first principal that isn't
of type Role or GroupPrincipal. If neither of these conditions hold, this method returns
WikiPrincipal.GUEST
.
public Principal getUserPrincipal()
Returns the primary user Principal associated with this session. The primary user principal is determined as follows:
WIKI_NAME
or (alternatively)
FULL_NAME
is the primary Principal.WikiPrincipal.GUEST
.public Locale getLocale()
public void addMessage(String message)
clearMessages()
method is called.message
- the message to add; if null
it is ignored.public void addMessage(String topic, String message)
clearMessages()
method is called.topic
- the topic to associate the message to;message
- the message to addpublic void clearMessages()
public void clearMessages(String topic)
topic
- the topic whose messages should be cleared.public String[] getMessages()
clearMessages()
.public String[] getMessages(String topic)
clearMessages(String)
.topic
- The topicpublic Principal[] getPrincipals()
AuthenticationManager.isUserPrincipal(Principal)
public Principal[] getRoles()
getName
method. Note that this method does not
consult the external Authorizer or GroupManager; it relies on the
Principals that have been injected into the user's Subject at login time,
or after group creation/modification/deletion.public static void removeWikiSession(WikiEngine engine, javax.servlet.http.HttpServletRequest request)
engine
- the wiki enginerequest
- the users's HTTP requestpublic boolean hasPrincipal(Principal principal)
true
if the WikiSession's Subject
possess a supplied Principal. This method eliminates the need
to externally request and inspect the JAAS subject.principal
- the Principal to testpublic void actionPerformed(WikiEvent event)
actionPerformed
in interface WikiEventListener
event
- a WikiEvent objectWikiEventListener.actionPerformed(org.apache.wiki.event.WikiEvent)
public void invalidate()
protected void injectGroupPrincipals()
GroupManager.getRoles()
to obtain the array of GroupPrincipals
the authorizer knows about. Then, the method
GroupManager.isUserInRole(WikiSession, Principal)
is called for
each Principal. If the user is a member of the group, an equivalent
GroupPrincipal is injected into the user's principal set. Existing
GroupPrincipals are flushed and replaced. This method should generally be
called after a user's UserProfile
is
saved. If the wiki session is null, or there is no matching user profile,
the method returns silently.protected void injectUserProfilePrincipals()
public String getStatus()
Returns the status of the wiki session as a text string. Valid values are:
public static WikiSession getWikiSession(WikiEngine engine, javax.servlet.http.HttpServletRequest request)
Static factory method that returns the WikiSession object associated with
the current HTTP request. This method looks up the associated HttpSession
in an internal WeakHashMap and attempts to retrieve the WikiSession. If
not found, one is created. This method is guaranteed to always return a
WikiSession, although the authentication status is unpredictable until
the user attempts to log in. If the servlet request parameter is
null
, a synthetic guestSession(WikiEngine)
is returned.
When a session is created, this method attaches a WikiEventListener to the GroupManager so that changes to groups are detected automatically.
engine
- the wiki enginerequest
- the servlet request objectpublic static WikiSession guestSession(WikiEngine engine)
WikiPrincipal.GUEST
,
plus the role principals Role.ALL
and
Role.ANONYMOUS
. This method also adds the session as a listener
for GroupManager, AuthenticationManager and UserManager events.engine
- the wiki enginepublic static int sessions(WikiEngine engine)
SessionMonitor.sessions()
method.engine
- the wiki sessionpublic static Principal[] userPrincipals(WikiEngine engine)
getUserPrincipal()
method. This method delegates to SessionMonitor.userPrincipals()
.engine
- the wiki enginepublic static Object doPrivileged(WikiSession session, PrivilegedAction<?> action) throws AccessControlException
Subject.doAsPrivileged(Subject, java.security.PrivilegedExceptionAction, java.security.AccessControlContext)
that executes an action with the privileges posssessed by a
WikiSession's Subject. The action executes with a null
AccessControlContext, which has the effect of running it "cleanly"
without the AccessControlContexts of the caller.session
- the wiki sessionaction
- the privileged actionnull
AccessControlException
- if the action is not permitted
by the security policyprotected static boolean isIPV4Address(String name)
name
- the address to testCopyright © 2001-2019 The Apache Software Foundation. All rights reserved.