CRecordSessionValidate
This record is needed when a session token we received from a user request is to be checked for validity. With the response we also receive the user ID and a list with the rights of the user.
This message belongs to the session manager API. It is part of the kernel.
{
"id": "deb4b364-0890-4380-8c36-a142fcb29034",
"name": "SESSION_VALIDATE",
"description": "Validate a token.",
"slots": [
{
"key": "userId",
"name": "USER_ID",
"direction": "RESPONSE",
"presenceConstraint": "OPTIONAL",
"type": "STRING",
"description": "The user ID (only on SUCCESS)."
},
{
"key": "rights",
"name": "RIGHTS",
"direction": "RESPONSE",
"presenceConstraint": "OPTIONAL",
"type": "STRING_ARRAY",
"description": "The rights the user has (only on SUCCESS)."
}
]
}
Usage
Sending the request
You need the microservice ID of the session manager:
public static final IId SESSION_MICROSERVICE_ID = CIdFactory.fromObject("ccf168c1-f18b-4229-85f9-24461a19ee6a");
To validate a session token, we need the token the user received when he logged in.
private void validateToken(final byte[] aSessionToken) throws CException
{
final CEnvelope env = CEnvelope.forMicroService(SESSION_MICROSERVICE_ID);
env.setSessionToken(aSessionToken);
final CRecord record = CRecordSessionValidate.create();
aTarget.sendRequest(env,
record);
}
Dealing with the response
To catch the response of the request, we need a message handler. We add it in the constructor of the message handler registry.
// constructor:
addMessageHandler(CRecordSessionValidate.ID,
this::asyncSessionValidate);
private boolean asyncSessionValidate(@NotNull final CEnvelope aEnvelope,
@NotNull final CRecord aRecord)
{
if (aEnvelope.isAnswer())
{
final int resultCode = aEnvelope.getResultCode();
if (resultCode == CResultCode.SUCCESS)
{
final byte[] token = aEnvelope.getSessionToken();
final String userId = CRecordSessionValidate.getUserId(aRecord,
null);
final String[] rights = CRecordSessionValidate.getRights(aRecord,
null);
// ...
}
return true;
}
return false;
}