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.
{ "id": "deb4b364-0890-4380-8c36-a142fcb29034", "name": "SESSION_VALIDATE", "description": "Validate a token.", "slots": [ { "key": "token", "name": "TOKEN", "direction": "REQUEST", "mandatory": "true", "type": "UUID", "description": "The token to validate." }, { "key": "userId", "name": "USER_ID", "direction": "ANSWER", "mandatory": "false", "type": "STRING", "description": "The user ID (only on SUCCESS)." }, { "key": "rights", "name": "RIGHTS", "direction": "ANSWER", "mandatory": "false", "type": "STRING_ARRAY", "description": "The rights the user has (only on SUCCESS)." } ] }
Example of use of the class CRecordSessionValidate (after generating)
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(@Nullable final UUID aSessionToken) throws CException { final CEnvelope env = CEnvelope.forMicroService(SESSION_MICROSERVICE_ID); final CRecord record = CRecordSessionValidate.create(); CRecordSessionValidate.setToken(record, aSessionToken); aTarget.sendRequest(env, record); }
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 UUID token = CRecordSessionValidate.getToken(aRecord, null); final String userId = CRecordSessionValidate.getUserId(aRecord, null); final String[] rights = CRecordSessionValidate.getRights(aRecord, null); // ... } return true; } return false; }