Hi,
Accessing the definition of candidate groups via the TaskDefinition is not a feature that we provide, so there is no API to access it. If you are interested in the contents that are written in the BPMN XML, you could parse the candidate groups from the model using the BPMN model api [1]. Note that in delegation code like an execution listener, you can use the method DelegateExecution#getBpmnModelElementInstance to access the BPMN activity that is currently executed as represented by the model API.
For your use case, you could also employ Camunda's authorization features [2]. Users that are candidates or assignees for a task automatically receive the right to read and update a task.
Cheers,
Thorben
[1]
https://docs.camunda.org/manual/latest/user-guide/model-api/bpmn-model-api/read-a-model/[2]
https://docs.camunda.org/manual/latest/user-guide/process-engine/authorization-service/