How to create custom remote UserDirectory for Bitbucket Datacenter edition

I would like to enable Bitbucket Datacenter to validate users and groups with my own systems.
I would like to do something like the existing Active Directory Connector but having myself the control on which users and groups to be created.

From what I have read, I have to implement RemoteDirectory class.

from which api should I implemnt it?
com.atlassian.crowd.directory.RemoteDirectory from maven package crowd-api or should I use a specific one for bitbucket.

Also how should I include my class into the running bitbucket?

Is there any example on how to implement the methods?
I mean I know how to get the information from my system but not how return the info to bitbucket.

Thanks.