Owner providers in the Authority system represent the resource owners—typically the users who own the data or resources being accessed. They play a crucial role in controlling access to their resources.
Configuring Owner Providers
To configure an owner provider, you need to establish ownership models in your application. This usually involves mapping user records to resources that they own.
In your database schema, make sure that resources have an owner_id field that corresponds to the user who owns the resource.
CREATETABLEresources ( id SERIALPRIMARY KEY, owner_id INTEGERREFERENCES users(id));
Using Owner Providers
Once the ownership structure is in place, you can enforce access control rules by checking whether the currently authenticated user is the owner of the resource they are trying to access.
Example in Crystal:
# Assuming `current_user` is the authenticated user and `resource` is the requested resource.moduleAuthorityclassOwnerProviderincludeAuthly::AuthorizableOwnerdefauthorized?(username:String, password:String) :BoolOwnerRepo.authenticate? username, passwordenddefid_token(user_id:String) :Hash(String,Int64|String)OwnerRepo.id_token user_idendendend
Owner providers help implement fine-grained access control mechanisms, ensuring that users can only access the resources they own.