Manage sessions using REST
To manage sessions using REST, send requests to the /json/sessions
endpoint, as shown in the following examples.
Get information about sessions
To get information about a session, send an HTTP POST request to the /json/sessions/
endpoint,
using the getSessionInfo
action.
This action returns information about the session token provided in the
iPlanetDirectoryPro
header by default. To get information about a different session token, include it in the POST body
as the value of the tokenId
parameter.
For information on retrieving custom session properties:
|
The following example shows an administrative user passing their session token in the iPlanetDirectoryPro
header,
and the session token of the demo
user as the value of the tokenId
:
$ curl \
--request POST \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=4.0" \
--header "Content-type: application/json" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=getSessionInfo'
{
"username": "demo",
"universalId": "id=demo,ou=user,dc=openam,dc=forgerock,dc=org",
"realm": "/",
"latestAccessTime": "2020-02-21T14:31:18Z",
"maxIdleExpirationTime": "2020-02-21T15:01:18Z",
"maxSessionExpirationTime": "2020-02-21T16:29:56Z",
"properties": {
"AMCtxId": "aba7b4f3-16ff-4680-b06a-d7ba237d3730-91932"
}
}
The getSessionInfo
action does not refresh the session idle timeout.
To obtain session information about a server-side session and reset the idle timeout,
use the getSessionInfoAndResetIdleTime
action, as follows:
$ curl \
--request POST \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=4.0, protocol=1.0" \
--header "Content-type: application/json" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=getSessionInfoAndResetIdleTime'
{
"username": "demo",
"universalId": "id=demo,ou=user,dc=openam,dc=forgerock,dc=org",
"realm": "/",
"latestAccessTime": "2020-02-21T14:32:49Z",
"maxIdleExpirationTime": "2020-02-21T15:02:49Z",
"maxSessionExpirationTime": "2020-02-21T16:29:56Z",
"properties": {
"AMCtxId": "aba7b4f3-16ff-4680-b06a-d7ba237d3730-91932"
}
}
|
Validate sessions
To check if a session token is valid, send an HTTP POST request to the /json/sessions/
endpoint with the validate
action. Provide the session token in the POST data as the value of the tokenId
parameter.
Provide the session token of an administrative user in the iPlanetDirectoryPro
header.
If you don’t specify a tokenId , this request validates the session in the iPlanetDirectoryPro header.
|
The following example shows an administrative user, such as amAdmin
,
validating a session token for the demo
user:
$ curl \
--request POST \
--header "Content-type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.0, protocol=1.0" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions?_action=validate'
If the session token is valid, the user ID and its realm is returned:
{
"valid": true,
"sessionUid": "c888fe06-dcb4-43e9-b74a-db838c163aa6-164702",
"uid": "bjensen",
"realm": "/alpha"
}
By default, validating a session resets the session’s idle time,
which triggers a write operation to the Core Token Service token store.
To avoid this, include refresh=false
, for example, validate&refresh=false
.
Refresh server-side sessions
To reset the idle time of a server-side session, send an HTTP POST request to the /json/sessions/
endpoint, with the
refresh
action. This action refreshes the session token provided in the iPlanetDirectoryPro
header by default.
To refresh a different session token, include it in the POST body as the value of the tokenId
query parameter.
The following example shows an administrative user passing their session token in the iPlanetDirectoryPro
header,
and bjensen’s session token as the tokenId
parameter:
$ curl \
--request POST \
--header 'Content-Type: application/json' \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=refresh'
{
"uid": "bjensen",
"realm": "/alpha",
"idletime": 0,
"maxidletime": 30,
"maxsessiontime": 120,
"maxtime": 6171
}
On success, AM resets the idle time for the server-side session, and returns timeout details of the session.
|
Invalidate sessions
To invalidate a session, send an HTTP POST request to the /json/sessions/
endpoint with the logout
action.
This action invalidates the session token provided in the iPlanetDirectoryPro
header by default:
$ curl \
--request POST \
--header "Content-type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
'https://openam.example.com:8443/openam/json/realms/root/realms/alpha/sessions/?_action=logout'
{
"result": "Successfully logged out"
}
On success, AM invalidates the session and returns a success message.
If the token is not valid and cannot be invalidated an error message is returned:
{
"result": "Token has expired"
}
An administrative user can invalidate the session of another authenticated user by providing the session token as the
value of the tokenId
query parameter.
This example shows an administrative user passing their session token in the iPlanetDirectoryPro
header and
invalidating bjensen’s session by passing her session token in the tokenId
parameter:
$ curl \
--request POST \
--header "Content-type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=logout'
{
"result": "Successfully logged out"
}
Invalidate specific sessions
To invalidate specific sessions for a user, first obtain a list of the user’s active sessions.
Send an HTTP GET request to the /json/sessions/
endpoint, using the SSO token of
an administrative user, such as amAdmin
as the value of the iPlanetDirectoryPro
header.
Use a queryFilter
to specify the name of the user and the realm to search.
For example, to obtain the list of active sessions for bjensen
in the alpha
realm, the query filter value would be:
username eq "bjensen" and realm eq "/alpha"
The query filter value must be URL-encoded when sent over HTTP. For more information about query filter parameters, refer to Query. |
In the following example, there are two active sessions. Note the value of the sessionHandle
properties.
$ curl \
--request GET \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
'https://openam.example.com:8443/openam/json/realms/root/realms/alpha/sessions?_queryFilter=username%20eq%20%22bjensen%22%20and%20realm%20eq%20%22%2Falpha%22'
{
"result": [
{
"username": "bjensen",
"universalId": "id=bjensen,ou=user,o=alpha,ou=services,dc=openam,dc=forgerock,dc=org",
"realm": "/alpha",
"sessionHandle": "shandle:ITnOR5S…AA.",
"latestAccessTime": "2022-11-11T09:32:28.265Z",
"maxIdleExpirationTime": "2022-11-11T10:02:28Z",
"maxSessionExpirationTime": "2022-11-11T11:32:28Z"
},
{
"username": "bjensen",
"universalId": "id=bjensen,ou=user,o=alpha,ou=services,dc=openam,dc=forgerock,dc=org",
"realm": "/alpha",
"sessionHandle": "shandle:JfMui6O…AA.",
"latestAccessTime": "2022-11-11T09:32:24.395Z",
"maxIdleExpirationTime": "2022-11-11T10:02:24Z",
"maxSessionExpirationTime": "2022-11-11T11:32:24Z"
}
],
"resultCount": 2,
"pagedResultsCookie": null,
"totalPagedResultsPolicy": "NONE",
"totalPagedResults": -1,
"remainingPagedResults": -1
}
To log out specific sessions, send an HTTP POST request to the /json/sessions/
endpoint, with the logoutByHandle
action.
Include an array of the session handles to invalidate as values of the sessionHandles
property in the POST body.
Use the SSO token of an administrative user, such as amAdmin
, as the value of the iPlanetDirectoryPro
header.
This example invalidates the sessions returned by the previous query:
$ curl \
--request POST \
--header "Content-Type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{
"sessionHandles": [
"shandle:ITnOR5S…AA.",
"shandle:JfMui6O…AA."
]
}' \
'https://openam.example.com:8443/openam/json/realms/root/realms/alpha/sessions/?_action=logoutByHandle'
{
"result": {
"shandle:ITnOR5S…AAA.": true,
"shandle:JfMui6O…AA.": true
}
}
Invalidate all sessions for a user
To invalidate (log out) all sessions for a user, send an HTTP POST request to the /json/sessions/
endpoint with the
logoutByUser
action, specifying the username in the request payload.
Provide the session token of an administrative user in the iPlanetDirectoryPro
header. Users can invalidate their
own sessions by providing their session token in the iPlanetDirectoryPro
header and their username in the body of the
request.
This example logs out all bjensen’s sessions:
$ curl \
--request POST \
--header "Content-Type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=5.1, protocol=1.0" \
--data '{"username": "bjensen"}' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=logoutByUser'
{
"result": true
}
Session invalidation by user works slightly differently for server-side and client-side sessions.
- Server-side sessions
-
Logout by user simply deletes all sessions for that user. This action creates an
AM-SESSION-LOGGED-OUT
event in the activity log and anAM-LOGOUT
event in the authentication log, as with any other session termination. - Client-side sessions
-
When the user logs out, a logout token is created. If there is only one AM instance in the site, the logout token is added to a local cache of that AM instance. If there are multiple AM instances in the site, the other instances poll the CTS a specified interval to update their logout token cache. For more information, refer to the Enable Invalidation of Sessions Based on User Identifier property.
An
AM-LOGOUT-USER-TOKEN
event is created in the activity log when the user is logged out. The action isCREATE
orUPDATE
depending on whether a token for the user being logged out already exists. TheuserId
component of this audit entry is that of the caller, not of the target. For example, if an administrative user logs out another user, theuserId
is that of the administrative user, not that of the user being logged out. TheobjectId
indicates the target of the operation.A
DELETE
event is audited when the logout token expires and is deleted from the CTS.
|
Get and set session properties
Use the REST API to read and update properties on users' sessions. Define the properties you want to set in the session property allowlist service configuration. For information on allowlisting session properties, refer to Session Property Whitelist service.
You can use the REST API to:
-
Get the names of the properties that you can read or update. This is the same set of properties configured in the Session Property Whitelist service.
-
Read and update property values.
The ability to set, change and delete session properties is affected by the session state:
|
Differentiate the user who performs the operation from the session affected by the operation as follows:
-
Specify the session token of the user performing the operation on session properties in the
iPlanetDirectoryPro
header. -
Specify the session token of the user whose session is to be read or modified as the
tokenId
parameter in the body of the REST API call. -
Omit the
tokenId
parameter from the body of the REST API call if the session of the user performing the operation is the same session that you want to read or modify.
The following examples assume that you configured a property named LoginLocation
in the Session Property Whitelist service.
To retrieve the names and values of the properties you can get or set, send an HTTP POST request to the json/sessions
endpoint, with the getSessionProperties
action:
$ curl \
--request POST \
--header "Content-type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{ "tokenId": "BXCCq…NX*1*" }' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=getSessionProperties'
{
"LoginLocation": ""
}
To set the value of a session property, send an HTTP POST request to the /json/sessions/
endpoint, with the updateSessionProperties
action. If you do not specify a tokenId
parameter in the request body, the operation
affects the session specified in the iPlanetDirectoryPro
header:
$ curl \
--request POST \
--header "Content-Type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{"LoginLocation":"40.748440, -73.984559"}' \
https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=updateSessionProperties
{
"LoginLocation": "40.748440, -73.984559"
}
To set multiple properties in a single REST API call, specify the list of properties and their values in the JSON payload; for example:
--data '{"property1":"value1", "property2":"value2"}'
To set the value of a session property on another user’s session, specify the session token of the user performing the action in the iPlanetDirectoryPro
header and the session token to be modified as the value of the tokenId
:
$ curl \
--request POST \
--header "Content-Type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{"LoginLocation": "40.748440, -73.984559", "tokenId": "BXCCq…NX*1*"}' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=updateSessionProperties'
{
"LoginLocation": "40.748440, -73.984559"
}
If the user attempting to modify the session does not have sufficient access privileges, the preceding examples result in a 403 Forbidden error.
You cannot set properties internal to AM sessions. If you try to modify an internal property in a REST API call, AM also returns a 403 Forbidden error; for example:
$ curl \
--request POST \
--header "Content-Type: application/json" \
--header "iPlanetDirectoryPro: AQICS…NzEz*" \
--header "Accept-API-Version: resource=3.1, protocol=1.0" \
--data '{"AuthLevel":"5", "tokenId": "BXCCq…NX*1*"}' \
'https://openam.example.com:8443/openam/json/realms/root/sessions/?_action=updateSessionProperties'
{
"code": 403,
"reason": "Forbidden",
"message": "Forbidden"
}
For a list of the default session properties, refer to Session properties.