You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltaspike.apache.org by Shane Bryzak <sb...@redhat.com> on 2012/04/18 00:52:58 UTC

[DISCUSS] DELTASPIKE-79 Authorization API

I'd like to kick off a discussion around the Authorization API, 
specifically object permissions.  This API is used to determine whether 
the currently authenticated user has the necessary privileges to perform 
an operation on a particular domain object.  I'll start by outlining my 
proposal covering the simple use cases of this API and we can then 
proceed from there.

First of all, the developer needs a gateway into the permission API so 
that they can perform permission checks within their own application.  
This is provided by the hasPermission() method:


Identity.hasPermission(Object resource, String operation)


A permission has three aspects; 1) The application resource for which 
the permission is granted, 2) The operation that has been granted for 
that resource, and 3) The recipient of the permission, which may be 
either a User, Group or Role.

For example, if we wish to check whether the user has permission to edit 
a Customer instance the code might look like this:

@Inject Identity identity;

public void editCustomer(Customer customer)
{
     if (!identity.hasPermission(customer, "EDIT"))
     {
         throw new AuthorizationException("Insufficient privileges to 
edit this customer");
     }

     // snip code
}

We could potentially also do some clever stuff with method-level 
annotations here.  Off the top of my head, something like this might work:

@CheckPermissions
public void editCustomer(@CheckPermission("EDIT") Customer customer)

The @CheckPermissions annotation would be a security binding type with a 
matching authorizer that would scan the parameter list and perform any 
checks on parameters annotated with @CheckPermission.  Anyway we can 
refine this idea in ongoing discussions.

In the default implementation, the Identity.hasPermission() method 
essentially contains no code, instead it delegates the permission check 
to the PermissionMapper bean, an implementation-only bean which contains 
a list of PermissionResolver instances that are used to perform the 
permission check.

public class DefaultIdentity implements Identity
{
     // snip code

     public boolean hasPermission(Object resource, String operation)
     {
         return permissionMapper.resolvePermission(resource, operation);
     }
}

The PermissionMapper bean provides the resolvePermission() method, which 
basically iterates through the known PermissionResolvers, and if one of 
them returns true for the permission check, then a true result is returned.

public class PermissionMapper
{
     @Inject Instance<PermissionResolver> resolvers;

     public boolean resolvePermission(Object resource, String operation)
     {
         for (PermissionResolver resolver : resolvers)
         {
             if (resolver.hasPermission(resource, operation)) return true;
         }
         return false;
     }
}

We can do some clever stuff here, like caching which permission resolver 
returns a true result for a particular class of resource, and then 
always using that resolver for that class, etc.

PermissionResolver is an API interface, the implementations of which do 
the actual work of checking the permission.

public interface PermissionResolver
{
     boolean hasPermission(Object resource, String operation);
}

We would provide one PermissionResolver implementation out of the box 
with DeltaSpike; PersistentPermissionResolver would provide permission 
checks for ACL-style object permissions and provide a key piece of 
functionality required by a complete permission management API.  
Developers can easily provide their own PermissionResolver 
implementations with custom business logic by simply deploying a 
PermissionResolver bean in their own application.

This pretty much covers the basics of the object permission API, at 
least on the consuming side.  We can discuss this area first before 
moving onto the permission management API shortly.


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi bruno,

@ prototype:
please have a look at [1].

regards,
gerhard

[1]
https://cwiki.apache.org/confluence/display/DeltaSpike/Suggested+Git+Workflows#SuggestedGitWorkflows-Discussionworkflow(optional)



2012/4/18 Bruno Oliveira <br...@abstractj.org>

> On Tue, Apr 17, 2012 at 7:52 PM, Shane Bryzak <sb...@redhat.com> wrote:
>
> > I'd like to kick off a discussion around the Authorization API,
> > specifically object permissions.  This API is used to determine whether
> the
> > currently authenticated user has the necessary privileges to perform an
> > operation on a particular domain object.  I'll start by outlining my
> > proposal covering the simple use cases of this API and we can then
> proceed
> > from there.
> >
> > First of all, the developer needs a gateway into the permission API so
> > that they can perform permission checks within their own application.
>  This
> > is provided by the hasPermission() method:
> >
>
> Hi Shane, good catch start to talk about it. Are you thinking about
> something using CDI interceptors or observers? To permission checks?
>
> >
> >
> > Identity.hasPermission(Object resource, String operation)
> >
>
> Does 'operation' here means CRUD operations?
>
> >
> >
> > A permission has three aspects; 1) The application resource for which the
> > permission is granted, 2) The operation that has been granted for that
> > resource, and 3) The recipient of the permission, which may be either a
> > User, Group or Role.
> >
> > For example, if we wish to check whether the user has permission to edit
> a
> > Customer instance the code might look like this:
> >
> > @Inject Identity identity;
> >
> > public void editCustomer(Customer customer)
> > {
> >    if (!identity.hasPermission(**customer, "EDIT"))
> >    {
> >        throw new AuthorizationException("**Insufficient privileges to
> > edit this customer");
> >    }
> >
> >    // snip code
> > }
> >
>
> I like your idea. Currently I'm working on something similar to it with
> interceptors:
>
> @RequestScoped
> public class DummyService {
>
>    @SecurityRole(name = "admin")
>    public void editCustomer(Customer customer) {
>        //Something here
>     }
> }
>
>
> >
> > We could potentially also do some clever stuff with method-level
> > annotations here.  Off the top of my head, something like this might
> work:
> >
> > @CheckPermissions
> > public void editCustomer(@CheckPermission(**"EDIT") Customer customer)
> >
>
> It would be great to specify "EDIT" on top of the method only
> with @CheckPermission annotation.
>
> >
> > The @CheckPermissions annotation would be a security binding type with a
> > matching authorizer that would scan the parameter list and perform any
> > checks on parameters annotated with @CheckPermission.  Anyway we can
> refine
> > this idea in ongoing discussions.
> >
>
> Do you think that's a good idea do something like this?
>
> @AroundInvoke
>  public Object filterDeniedInvocations(InvocationContext invocationContext)
> throws Exception {
>      //read annotation value here with parameter list and perform checks
> }
>
> >
> > In the default implementation, the Identity.hasPermission() method
> > essentially contains no code, instead it delegates the permission check
> to
> > the PermissionMapper bean, an implementation-only bean which contains a
> > list of PermissionResolver instances that are used to perform the
> > permission check.
> >
> > public class DefaultIdentity implements Identity
> > {
> >    // snip code
> >
> >    public boolean hasPermission(Object resource, String operation)
> >    {
> >        return permissionMapper.**resolvePermission(resource, operation);
> >    }
> > }
> >
> > The PermissionMapper bean provides the resolvePermission() method, which
> > basically iterates through the known PermissionResolvers, and if one of
> > them returns true for the permission check, then a true result is
> returned.
> >
> > public class PermissionMapper
> > {
> >    @Inject Instance<PermissionResolver> resolvers;
> >
> >    public boolean resolvePermission(Object resource, String operation)
> >    {
> >        for (PermissionResolver resolver : resolvers)
> >        {
> >            if (resolver.hasPermission(**resource, operation)) return
> true;
> >        }
> >        return false;
> >    }
> > }
> >
> > We can do some clever stuff here, like caching which permission resolver
> > returns a true result for a particular class of resource, and then always
> > using that resolver for that class, etc.
> >
>
> How do you think to cache it?
>
> >
> > PermissionResolver is an API interface, the implementations of which do
> > the actual work of checking the permission.
> >
> > public interface PermissionResolver
> > {
> >    boolean hasPermission(Object resource, String operation);
> > }
> >
> > We would provide one PermissionResolver implementation out of the box
> with
> > DeltaSpike; PersistentPermissionResolver would provide permission checks
> > for ACL-style object permissions and provide a key piece of functionality
> > required by a complete permission management API.  Developers can easily
> > provide their own PermissionResolver implementations with custom business
> > logic by simply deploying a PermissionResolver bean in their own
> > application.
> >
> > This pretty much covers the basics of the object permission API, at least
> > on the consuming side.  We can discuss this area first before moving onto
> > the permission management API shortly.
> >
> > What do you think about a separated branch to prototype it? Let me know
> if
> I could help with something.
>
>
> --
>
> --
> "Know the rules well, so you can break them effectively" - Dalai Lama XIV
> -
> @abstractj
> -
> Volenti Nihil Difficile
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi @ all,

> @SecurityRole(name = "admin")

if i remember correctly, we agreed on improving @SecurityBindingType to
allow such a (custom) annotation easily.

> PermissionResolver + PermissionType:

that wouldn't be a resolver. a resolver just resolves the information.
a voter or evaluator should do the calculation (right now we have a
AccessDecisionVoter in place - we can refactor it and add the concepts
of ResourceIdentifier and Operation to AccessDecisionVoterContext or
ResourceAwareAccessDecisionVoterContext).

> ResourceIdentifier:

+1 (but it should be an interface)
and we should do the same with the operation/action - e.g.:

public interface Operation
{
}

public enum CrudOperation implements Operation
{
    CREATE, READ, UPDATE, DELETE
}

->
identity.hasPermission(new CustomerResourceIdentifier(customer),
CrudOperation.UPDATE)

> el use-case:
imo we don't need it right now. the upcoming jsf-module (or
el-security-module) can provide a helper which provides it.
we can even keep the bean-name "identity", because the security-module
doesn't need to provide such a named bean.

regards,
gerhard



2012/4/19 Bruno Oliveira <br...@abstractj.org>

> Hi Shane, if I'm following your thought the idea is to have security on
> object-level and not just check methods (resource level) as I was thinking
> with @CheckPermissions on the method level.
>
> About filterDeniedInvocations I got it borrowed from SecurityInterceptor.
> I'm checking permissions on the method level only without going too deep
> with object-level.
>
> Thanks for clarify.
>
> On Wed, Apr 18, 2012 at 7:02 PM, Shane Bryzak <sb...@redhat.com> wrote:
>
> > On 19/04/12 06:35, Bruno Oliveira wrote:
> >
> >> On Tue, Apr 17, 2012 at 7:52 PM, Shane Bryzak<sb...@redhat.com>
>  wrote:
> >>
> >>  I'd like to kick off a discussion around the Authorization API,
> >>> specifically object permissions.  This API is used to determine whether
> >>> the
> >>> currently authenticated user has the necessary privileges to perform an
> >>> operation on a particular domain object.  I'll start by outlining my
> >>> proposal covering the simple use cases of this API and we can then
> >>> proceed
> >>> from there.
> >>>
> >>> First of all, the developer needs a gateway into the permission API so
> >>> that they can perform permission checks within their own application.
> >>>  This
> >>> is provided by the hasPermission() method:
> >>>
> >>>  Hi Shane, good catch start to talk about it. Are you thinking about
> >> something using CDI interceptors or observers? To permission checks?
> >>
> >
> > Hi Bruno.  Probably both, we definitely need a programmatic API (the
> > hasPermission() method below) but I think a security binding type is also
> > useful (also detailed below).
> >
> >
> >
> >>
> >>> Identity.hasPermission(Object resource, String operation)
> >>>
> >>>  Does 'operation' here means CRUD operations?
> >>
> >
> > CRUD would be supported here but the operation is not limited to CRUD, it
> > can potentially be anything.
> >
> >
> >
> >>
> >>> A permission has three aspects; 1) The application resource for which
> the
> >>> permission is granted, 2) The operation that has been granted for that
> >>> resource, and 3) The recipient of the permission, which may be either a
> >>> User, Group or Role.
> >>>
> >>> For example, if we wish to check whether the user has permission to
> edit
> >>> a
> >>> Customer instance the code might look like this:
> >>>
> >>> @Inject Identity identity;
> >>>
> >>> public void editCustomer(Customer customer)
> >>> {
> >>>    if (!identity.hasPermission(****customer, "EDIT"))
> >>>    {
> >>>        throw new AuthorizationException("****Insufficient privileges to
> >>> edit this customer");
> >>>    }
> >>>
> >>>    // snip code
> >>> }
> >>>
> >>>  I like your idea. Currently I'm working on something similar to it
> with
> >> interceptors:
> >>
> >> @RequestScoped
> >> public class DummyService {
> >>
> >>     @SecurityRole(name = "admin")
> >>     public void editCustomer(Customer customer) {
> >>         //Something here
> >>     }
> >> }
> >>
> >
> > This is an example of coarse-grained security, whereas ACL is
> fine-grained
> > (object-level) security.
> >
> >
> >
> >>
> >>  We could potentially also do some clever stuff with method-level
> >>> annotations here.  Off the top of my head, something like this might
> >>> work:
> >>>
> >>> @CheckPermissions
> >>> public void editCustomer(@CheckPermission(****"EDIT") Customer
> customer)
> >>>
> >>>  It would be great to specify "EDIT" on top of the method only
> >> with @CheckPermission annotation.
> >>
> >
> > How would you support specifying permission checks on multiple parameters
> > of different types?
>
>
> >
> >
> >>  The @CheckPermissions annotation would be a security binding type with
> a
> >>> matching authorizer that would scan the parameter list and perform any
> >>> checks on parameters annotated with @CheckPermission.  Anyway we can
> >>> refine
> >>> this idea in ongoing discussions.
> >>>
> >>>  Do you think that's a good idea do something like this?
> >>
> >> @AroundInvoke
> >>  public Object filterDeniedInvocations(**InvocationContext
> >> invocationContext)
> >> throws Exception {
> >>       //read annotation value here with parameter list and perform
> checks
> >> }
> >>
> >
> > Not sure what you're suggesting here - could you please elaborate?
> >
> >
> >
> >>  In the default implementation, the Identity.hasPermission() method
> >>> essentially contains no code, instead it delegates the permission check
> >>> to
> >>> the PermissionMapper bean, an implementation-only bean which contains a
> >>> list of PermissionResolver instances that are used to perform the
> >>> permission check.
> >>>
> >>> public class DefaultIdentity implements Identity
> >>> {
> >>>    // snip code
> >>>
> >>>    public boolean hasPermission(Object resource, String operation)
> >>>    {
> >>>        return permissionMapper.****resolvePermission(resource,
> >>> operation);
> >>>    }
> >>> }
> >>>
> >>> The PermissionMapper bean provides the resolvePermission() method,
> which
> >>> basically iterates through the known PermissionResolvers, and if one of
> >>> them returns true for the permission check, then a true result is
> >>> returned.
> >>>
> >>> public class PermissionMapper
> >>> {
> >>>    @Inject Instance<PermissionResolver>  resolvers;
> >>>
> >>>    public boolean resolvePermission(Object resource, String operation)
> >>>    {
> >>>        for (PermissionResolver resolver : resolvers)
> >>>        {
> >>>            if (resolver.hasPermission(****resource, operation)) return
> >>> true;
> >>>        }
> >>>        return false;
> >>>    }
> >>> }
> >>>
> >>> We can do some clever stuff here, like caching which permission
> resolver
> >>> returns a true result for a particular class of resource, and then
> always
> >>> using that resolver for that class, etc.
> >>>
> >>>  How do you think to cache it?
> >>
> >
> > This is really just an implementation detail, but I think a
> > Map<Class,PermissionResolver> would be sufficient.
> >
> >
> >
> >>  PermissionResolver is an API interface, the implementations of which do
> >>> the actual work of checking the permission.
> >>>
> >>> public interface PermissionResolver
> >>> {
> >>>    boolean hasPermission(Object resource, String operation);
> >>> }
> >>>
> >>> We would provide one PermissionResolver implementation out of the box
> >>> with
> >>> DeltaSpike; PersistentPermissionResolver would provide permission
> checks
> >>> for ACL-style object permissions and provide a key piece of
> functionality
> >>> required by a complete permission management API.  Developers can
> easily
> >>> provide their own PermissionResolver implementations with custom
> business
> >>> logic by simply deploying a PermissionResolver bean in their own
> >>> application.
> >>>
> >>> This pretty much covers the basics of the object permission API, at
> least
> >>> on the consuming side.  We can discuss this area first before moving
> onto
> >>> the permission management API shortly.
> >>>
> >>> What do you think about a separated branch to prototype it? Let me know
> >>> if
> >>>
> >> I could help with something.
> >>
> >>
> >>
> > I guess it will depend on whether we introduce any/many changes as a
> > result of discussion.  This is just a small part of the authorization API
> > and really requires some of the other parts (in particular permission
> > management) to be really useful.
> >
> >
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Bruno Oliveira <br...@abstractj.org>.
Hi Shane, if I'm following your thought the idea is to have security on
object-level and not just check methods (resource level) as I was thinking
with @CheckPermissions on the method level.

About filterDeniedInvocations I got it borrowed from SecurityInterceptor.
I'm checking permissions on the method level only without going too deep
with object-level.

Thanks for clarify.

On Wed, Apr 18, 2012 at 7:02 PM, Shane Bryzak <sb...@redhat.com> wrote:

> On 19/04/12 06:35, Bruno Oliveira wrote:
>
>> On Tue, Apr 17, 2012 at 7:52 PM, Shane Bryzak<sb...@redhat.com>  wrote:
>>
>>  I'd like to kick off a discussion around the Authorization API,
>>> specifically object permissions.  This API is used to determine whether
>>> the
>>> currently authenticated user has the necessary privileges to perform an
>>> operation on a particular domain object.  I'll start by outlining my
>>> proposal covering the simple use cases of this API and we can then
>>> proceed
>>> from there.
>>>
>>> First of all, the developer needs a gateway into the permission API so
>>> that they can perform permission checks within their own application.
>>>  This
>>> is provided by the hasPermission() method:
>>>
>>>  Hi Shane, good catch start to talk about it. Are you thinking about
>> something using CDI interceptors or observers? To permission checks?
>>
>
> Hi Bruno.  Probably both, we definitely need a programmatic API (the
> hasPermission() method below) but I think a security binding type is also
> useful (also detailed below).
>
>
>
>>
>>> Identity.hasPermission(Object resource, String operation)
>>>
>>>  Does 'operation' here means CRUD operations?
>>
>
> CRUD would be supported here but the operation is not limited to CRUD, it
> can potentially be anything.
>
>
>
>>
>>> A permission has three aspects; 1) The application resource for which the
>>> permission is granted, 2) The operation that has been granted for that
>>> resource, and 3) The recipient of the permission, which may be either a
>>> User, Group or Role.
>>>
>>> For example, if we wish to check whether the user has permission to edit
>>> a
>>> Customer instance the code might look like this:
>>>
>>> @Inject Identity identity;
>>>
>>> public void editCustomer(Customer customer)
>>> {
>>>    if (!identity.hasPermission(****customer, "EDIT"))
>>>    {
>>>        throw new AuthorizationException("****Insufficient privileges to
>>> edit this customer");
>>>    }
>>>
>>>    // snip code
>>> }
>>>
>>>  I like your idea. Currently I'm working on something similar to it with
>> interceptors:
>>
>> @RequestScoped
>> public class DummyService {
>>
>>     @SecurityRole(name = "admin")
>>     public void editCustomer(Customer customer) {
>>         //Something here
>>     }
>> }
>>
>
> This is an example of coarse-grained security, whereas ACL is fine-grained
> (object-level) security.
>
>
>
>>
>>  We could potentially also do some clever stuff with method-level
>>> annotations here.  Off the top of my head, something like this might
>>> work:
>>>
>>> @CheckPermissions
>>> public void editCustomer(@CheckPermission(****"EDIT") Customer customer)
>>>
>>>  It would be great to specify "EDIT" on top of the method only
>> with @CheckPermission annotation.
>>
>
> How would you support specifying permission checks on multiple parameters
> of different types?


>
>
>>  The @CheckPermissions annotation would be a security binding type with a
>>> matching authorizer that would scan the parameter list and perform any
>>> checks on parameters annotated with @CheckPermission.  Anyway we can
>>> refine
>>> this idea in ongoing discussions.
>>>
>>>  Do you think that's a good idea do something like this?
>>
>> @AroundInvoke
>>  public Object filterDeniedInvocations(**InvocationContext
>> invocationContext)
>> throws Exception {
>>       //read annotation value here with parameter list and perform checks
>> }
>>
>
> Not sure what you're suggesting here - could you please elaborate?
>
>
>
>>  In the default implementation, the Identity.hasPermission() method
>>> essentially contains no code, instead it delegates the permission check
>>> to
>>> the PermissionMapper bean, an implementation-only bean which contains a
>>> list of PermissionResolver instances that are used to perform the
>>> permission check.
>>>
>>> public class DefaultIdentity implements Identity
>>> {
>>>    // snip code
>>>
>>>    public boolean hasPermission(Object resource, String operation)
>>>    {
>>>        return permissionMapper.****resolvePermission(resource,
>>> operation);
>>>    }
>>> }
>>>
>>> The PermissionMapper bean provides the resolvePermission() method, which
>>> basically iterates through the known PermissionResolvers, and if one of
>>> them returns true for the permission check, then a true result is
>>> returned.
>>>
>>> public class PermissionMapper
>>> {
>>>    @Inject Instance<PermissionResolver>  resolvers;
>>>
>>>    public boolean resolvePermission(Object resource, String operation)
>>>    {
>>>        for (PermissionResolver resolver : resolvers)
>>>        {
>>>            if (resolver.hasPermission(****resource, operation)) return
>>> true;
>>>        }
>>>        return false;
>>>    }
>>> }
>>>
>>> We can do some clever stuff here, like caching which permission resolver
>>> returns a true result for a particular class of resource, and then always
>>> using that resolver for that class, etc.
>>>
>>>  How do you think to cache it?
>>
>
> This is really just an implementation detail, but I think a
> Map<Class,PermissionResolver> would be sufficient.
>
>
>
>>  PermissionResolver is an API interface, the implementations of which do
>>> the actual work of checking the permission.
>>>
>>> public interface PermissionResolver
>>> {
>>>    boolean hasPermission(Object resource, String operation);
>>> }
>>>
>>> We would provide one PermissionResolver implementation out of the box
>>> with
>>> DeltaSpike; PersistentPermissionResolver would provide permission checks
>>> for ACL-style object permissions and provide a key piece of functionality
>>> required by a complete permission management API.  Developers can easily
>>> provide their own PermissionResolver implementations with custom business
>>> logic by simply deploying a PermissionResolver bean in their own
>>> application.
>>>
>>> This pretty much covers the basics of the object permission API, at least
>>> on the consuming side.  We can discuss this area first before moving onto
>>> the permission management API shortly.
>>>
>>> What do you think about a separated branch to prototype it? Let me know
>>> if
>>>
>> I could help with something.
>>
>>
>>
> I guess it will depend on whether we introduce any/many changes as a
> result of discussion.  This is just a small part of the authorization API
> and really requires some of the other parts (in particular permission
> management) to be really useful.
>
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Shane Bryzak <sb...@redhat.com>.
On 19/04/12 06:35, Bruno Oliveira wrote:
> On Tue, Apr 17, 2012 at 7:52 PM, Shane Bryzak<sb...@redhat.com>  wrote:
>
>> I'd like to kick off a discussion around the Authorization API,
>> specifically object permissions.  This API is used to determine whether the
>> currently authenticated user has the necessary privileges to perform an
>> operation on a particular domain object.  I'll start by outlining my
>> proposal covering the simple use cases of this API and we can then proceed
>> from there.
>>
>> First of all, the developer needs a gateway into the permission API so
>> that they can perform permission checks within their own application.  This
>> is provided by the hasPermission() method:
>>
> Hi Shane, good catch start to talk about it. Are you thinking about
> something using CDI interceptors or observers? To permission checks?

Hi Bruno.  Probably both, we definitely need a programmatic API (the 
hasPermission() method below) but I think a security binding type is 
also useful (also detailed below).

>
>>
>> Identity.hasPermission(Object resource, String operation)
>>
> Does 'operation' here means CRUD operations?

CRUD would be supported here but the operation is not limited to CRUD, 
it can potentially be anything.

>
>>
>> A permission has three aspects; 1) The application resource for which the
>> permission is granted, 2) The operation that has been granted for that
>> resource, and 3) The recipient of the permission, which may be either a
>> User, Group or Role.
>>
>> For example, if we wish to check whether the user has permission to edit a
>> Customer instance the code might look like this:
>>
>> @Inject Identity identity;
>>
>> public void editCustomer(Customer customer)
>> {
>>     if (!identity.hasPermission(**customer, "EDIT"))
>>     {
>>         throw new AuthorizationException("**Insufficient privileges to
>> edit this customer");
>>     }
>>
>>     // snip code
>> }
>>
> I like your idea. Currently I'm working on something similar to it with
> interceptors:
>
> @RequestScoped
> public class DummyService {
>
>      @SecurityRole(name = "admin")
>      public void editCustomer(Customer customer) {
>          //Something here
>      }
> }

This is an example of coarse-grained security, whereas ACL is 
fine-grained (object-level) security.

>
>
>> We could potentially also do some clever stuff with method-level
>> annotations here.  Off the top of my head, something like this might work:
>>
>> @CheckPermissions
>> public void editCustomer(@CheckPermission(**"EDIT") Customer customer)
>>
> It would be great to specify "EDIT" on top of the method only
> with @CheckPermission annotation.

How would you support specifying permission checks on multiple 
parameters of different types?

>
>> The @CheckPermissions annotation would be a security binding type with a
>> matching authorizer that would scan the parameter list and perform any
>> checks on parameters annotated with @CheckPermission.  Anyway we can refine
>> this idea in ongoing discussions.
>>
> Do you think that's a good idea do something like this?
>
> @AroundInvoke
>   public Object filterDeniedInvocations(InvocationContext invocationContext)
> throws Exception {
>        //read annotation value here with parameter list and perform checks
> }

Not sure what you're suggesting here - could you please elaborate?

>
>> In the default implementation, the Identity.hasPermission() method
>> essentially contains no code, instead it delegates the permission check to
>> the PermissionMapper bean, an implementation-only bean which contains a
>> list of PermissionResolver instances that are used to perform the
>> permission check.
>>
>> public class DefaultIdentity implements Identity
>> {
>>     // snip code
>>
>>     public boolean hasPermission(Object resource, String operation)
>>     {
>>         return permissionMapper.**resolvePermission(resource, operation);
>>     }
>> }
>>
>> The PermissionMapper bean provides the resolvePermission() method, which
>> basically iterates through the known PermissionResolvers, and if one of
>> them returns true for the permission check, then a true result is returned.
>>
>> public class PermissionMapper
>> {
>>     @Inject Instance<PermissionResolver>  resolvers;
>>
>>     public boolean resolvePermission(Object resource, String operation)
>>     {
>>         for (PermissionResolver resolver : resolvers)
>>         {
>>             if (resolver.hasPermission(**resource, operation)) return true;
>>         }
>>         return false;
>>     }
>> }
>>
>> We can do some clever stuff here, like caching which permission resolver
>> returns a true result for a particular class of resource, and then always
>> using that resolver for that class, etc.
>>
> How do you think to cache it?

This is really just an implementation detail, but I think a 
Map<Class,PermissionResolver> would be sufficient.

>
>> PermissionResolver is an API interface, the implementations of which do
>> the actual work of checking the permission.
>>
>> public interface PermissionResolver
>> {
>>     boolean hasPermission(Object resource, String operation);
>> }
>>
>> We would provide one PermissionResolver implementation out of the box with
>> DeltaSpike; PersistentPermissionResolver would provide permission checks
>> for ACL-style object permissions and provide a key piece of functionality
>> required by a complete permission management API.  Developers can easily
>> provide their own PermissionResolver implementations with custom business
>> logic by simply deploying a PermissionResolver bean in their own
>> application.
>>
>> This pretty much covers the basics of the object permission API, at least
>> on the consuming side.  We can discuss this area first before moving onto
>> the permission management API shortly.
>>
>> What do you think about a separated branch to prototype it? Let me know if
> I could help with something.
>
>

I guess it will depend on whether we introduce any/many changes as a 
result of discussion.  This is just a small part of the authorization 
API and really requires some of the other parts (in particular 
permission management) to be really useful.


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Bruno Oliveira <br...@abstractj.org>.
On Tue, Apr 17, 2012 at 7:52 PM, Shane Bryzak <sb...@redhat.com> wrote:

> I'd like to kick off a discussion around the Authorization API,
> specifically object permissions.  This API is used to determine whether the
> currently authenticated user has the necessary privileges to perform an
> operation on a particular domain object.  I'll start by outlining my
> proposal covering the simple use cases of this API and we can then proceed
> from there.
>
> First of all, the developer needs a gateway into the permission API so
> that they can perform permission checks within their own application.  This
> is provided by the hasPermission() method:
>

Hi Shane, good catch start to talk about it. Are you thinking about
something using CDI interceptors or observers? To permission checks?

>
>
> Identity.hasPermission(Object resource, String operation)
>

Does 'operation' here means CRUD operations?

>
>
> A permission has three aspects; 1) The application resource for which the
> permission is granted, 2) The operation that has been granted for that
> resource, and 3) The recipient of the permission, which may be either a
> User, Group or Role.
>
> For example, if we wish to check whether the user has permission to edit a
> Customer instance the code might look like this:
>
> @Inject Identity identity;
>
> public void editCustomer(Customer customer)
> {
>    if (!identity.hasPermission(**customer, "EDIT"))
>    {
>        throw new AuthorizationException("**Insufficient privileges to
> edit this customer");
>    }
>
>    // snip code
> }
>

I like your idea. Currently I'm working on something similar to it with
interceptors:

@RequestScoped
public class DummyService {

    @SecurityRole(name = "admin")
    public void editCustomer(Customer customer) {
        //Something here
    }
}


>
> We could potentially also do some clever stuff with method-level
> annotations here.  Off the top of my head, something like this might work:
>
> @CheckPermissions
> public void editCustomer(@CheckPermission(**"EDIT") Customer customer)
>

It would be great to specify "EDIT" on top of the method only
with @CheckPermission annotation.

>
> The @CheckPermissions annotation would be a security binding type with a
> matching authorizer that would scan the parameter list and perform any
> checks on parameters annotated with @CheckPermission.  Anyway we can refine
> this idea in ongoing discussions.
>

Do you think that's a good idea do something like this?

@AroundInvoke
 public Object filterDeniedInvocations(InvocationContext invocationContext)
throws Exception {
      //read annotation value here with parameter list and perform checks
}

>
> In the default implementation, the Identity.hasPermission() method
> essentially contains no code, instead it delegates the permission check to
> the PermissionMapper bean, an implementation-only bean which contains a
> list of PermissionResolver instances that are used to perform the
> permission check.
>
> public class DefaultIdentity implements Identity
> {
>    // snip code
>
>    public boolean hasPermission(Object resource, String operation)
>    {
>        return permissionMapper.**resolvePermission(resource, operation);
>    }
> }
>
> The PermissionMapper bean provides the resolvePermission() method, which
> basically iterates through the known PermissionResolvers, and if one of
> them returns true for the permission check, then a true result is returned.
>
> public class PermissionMapper
> {
>    @Inject Instance<PermissionResolver> resolvers;
>
>    public boolean resolvePermission(Object resource, String operation)
>    {
>        for (PermissionResolver resolver : resolvers)
>        {
>            if (resolver.hasPermission(**resource, operation)) return true;
>        }
>        return false;
>    }
> }
>
> We can do some clever stuff here, like caching which permission resolver
> returns a true result for a particular class of resource, and then always
> using that resolver for that class, etc.
>

How do you think to cache it?

>
> PermissionResolver is an API interface, the implementations of which do
> the actual work of checking the permission.
>
> public interface PermissionResolver
> {
>    boolean hasPermission(Object resource, String operation);
> }
>
> We would provide one PermissionResolver implementation out of the box with
> DeltaSpike; PersistentPermissionResolver would provide permission checks
> for ACL-style object permissions and provide a key piece of functionality
> required by a complete permission management API.  Developers can easily
> provide their own PermissionResolver implementations with custom business
> logic by simply deploying a PermissionResolver bean in their own
> application.
>
> This pretty much covers the basics of the object permission API, at least
> on the consuming side.  We can discuss this area first before moving onto
> the permission management API shortly.
>
> What do you think about a separated branch to prototype it? Let me know if
I could help with something.


-- 

-- 
"Know the rules well, so you can break them effectively" - Dalai Lama XIV
-
@abstractj
-
Volenti Nihil Difficile

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi marek,

your use-cases are just sub-sets and are still possible with my suggestion.

instead of ResourceIdentifier i'm going to call it ResourceDescriptor.

you can implement an AccessDecisionVoter which is responsible for handling
CustomerResourceDescriptor which provides #getCustomerName.
(again: it isn't a resolver - that's something different.)
if you don't like it, you can even provide #getResource.

@ dyn. operation:
you can still do dyn. (and not type-safe) implementations - e.g.:

public class DynamicOperation implements Operation
{
    private final String operationValue;

    public DynamicOperation(String operationValue)
    {
        this.operationValue = operationValue;
    }

    @Override
    public String toString()
    {
        return this.operationValue;
    }
}

that's also what the el-security module can do.

-> imo

Identity#hasPermission(ResourceDescriptor, Operation) throws
AuthorizationException;

is enough (for sure we have to prototype it).

regards,
gerhard



2012/4/23 Marek Posolda <mp...@redhat.com>

> Hi all,
>
>
>
> On 22.4.2012 19:27, Christian Kaltepoth wrote:
>
>> I prefer the method signature Shane suggested in his initial mail.
>> Providing a single method with an object parameter makes most sense to
>> me:
>>
>>   Identity.hasPermission(Object resource, String operation)
>>
>> This way the developer is completely free to choose how to identify
>> the resource. I think in most cases the object itself will be used.
>> And the JSF example Shane showed is a very good example for this.
>>
>> Of cause people may need other ways to refer to resources like in the
>> ResourceIdentifier concept Marek showed. But actually I see something
>> like this more as an usage pattern and I don't think it belongs
>> directly into the API. Perhaps we could provide something like
>> ResourceIdentifier as an utility class that people can use if they
>> want to refer to resources this way. But the API of Identity should be
>> as simple as possible. And the object parameter will allow people to
>> use any object they want. Even something like ResourceIdentifier.
>>
> That's the question. I think that API should be clear and using single
> method:
>
> Identity.hasPermission(Object resource, String operation)
>
>
> is not clear enough. As in some cases, your argument is directly secured
> object and in other cases it's only ResourceIdentifier for this object.
> Also it would mean that you will need to ask for the type of object in your
> PermissionResolver implementation:
>
> if (resource instanceof ResourceIdentifier)
> {
>  // compute permissions for ResourceIdentifier
> }
> else
> {
>  // Argument is "resource" so compute permissions for this resource
> }
>
>
>
>
> On the other hand, using single method:
> Identity.hasPermission(**ResourceIdentifier resource, Operation operation)
>
> may not be appropriate enough for some use-cases. Let's imagine that you
> have Customer and CustomerResourceIdntifier objects:
>
> public class Customer
> {
>   private String ID;
>   private String customerName;
>
>   // getters and setters here
> }
>
> public class CustomerResourceIdentifier implements ResourceIdentifier
> {
>     private String customerId;
>
>     // getters and setters
> }
>
>
> And you want to create PermissionResolver, which will compute permission
> based on customer name. With the API method:
>
> Identity.hasPermission(**ResourceIdentifier resourceIdentifier, Operation
> operation)
>
>
> you can't manage to do it (unless you change implementation of
> CustomerResourceIdentifier to encapsulate customerName)
>
>
>  Operation
>>
> I agree that big advantage of this approach is type-safety. On the other
> hand, using String directly is more flexible as you have more freedom.
> Let's imagine that you have some permission rules stored in database and
> you want to add new type of operation like "ADMINISTER". With using String
> as argument, you can directly create records in DB and use this new
> "ADMINISTER" argument without doing any changes in Java code. With the
> Operation approach, you would need to add this new ADMINISTER operation
> into your Operation enum:
>
>
> public enum CrudOperation implements Operation
> {
>    CREATE, READ, UPDATE, DELETE, ADMINISTER
> }
>
> I wanted to point this out, but I agree that for most cases is type-safe
> approach with Operation interface probably better:-)
>
> So after all, my opinion is to have two methods:
>
> Identity.hasPermission(Object object, Operation operation) throws
> AuthorizationException;
> Identity.hasPermission(**ResourceIdentifier resourceIdentifier, Operation
> operation) throws AuthorizationException;
>
>
> Thanks,
> Marek
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi shane,

we could keep both methods for now and prototype & discuss the type-safe
approach.
if it works without major disadvantages, we could drop the 2nd method.

regards,
gerhard



2012/4/24 Shane Bryzak <sb...@redhat.com>

> On 23/04/12 20:51, Gerhard Petracek wrote:
>
>> hi shane,
>>
>> imo we just need a solution for your @CheckPermission example.
>> e.g. a custom annotation which can use the custom Operation (e.g. the
>> custom enum) directly. the custom annotation would be meta-annotated with
>> a
>> marker annotation.
>>
>
> The issue was not with the Operation, it was in regards to how the correct
> ResourceIdentifier type could be selected by the authorizer for the
> @CheckPermissions security binding type.  IMO this is a showstopper for the
> case where we only provide the Identity.hasPermission(**ResourceIdentifier,
> ...) method and not Identity.hasPermission(Object, ...) as well.
>
>
>> the rest shouldn't be a problem.
>>
>> regards,
>> gerhard
>>
>>
>>
>> 2012/4/23 Shane Bryzak<sb...@redhat.com>
>>
>>  On 23/04/12 18:03, Marek Posolda wrote:
>>>
>>>  Hi all,
>>>>
>>>>
>>>> On 22.4.2012 19:27, Christian Kaltepoth wrote:
>>>>
>>>>  I prefer the method signature Shane suggested in his initial mail.
>>>>> Providing a single method with an object parameter makes most sense to
>>>>> me:
>>>>>
>>>>>   Identity.hasPermission(Object resource, String operation)
>>>>>
>>>>> This way the developer is completely free to choose how to identify
>>>>> the resource. I think in most cases the object itself will be used.
>>>>> And the JSF example Shane showed is a very good example for this.
>>>>>
>>>>> Of cause people may need other ways to refer to resources like in the
>>>>> ResourceIdentifier concept Marek showed. But actually I see something
>>>>> like this more as an usage pattern and I don't think it belongs
>>>>> directly into the API. Perhaps we could provide something like
>>>>> ResourceIdentifier as an utility class that people can use if they
>>>>> want to refer to resources this way. But the API of Identity should be
>>>>> as simple as possible. And the object parameter will allow people to
>>>>> use any object they want. Even something like ResourceIdentifier.
>>>>>
>>>>>  That's the question. I think that API should be clear and using single
>>>> method:
>>>>
>>>> Identity.hasPermission(Object resource, String operation)
>>>>
>>>>
>>>> is not clear enough. As in some cases, your argument is directly secured
>>>> object and in other cases it's only ResourceIdentifier for this object.
>>>> Also it would mean that you will need to ask for the type of object in
>>>> your
>>>> PermissionResolver implementation:
>>>>
>>>> if (resource instanceof ResourceIdentifier)
>>>> {
>>>>  // compute permissions for ResourceIdentifier
>>>> }
>>>> else
>>>> {
>>>>  // Argument is "resource" so compute permissions for this resource
>>>> }
>>>>
>>>>
>>>>
>>>>
>>>> On the other hand, using single method:
>>>> Identity.hasPermission(****ResourceIdentifier resource, Operation
>>>>
>>>> operation)
>>>>
>>>> may not be appropriate enough for some use-cases. Let's imagine that you
>>>> have Customer and CustomerResourceIdntifier objects:
>>>>
>>>> public class Customer
>>>> {
>>>>   private String ID;
>>>>   private String customerName;
>>>>
>>>>   // getters and setters here
>>>> }
>>>>
>>>> public class CustomerResourceIdentifier implements ResourceIdentifier
>>>> {
>>>>     private String customerId;
>>>>
>>>>     // getters and setters
>>>> }
>>>>
>>>>
>>>> And you want to create PermissionResolver, which will compute permission
>>>> based on customer name. With the API method:
>>>>
>>>> Identity.hasPermission(****ResourceIdentifier resourceIdentifier,
>>>>
>>>> Operation operation)
>>>>
>>>>
>>>> you can't manage to do it (unless you change implementation of
>>>> CustomerResourceIdentifier to encapsulate customerName)
>>>>
>>>>
>>>>  Operation
>>>> I agree that big advantage of this approach is type-safety. On the other
>>>> hand, using String directly is more flexible as you have more freedom.
>>>> Let's imagine that you have some permission rules stored in database and
>>>> you want to add new type of operation like "ADMINISTER". With using
>>>> String
>>>> as argument, you can directly create records in DB and use this new
>>>> "ADMINISTER" argument without doing any changes in Java code. With the
>>>> Operation approach, you would need to add this new ADMINISTER operation
>>>> into your Operation enum:
>>>>
>>>>  This is exactly why the operation cannot be an enum - permissions will
>>> be
>>> stored in the database as string values, and if we use an enum value
>>> there
>>> will be no way of knowing which enum class the operation(s) string needs
>>> to
>>> be converted back into.  I should have actually expanded on the details
>>> of
>>> this a little bit earlier which would have made this more obvious.
>>>
>>> Another use case which I didn't mention is the ability to use bit flags
>>> to
>>> set permissions.  This is extremely important for a database with
>>> millions
>>> of records - instead of using a String in the database to store the
>>> operations it is possible to use a numerical value instead, with each bit
>>> representing a different operation.  It also allows us to define
>>> available
>>> permissions for a particular domain object, which is important when
>>> building management interfaces.
>>>
>>> Let's say we have the following entity:
>>>
>>> @Entity
>>> @Permissions({
>>>    @Permission(operation="create"****),
>>>    @Permission(operation="read"),
>>>    @Permission(operation="update"****),
>>>    @Permission(operation="delete"****)
>>>
>>> })
>>> public class Customer
>>> {
>>>  // snip
>>> }
>>>
>>> The @Permissions annotation tells the PermissionManager that we're
>>> allowed
>>> to assign four specific permissions; create, read, update and delete for
>>> the Customer entity.  We can take this a step further and define a bit
>>> mask
>>> value for each of these:
>>>
>>> @Entity
>>> @Permissions({
>>>    @Permission(operation="create"****, mask = 1),
>>>
>>>    @Permission(operation="read", mask = 2),
>>>    @Permission(operation="update"****, mask = 4),
>>>    @Permission(operation="delete"****, mask = 8)
>>>
>>> })
>>> public class Customer
>>> {
>>>  // snip
>>> }
>>>
>>> By providing mask values, the PermissionManager knows that the cumulative
>>> permissions for this entity will be stored in the database as a numerical
>>> value.  So a user with create, read and update permissions for a
>>> particular
>>> customer will have a stored operation value of 1 (create) + 2 (read) + 4
>>> (update) = 7.
>>>
>>>
>>>
>>>
>>>  public enum CrudOperation implements Operation
>>>> {
>>>>    CREATE, READ, UPDATE, DELETE, ADMINISTER
>>>> }
>>>>
>>>> I wanted to point this out, but I agree that for most cases is type-safe
>>>> approach with Operation interface probably better:-)
>>>>
>>>> So after all, my opinion is to have two methods:
>>>>
>>>> Identity.hasPermission(Object object, Operation operation) throws
>>>> AuthorizationException;
>>>> Identity.hasPermission(****ResourceIdentifier resourceIdentifier,
>>>> Operation operation) throws AuthorizationException;
>>>>
>>>>  +1, I think that having two methods is the best solution, with the
>>> caveat
>>> that overloading like this doesn't cause issues with EL.  The Object
>>> parameter version of this method will most likely delegate to the
>>> ResourceIdentifier method anyway, as a ResourceIdentifier will be
>>> required
>>> for all lookups.  By the way, we should make this as transparent as
>>> possible - out of the box, DeltaSpike should provide a ResourceIdentifier
>>> implementation for entity beans that will build a unique identifier based
>>> on the class name and the primary key value.  Another alternative that we
>>> should support is allowing the user to specify which ResourceIdentifier
>>> to
>>> use on the domain class itself:
>>>
>>> @Entity
>>> @Identifier(****CustomerResourceIdentifier.****class)
>>>
>>> public class Customer
>>> {
>>>
>>> }
>>>
>>> Yet another alternative that we should support is the packaging of
>>> additional ResourceIdentifier implementations with an application, that
>>> are
>>> automatically discovered and iterated through when the permission check
>>> can't determine which ResourceIdentifier to use.  With that in mind, the
>>> ResourceIdentifier interface should look like this:
>>>
>>> public interface ResourceIdentifier
>>> {
>>>    boolean canIdentify(Class targetClass);
>>>    String getIdentifier(Object resource);
>>> }
>>>
>>> The canIdentify() method will return true if the specified class is one
>>> that this ResourceIdentifier is capable of generating identifier strings
>>> for.
>>>
>>>
>>>  Thanks,
>>>> Marek
>>>>
>>>>
>>>
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Shane Bryzak <sb...@redhat.com>.
On 23/04/12 20:51, Gerhard Petracek wrote:
> hi shane,
>
> imo we just need a solution for your @CheckPermission example.
> e.g. a custom annotation which can use the custom Operation (e.g. the
> custom enum) directly. the custom annotation would be meta-annotated with a
> marker annotation.

The issue was not with the Operation, it was in regards to how the 
correct ResourceIdentifier type could be selected by the authorizer for 
the @CheckPermissions security binding type.  IMO this is a showstopper 
for the case where we only provide the 
Identity.hasPermission(ResourceIdentifier, ...) method and not 
Identity.hasPermission(Object, ...) as well.

>
> the rest shouldn't be a problem.
>
> regards,
> gerhard
>
>
>
> 2012/4/23 Shane Bryzak<sb...@redhat.com>
>
>> On 23/04/12 18:03, Marek Posolda wrote:
>>
>>> Hi all,
>>>
>>>
>>> On 22.4.2012 19:27, Christian Kaltepoth wrote:
>>>
>>>> I prefer the method signature Shane suggested in his initial mail.
>>>> Providing a single method with an object parameter makes most sense to
>>>> me:
>>>>
>>>>    Identity.hasPermission(Object resource, String operation)
>>>>
>>>> This way the developer is completely free to choose how to identify
>>>> the resource. I think in most cases the object itself will be used.
>>>> And the JSF example Shane showed is a very good example for this.
>>>>
>>>> Of cause people may need other ways to refer to resources like in the
>>>> ResourceIdentifier concept Marek showed. But actually I see something
>>>> like this more as an usage pattern and I don't think it belongs
>>>> directly into the API. Perhaps we could provide something like
>>>> ResourceIdentifier as an utility class that people can use if they
>>>> want to refer to resources this way. But the API of Identity should be
>>>> as simple as possible. And the object parameter will allow people to
>>>> use any object they want. Even something like ResourceIdentifier.
>>>>
>>> That's the question. I think that API should be clear and using single
>>> method:
>>>
>>> Identity.hasPermission(Object resource, String operation)
>>>
>>>
>>> is not clear enough. As in some cases, your argument is directly secured
>>> object and in other cases it's only ResourceIdentifier for this object.
>>> Also it would mean that you will need to ask for the type of object in your
>>> PermissionResolver implementation:
>>>
>>> if (resource instanceof ResourceIdentifier)
>>> {
>>>   // compute permissions for ResourceIdentifier
>>> }
>>> else
>>> {
>>>   // Argument is "resource" so compute permissions for this resource
>>> }
>>>
>>>
>>>
>>>
>>> On the other hand, using single method:
>>> Identity.hasPermission(**ResourceIdentifier resource, Operation
>>> operation)
>>>
>>> may not be appropriate enough for some use-cases. Let's imagine that you
>>> have Customer and CustomerResourceIdntifier objects:
>>>
>>> public class Customer
>>> {
>>>    private String ID;
>>>    private String customerName;
>>>
>>>    // getters and setters here
>>> }
>>>
>>> public class CustomerResourceIdentifier implements ResourceIdentifier
>>> {
>>>      private String customerId;
>>>
>>>      // getters and setters
>>> }
>>>
>>>
>>> And you want to create PermissionResolver, which will compute permission
>>> based on customer name. With the API method:
>>>
>>> Identity.hasPermission(**ResourceIdentifier resourceIdentifier,
>>> Operation operation)
>>>
>>>
>>> you can't manage to do it (unless you change implementation of
>>> CustomerResourceIdentifier to encapsulate customerName)
>>>
>>>
>>>   Operation
>>> I agree that big advantage of this approach is type-safety. On the other
>>> hand, using String directly is more flexible as you have more freedom.
>>> Let's imagine that you have some permission rules stored in database and
>>> you want to add new type of operation like "ADMINISTER". With using String
>>> as argument, you can directly create records in DB and use this new
>>> "ADMINISTER" argument without doing any changes in Java code. With the
>>> Operation approach, you would need to add this new ADMINISTER operation
>>> into your Operation enum:
>>>
>> This is exactly why the operation cannot be an enum - permissions will be
>> stored in the database as string values, and if we use an enum value there
>> will be no way of knowing which enum class the operation(s) string needs to
>> be converted back into.  I should have actually expanded on the details of
>> this a little bit earlier which would have made this more obvious.
>>
>> Another use case which I didn't mention is the ability to use bit flags to
>> set permissions.  This is extremely important for a database with millions
>> of records - instead of using a String in the database to store the
>> operations it is possible to use a numerical value instead, with each bit
>> representing a different operation.  It also allows us to define available
>> permissions for a particular domain object, which is important when
>> building management interfaces.
>>
>> Let's say we have the following entity:
>>
>> @Entity
>> @Permissions({
>>     @Permission(operation="create"**),
>>     @Permission(operation="read"),
>>     @Permission(operation="update"**),
>>     @Permission(operation="delete"**)
>> })
>> public class Customer
>> {
>>   // snip
>> }
>>
>> The @Permissions annotation tells the PermissionManager that we're allowed
>> to assign four specific permissions; create, read, update and delete for
>> the Customer entity.  We can take this a step further and define a bit mask
>> value for each of these:
>>
>> @Entity
>> @Permissions({
>>     @Permission(operation="create"**, mask = 1),
>>     @Permission(operation="read", mask = 2),
>>     @Permission(operation="update"**, mask = 4),
>>     @Permission(operation="delete"**, mask = 8)
>> })
>> public class Customer
>> {
>>   // snip
>> }
>>
>> By providing mask values, the PermissionManager knows that the cumulative
>> permissions for this entity will be stored in the database as a numerical
>> value.  So a user with create, read and update permissions for a particular
>> customer will have a stored operation value of 1 (create) + 2 (read) + 4
>> (update) = 7.
>>
>>
>>
>>
>>> public enum CrudOperation implements Operation
>>> {
>>>     CREATE, READ, UPDATE, DELETE, ADMINISTER
>>> }
>>>
>>> I wanted to point this out, but I agree that for most cases is type-safe
>>> approach with Operation interface probably better:-)
>>>
>>> So after all, my opinion is to have two methods:
>>>
>>> Identity.hasPermission(Object object, Operation operation) throws
>>> AuthorizationException;
>>> Identity.hasPermission(**ResourceIdentifier resourceIdentifier,
>>> Operation operation) throws AuthorizationException;
>>>
>> +1, I think that having two methods is the best solution, with the caveat
>> that overloading like this doesn't cause issues with EL.  The Object
>> parameter version of this method will most likely delegate to the
>> ResourceIdentifier method anyway, as a ResourceIdentifier will be required
>> for all lookups.  By the way, we should make this as transparent as
>> possible - out of the box, DeltaSpike should provide a ResourceIdentifier
>> implementation for entity beans that will build a unique identifier based
>> on the class name and the primary key value.  Another alternative that we
>> should support is allowing the user to specify which ResourceIdentifier to
>> use on the domain class itself:
>>
>> @Entity
>> @Identifier(**CustomerResourceIdentifier.**class)
>> public class Customer
>> {
>>
>> }
>>
>> Yet another alternative that we should support is the packaging of
>> additional ResourceIdentifier implementations with an application, that are
>> automatically discovered and iterated through when the permission check
>> can't determine which ResourceIdentifier to use.  With that in mind, the
>> ResourceIdentifier interface should look like this:
>>
>> public interface ResourceIdentifier
>> {
>>     boolean canIdentify(Class targetClass);
>>     String getIdentifier(Object resource);
>> }
>>
>> The canIdentify() method will return true if the specified class is one
>> that this ResourceIdentifier is capable of generating identifier strings
>> for.
>>
>>
>>> Thanks,
>>> Marek
>>>
>>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi shane,

imo we just need a solution for your @CheckPermission example.
e.g. a custom annotation which can use the custom Operation (e.g. the
custom enum) directly. the custom annotation would be meta-annotated with a
marker annotation.

the rest shouldn't be a problem.

regards,
gerhard



2012/4/23 Shane Bryzak <sb...@redhat.com>

> On 23/04/12 18:03, Marek Posolda wrote:
>
>> Hi all,
>>
>>
>> On 22.4.2012 19:27, Christian Kaltepoth wrote:
>>
>>> I prefer the method signature Shane suggested in his initial mail.
>>> Providing a single method with an object parameter makes most sense to
>>> me:
>>>
>>>   Identity.hasPermission(Object resource, String operation)
>>>
>>> This way the developer is completely free to choose how to identify
>>> the resource. I think in most cases the object itself will be used.
>>> And the JSF example Shane showed is a very good example for this.
>>>
>>> Of cause people may need other ways to refer to resources like in the
>>> ResourceIdentifier concept Marek showed. But actually I see something
>>> like this more as an usage pattern and I don't think it belongs
>>> directly into the API. Perhaps we could provide something like
>>> ResourceIdentifier as an utility class that people can use if they
>>> want to refer to resources this way. But the API of Identity should be
>>> as simple as possible. And the object parameter will allow people to
>>> use any object they want. Even something like ResourceIdentifier.
>>>
>> That's the question. I think that API should be clear and using single
>> method:
>>
>> Identity.hasPermission(Object resource, String operation)
>>
>>
>> is not clear enough. As in some cases, your argument is directly secured
>> object and in other cases it's only ResourceIdentifier for this object.
>> Also it would mean that you will need to ask for the type of object in your
>> PermissionResolver implementation:
>>
>> if (resource instanceof ResourceIdentifier)
>> {
>>  // compute permissions for ResourceIdentifier
>> }
>> else
>> {
>>  // Argument is "resource" so compute permissions for this resource
>> }
>>
>>
>>
>>
>> On the other hand, using single method:
>> Identity.hasPermission(**ResourceIdentifier resource, Operation
>> operation)
>>
>> may not be appropriate enough for some use-cases. Let's imagine that you
>> have Customer and CustomerResourceIdntifier objects:
>>
>> public class Customer
>> {
>>   private String ID;
>>   private String customerName;
>>
>>   // getters and setters here
>> }
>>
>> public class CustomerResourceIdentifier implements ResourceIdentifier
>> {
>>     private String customerId;
>>
>>     // getters and setters
>> }
>>
>>
>> And you want to create PermissionResolver, which will compute permission
>> based on customer name. With the API method:
>>
>> Identity.hasPermission(**ResourceIdentifier resourceIdentifier,
>> Operation operation)
>>
>>
>> you can't manage to do it (unless you change implementation of
>> CustomerResourceIdentifier to encapsulate customerName)
>>
>>
>>  Operation
>>>
>> I agree that big advantage of this approach is type-safety. On the other
>> hand, using String directly is more flexible as you have more freedom.
>> Let's imagine that you have some permission rules stored in database and
>> you want to add new type of operation like "ADMINISTER". With using String
>> as argument, you can directly create records in DB and use this new
>> "ADMINISTER" argument without doing any changes in Java code. With the
>> Operation approach, you would need to add this new ADMINISTER operation
>> into your Operation enum:
>>
>
> This is exactly why the operation cannot be an enum - permissions will be
> stored in the database as string values, and if we use an enum value there
> will be no way of knowing which enum class the operation(s) string needs to
> be converted back into.  I should have actually expanded on the details of
> this a little bit earlier which would have made this more obvious.
>
> Another use case which I didn't mention is the ability to use bit flags to
> set permissions.  This is extremely important for a database with millions
> of records - instead of using a String in the database to store the
> operations it is possible to use a numerical value instead, with each bit
> representing a different operation.  It also allows us to define available
> permissions for a particular domain object, which is important when
> building management interfaces.
>
> Let's say we have the following entity:
>
> @Entity
> @Permissions({
>    @Permission(operation="create"**),
>    @Permission(operation="read"),
>    @Permission(operation="update"**),
>    @Permission(operation="delete"**)
> })
> public class Customer
> {
>  // snip
> }
>
> The @Permissions annotation tells the PermissionManager that we're allowed
> to assign four specific permissions; create, read, update and delete for
> the Customer entity.  We can take this a step further and define a bit mask
> value for each of these:
>
> @Entity
> @Permissions({
>    @Permission(operation="create"**, mask = 1),
>    @Permission(operation="read", mask = 2),
>    @Permission(operation="update"**, mask = 4),
>    @Permission(operation="delete"**, mask = 8)
> })
> public class Customer
> {
>  // snip
> }
>
> By providing mask values, the PermissionManager knows that the cumulative
> permissions for this entity will be stored in the database as a numerical
> value.  So a user with create, read and update permissions for a particular
> customer will have a stored operation value of 1 (create) + 2 (read) + 4
> (update) = 7.
>
>
>
>
>> public enum CrudOperation implements Operation
>> {
>>    CREATE, READ, UPDATE, DELETE, ADMINISTER
>> }
>>
>> I wanted to point this out, but I agree that for most cases is type-safe
>> approach with Operation interface probably better:-)
>>
>> So after all, my opinion is to have two methods:
>>
>> Identity.hasPermission(Object object, Operation operation) throws
>> AuthorizationException;
>> Identity.hasPermission(**ResourceIdentifier resourceIdentifier,
>> Operation operation) throws AuthorizationException;
>>
>
> +1, I think that having two methods is the best solution, with the caveat
> that overloading like this doesn't cause issues with EL.  The Object
> parameter version of this method will most likely delegate to the
> ResourceIdentifier method anyway, as a ResourceIdentifier will be required
> for all lookups.  By the way, we should make this as transparent as
> possible - out of the box, DeltaSpike should provide a ResourceIdentifier
> implementation for entity beans that will build a unique identifier based
> on the class name and the primary key value.  Another alternative that we
> should support is allowing the user to specify which ResourceIdentifier to
> use on the domain class itself:
>
> @Entity
> @Identifier(**CustomerResourceIdentifier.**class)
> public class Customer
> {
>
> }
>
> Yet another alternative that we should support is the packaging of
> additional ResourceIdentifier implementations with an application, that are
> automatically discovered and iterated through when the permission check
> can't determine which ResourceIdentifier to use.  With that in mind, the
> ResourceIdentifier interface should look like this:
>
> public interface ResourceIdentifier
> {
>    boolean canIdentify(Class targetClass);
>    String getIdentifier(Object resource);
> }
>
> The canIdentify() method will return true if the specified class is one
> that this ResourceIdentifier is capable of generating identifier strings
> for.
>
>
>>
>> Thanks,
>> Marek
>>
>
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Marek Posolda <mp...@redhat.com>.
>> So after all, my opinion is to have two methods:
>>
>> Identity.hasPermission(Object object, Operation operation) throws 
>> AuthorizationException;
>> Identity.hasPermission(ResourceIdentifier resourceIdentifier, 
>> Operation operation) throws AuthorizationException;
>
> +1, I think that having two methods is the best solution, with the 
> caveat that overloading like this doesn't cause issues with EL.  The 
> Object parameter version of this method will most likely delegate to 
> the ResourceIdentifier method anyway, as a ResourceIdentifier will be 
> required for all lookups.  By the way, we should make this as 
> transparent as possible - out of the box, DeltaSpike should provide a 
> ResourceIdentifier implementation for entity beans that will build a 
> unique identifier based on the class name and the primary key value.  
> Another alternative that we should support is allowing the user to 
> specify which ResourceIdentifier to use on the domain class itself:
>
> @Entity
> @Identifier(CustomerResourceIdentifier.class)
> public class Customer
> {
>
> }
>
> Yet another alternative that we should support is the packaging of 
> additional ResourceIdentifier implementations with an application, 
> that are automatically discovered and iterated through when the 
> permission check can't determine which ResourceIdentifier to use.  
> With that in mind, the ResourceIdentifier interface should look like 
> this:
>
> public interface ResourceIdentifier
> {
>     boolean canIdentify(Class targetClass);
>     String getIdentifier(Object resource);
> }
>
> The canIdentify() method will return true if the specified class is 
> one that this ResourceIdentifier is capable of generating identifier 
> strings for.
That will be nice. Another idea can be to have some interface, which 
needs to be implemented by the Resource object and ResourceIdentifier 
can use it then. Like:

public interface Resource
{
   public ResourceIdentifier getIdentifier();
}

public class Customer implements Resource
{

   private customerId;

   // snippet


   public ResourceIdentifier getIdentifier
   {
       return  new ResourceIdentifier(Customer.class.getName(), 
getCustomerId());
   }

}

And then Identity like

public Identity
{

   public hasPermission(Object resource, String action)
   {
      if (resource instanceof Resource)
      {
            return hasPermission((Resource)resource.getIdentifier(), 
action);
      }
      else
      {
          // use other ways to map ResourceIdentifier from Object
       }
   }

   public hasPermission(ResourceIdentifier resIdentifier, action)
   {
        // snipet
    }

}

With this design, it would be easy to map ResourceIdentifier for various 
type of objects. Of course, limitation is that Object needs to implement 
the Resource interface. which can't be always done. So we will need 
toher ways for ResourceIdentifier mapping anyway.

>
>>
>>
>> Thanks,
>> Marek
>
>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Shane Bryzak <sb...@redhat.com>.
On 23/04/12 18:03, Marek Posolda wrote:
> Hi all,
>
>
> On 22.4.2012 19:27, Christian Kaltepoth wrote:
>> I prefer the method signature Shane suggested in his initial mail.
>> Providing a single method with an object parameter makes most sense to
>> me:
>>
>>    Identity.hasPermission(Object resource, String operation)
>>
>> This way the developer is completely free to choose how to identify
>> the resource. I think in most cases the object itself will be used.
>> And the JSF example Shane showed is a very good example for this.
>>
>> Of cause people may need other ways to refer to resources like in the
>> ResourceIdentifier concept Marek showed. But actually I see something
>> like this more as an usage pattern and I don't think it belongs
>> directly into the API. Perhaps we could provide something like
>> ResourceIdentifier as an utility class that people can use if they
>> want to refer to resources this way. But the API of Identity should be
>> as simple as possible. And the object parameter will allow people to
>> use any object they want. Even something like ResourceIdentifier.
> That's the question. I think that API should be clear and using single 
> method:
>
> Identity.hasPermission(Object resource, String operation)
>
>
> is not clear enough. As in some cases, your argument is directly 
> secured object and in other cases it's only ResourceIdentifier for 
> this object. Also it would mean that you will need to ask for the type 
> of object in your PermissionResolver implementation:
>
> if (resource instanceof ResourceIdentifier)
> {
>   // compute permissions for ResourceIdentifier
> }
> else
> {
>   // Argument is "resource" so compute permissions for this resource
> }
>
>
>
>
> On the other hand, using single method:
> Identity.hasPermission(ResourceIdentifier resource, Operation operation)
>
> may not be appropriate enough for some use-cases. Let's imagine that 
> you have Customer and CustomerResourceIdntifier objects:
>
> public class Customer
> {
>    private String ID;
>    private String customerName;
>
>    // getters and setters here
> }
>
> public class CustomerResourceIdentifier implements ResourceIdentifier
> {
>      private String customerId;
>
>      // getters and setters
> }
>
>
> And you want to create PermissionResolver, which will compute 
> permission based on customer name. With the API method:
>
> Identity.hasPermission(ResourceIdentifier resourceIdentifier, 
> Operation operation)
>
>
> you can't manage to do it (unless you change implementation of 
> CustomerResourceIdentifier to encapsulate customerName)
>
>
>> Operation
> I agree that big advantage of this approach is type-safety. On the 
> other hand, using String directly is more flexible as you have more 
> freedom. Let's imagine that you have some permission rules stored in 
> database and you want to add new type of operation like "ADMINISTER". 
> With using String as argument, you can directly create records in DB 
> and use this new "ADMINISTER" argument without doing any changes in 
> Java code. With the Operation approach, you would need to add this new 
> ADMINISTER operation into your Operation enum:

This is exactly why the operation cannot be an enum - permissions will 
be stored in the database as string values, and if we use an enum value 
there will be no way of knowing which enum class the operation(s) string 
needs to be converted back into.  I should have actually expanded on the 
details of this a little bit earlier which would have made this more 
obvious.

Another use case which I didn't mention is the ability to use bit flags 
to set permissions.  This is extremely important for a database with 
millions of records - instead of using a String in the database to store 
the operations it is possible to use a numerical value instead, with 
each bit representing a different operation.  It also allows us to 
define available permissions for a particular domain object, which is 
important when building management interfaces.

Let's say we have the following entity:

@Entity
@Permissions({
     @Permission(operation="create"),
     @Permission(operation="read"),
     @Permission(operation="update"),
     @Permission(operation="delete")
})
public class Customer
{
   // snip
}

The @Permissions annotation tells the PermissionManager that we're 
allowed to assign four specific permissions; create, read, update and 
delete for the Customer entity.  We can take this a step further and 
define a bit mask value for each of these:

@Entity
@Permissions({
     @Permission(operation="create", mask = 1),
     @Permission(operation="read", mask = 2),
     @Permission(operation="update", mask = 4),
     @Permission(operation="delete", mask = 8)
})
public class Customer
{
   // snip
}

By providing mask values, the PermissionManager knows that the 
cumulative permissions for this entity will be stored in the database as 
a numerical value.  So a user with create, read and update permissions 
for a particular customer will have a stored operation value of 1 
(create) + 2 (read) + 4 (update) = 7.


>
> public enum CrudOperation implements Operation
> {
>     CREATE, READ, UPDATE, DELETE, ADMINISTER
> }
>
> I wanted to point this out, but I agree that for most cases is 
> type-safe approach with Operation interface probably better:-)
>
> So after all, my opinion is to have two methods:
>
> Identity.hasPermission(Object object, Operation operation) throws 
> AuthorizationException;
> Identity.hasPermission(ResourceIdentifier resourceIdentifier, 
> Operation operation) throws AuthorizationException;

+1, I think that having two methods is the best solution, with the 
caveat that overloading like this doesn't cause issues with EL.  The 
Object parameter version of this method will most likely delegate to the 
ResourceIdentifier method anyway, as a ResourceIdentifier will be 
required for all lookups.  By the way, we should make this as 
transparent as possible - out of the box, DeltaSpike should provide a 
ResourceIdentifier implementation for entity beans that will build a 
unique identifier based on the class name and the primary key value.  
Another alternative that we should support is allowing the user to 
specify which ResourceIdentifier to use on the domain class itself:

@Entity
@Identifier(CustomerResourceIdentifier.class)
public class Customer
{

}

Yet another alternative that we should support is the packaging of 
additional ResourceIdentifier implementations with an application, that 
are automatically discovered and iterated through when the permission 
check can't determine which ResourceIdentifier to use.  With that in 
mind, the ResourceIdentifier interface should look like this:

public interface ResourceIdentifier
{
     boolean canIdentify(Class targetClass);
     String getIdentifier(Object resource);
}

The canIdentify() method will return true if the specified class is one 
that this ResourceIdentifier is capable of generating identifier strings 
for.

>
>
> Thanks,
> Marek


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Marek Posolda <mp...@redhat.com>.
Hi all,


On 22.4.2012 19:27, Christian Kaltepoth wrote:
> I prefer the method signature Shane suggested in his initial mail.
> Providing a single method with an object parameter makes most sense to
> me:
>
>    Identity.hasPermission(Object resource, String operation)
>
> This way the developer is completely free to choose how to identify
> the resource. I think in most cases the object itself will be used.
> And the JSF example Shane showed is a very good example for this.
>
> Of cause people may need other ways to refer to resources like in the
> ResourceIdentifier concept Marek showed. But actually I see something
> like this more as an usage pattern and I don't think it belongs
> directly into the API. Perhaps we could provide something like
> ResourceIdentifier as an utility class that people can use if they
> want to refer to resources this way. But the API of Identity should be
> as simple as possible. And the object parameter will allow people to
> use any object they want. Even something like ResourceIdentifier.
That's the question. I think that API should be clear and using single 
method:

Identity.hasPermission(Object resource, String operation)


is not clear enough. As in some cases, your argument is directly secured 
object and in other cases it's only ResourceIdentifier for this object. 
Also it would mean that you will need to ask for the type of object in 
your PermissionResolver implementation:

if (resource instanceof ResourceIdentifier)
{
   // compute permissions for ResourceIdentifier
}
else
{
   // Argument is "resource" so compute permissions for this resource
}




On the other hand, using single method:
Identity.hasPermission(ResourceIdentifier resource, Operation operation)

may not be appropriate enough for some use-cases. Let's imagine that you 
have Customer and CustomerResourceIdntifier objects:

public class Customer
{
    private String ID;
    private String customerName;

    // getters and setters here
}

public class CustomerResourceIdentifier implements ResourceIdentifier
{
      private String customerId;

      // getters and setters
}


And you want to create PermissionResolver, which will compute permission 
based on customer name. With the API method:

Identity.hasPermission(ResourceIdentifier resourceIdentifier, Operation operation)


you can't manage to do it (unless you change implementation of 
CustomerResourceIdentifier to encapsulate customerName)


> Operation
I agree that big advantage of this approach is type-safety. On the other 
hand, using String directly is more flexible as you have more freedom. 
Let's imagine that you have some permission rules stored in database and 
you want to add new type of operation like "ADMINISTER". With using 
String as argument, you can directly create records in DB and use this 
new "ADMINISTER" argument without doing any changes in Java code. With 
the Operation approach, you would need to add this new ADMINISTER 
operation into your Operation enum:

public enum CrudOperation implements Operation
{
     CREATE, READ, UPDATE, DELETE, ADMINISTER
}

I wanted to point this out, but I agree that for most cases is type-safe 
approach with Operation interface probably better:-)

So after all, my opinion is to have two methods:

Identity.hasPermission(Object object, Operation operation) throws 
AuthorizationException;
Identity.hasPermission(ResourceIdentifier resourceIdentifier, Operation 
operation) throws AuthorizationException;


Thanks,
Marek

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi christian,

yes

>if< you don't have a well defined set of such resources in an application,
you can still use:

public class ObjectResourceIdentifier implements ResourceIdentifier
{
    private final String identifier;

    public ObjectResourceIdentifier(Object o)
    {
        this.identifier = calculateIdentifier(o);
    }

    //...

    @Override
    public String toString()
    {
        return this.identifier;
    }
}

or we can think about #getResource (instead of using #toString).

>but< i would only suggest it as a last resort. instead i would define such
resource-types explicitly for the application (but it's still possible to
do it the generic way).
(i would prefer ResourceDescriptor - but that's just a detail)

regards,
gerhard



2012/4/22 Christian Kaltepoth <ch...@kaltepoth.de>

> Hey Gerhard,
>
> sorry, I missed your mail because we wrote to the list nearly at the
> same time. :)
>
> I like your idea of a more type-safe way to specify the operation. I
> think using an enum with an marker interface will work out fine. This
> is much nicer then having a String to identify the operation. Perhaps
> we could provide a default implementation for Operation so that people
> can use this one for simple cases. This would simplify it for users
> who want to get started quickly.
>
> But what about the resource? Do you want to use an interface here too?
>
> Christian
>
>
> 2012/4/22 Gerhard Petracek <ge...@gmail.com>:
> > hi christian,
> >
> > you can do the same (but type-safe) with my suggestion.
> >
> > regards,
> > gerhard
> >
> >
> >
> > 2012/4/22 Christian Kaltepoth <ch...@kaltepoth.de>
> >
> >> I prefer the method signature Shane suggested in his initial mail.
> >> Providing a single method with an object parameter makes most sense to
> >> me:
> >>
> >>  Identity.hasPermission(Object resource, String operation)
> >>
> >> This way the developer is completely free to choose how to identify
> >> the resource. I think in most cases the object itself will be used.
> >> And the JSF example Shane showed is a very good example for this.
> >>
> >> Of cause people may need other ways to refer to resources like in the
> >> ResourceIdentifier concept Marek showed. But actually I see something
> >> like this more as an usage pattern and I don't think it belongs
> >> directly into the API. Perhaps we could provide something like
> >> ResourceIdentifier as an utility class that people can use if they
> >> want to refer to resources this way. But the API of Identity should be
> >> as simple as possible. And the object parameter will allow people to
> >> use any object they want. Even something like ResourceIdentifier.
> >>
> >> Christian
> >>
> >>
> >> 2012/4/19 Marek Posolda <mp...@redhat.com>:
> >> > On 19.4.2012 13:56, Shane Bryzak wrote:
> >> >>
> >> >> On 19/04/12 20:15, Marek Posolda wrote:
> >> >>>
> >> >>> On 19.4.2012 00:56, Shane Bryzak wrote:
> >> >>>>
> >> >>>> On 19/04/12 08:03, Marek Posolda wrote:
> >> >>>>>
> >> >>>>> Hi all,
> >> >>>>>
> >> >>>>> I am Marek Posolda and i am working on GateIn portal project
> together
> >> >>>>> wth Bolek Dawidowicz. Some of my notes inline:
> >> >>>>>
> >> >>>>> On 18.4.2012 00:52, Shane Bryzak wrote:
> >> >>>>>>
> >> >>>>>> I'd like to kick off a discussion around the Authorization API,
> >> >>>>>> specifically object permissions.  This API is used to determine
> >> whether the
> >> >>>>>> currently authenticated user has the necessary privileges to
> >> perform an
> >> >>>>>> operation on a particular domain object.  I'll start by
> outlining my
> >> >>>>>> proposal covering the simple use cases of this API and we can
> then
> >> proceed
> >> >>>>>> from there.
> >> >>>>>>
> >> >>>>>> First of all, the developer needs a gateway into the permission
> API
> >> so
> >> >>>>>> that they can perform permission checks within their own
> >> application.  This
> >> >>>>>> is provided by the hasPermission() method:
> >> >>>>>>
> >> >>>>>>
> >> >>>>>> Identity.hasPermission(Object resource, String operation)
> >> >>>>>
> >> >>>>> How about having additional method like:
> >> >>>>>
> >> >>>>>
> >> >>>>> Identity.hasPermission(String resourceType, Serializable
> resourceId,
> >> >>>>> String operation);
> >> >>>>>
> >> >>>>> This have one advantage that you don't need to obtain instance of
> >> >>>>> Object from DB when you have only ID of this resource. Use case
> >> example can
> >> >>>>> be:
> >> >>>>>
> >> >>>>> public Customer loadCustomer(Long customerId)
> >> >>>>> {
> >> >>>>>  if (!identity.hasPermission("org.myorganization.Customer",
> >> customerId,
> >> >>>>> "READ")
> >> >>>>>  {
> >> >>>>>    throw new AuthorizationException("Insufficient privileges to
> load
> >> >>>>> this customer");
> >> >>>>>  }
> >> >>>>>
> >> >>>>>  // snip code
> >> >>>>> }
> >> >>>>>
> >> >>>>
> >> >>>> This is a good use case, I'm wondering though if we could still
> wrap
> >> >>>> this up as a resource object to keep the API clean.  Perhaps
> >> something like
> >> >>>> this:
> >> >>>>
> >> >>>> public Customer loadCustomer(Long customerId)
> >> >>>> {
> >> >>>>  if (!identity.hasPermission(new
> >> >>>> ResourceIdentifier("org.myorganization.Customer", customerId),
> "READ")
> >> >>>>  {
> >> >>>>    throw new AuthorizationException("Insufficient privileges to
> load
> >> >>>> this customer");
> >> >>>>  }
> >> >>>>
> >> >>>>  // snip code
> >> >>>> }
> >> >>>
> >> >>> Yes, that should work too. Question is if we still have two methods
> >> then
> >> >>> or only single method with signature:
> >> >>> Identity.hasPermission(ResourceIdentifier identifier, String action)
> >> throws
> >> >>> AuthorizationException;  ?
> >> >>>
> >> >>> Seems that one method may be sufficient as long as you can always
> >> obtain
> >> >>> ResourceIdentifier for particular object when you have Type and ID
> of
> >> this
> >> >>> object. So your edit method can then looks like:
> >> >>>
> >> >>> @Inject Identity identity;
> >> >>>
> >> >>> public void editCustomer(Customer customer)
> >> >>> {
> >> >>>    if (!identity.hasPermission(new
> >> >>> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
> >> >>>    {
> >> >>>        throw new AuthorizationException("Insufficient privileges to
> >> edit
> >> >>> this customer");
> >> >>>    }
> >> >>>
> >> >>>    // snip code
> >> >>> }
> >> >>>
> >> >>>
> >> >>> Marek
> >> >>
> >> >>
> >> >>
> >> >> Except that this makes it harder to consume - for example, think of a
> >> JSF
> >> >> page with a table of records.  For each row in the table, you may
> wish
> >> to
> >> >> display action buttons etc depending on the privileges that the user
> >> has for
> >> >> that row.
> >> >>
> >> >> <h:dataTable value="#{customerSearch.searchResults}" var="customer">
> >> >> <h:column>
> >> >> <h:outputText value="#{customer.name}"/>
> >> >> </h:column>
> >> >> <h:column>
> >> >> <ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
> >> >> <!-- action buttons go here -->
> >> >> </ui:fragment>
> >> >> </h:column>
> >> >> </h:dataTable>
> >> >>
> >> >> This wouldn't be possibly if Identity.hasPermission() required a
> >> >> ResourceIdentifier parameter.  I think an Object parameter is going
> to
> >> be
> >> >> the most versatile for our requirements, and we can just have special
> >> >> handling when the resource type is a ResourceIdentifier.
> >> >
> >> > True. So having two methods:
> >> > - Identity.hasPermission(Object resource, String action) throws
> >> > AuthorizationException;
> >> > - Identity.hasPermission(ResourceIdentifier resourceIdentifier, String
> >> > action) throws AuthorizationException;
> >> >
> >> > should work better then.
> >> >
> >> >
> >> >>
> >> >>>
> >> >>>
> >> >>>>
> >> >>>>
> >> >>>>
> >> >>>>>>
> >> >>>>>>
> >> >>>>>> A permission has three aspects; 1) The application resource for
> >> which
> >> >>>>>> the permission is granted, 2) The operation that has been granted
> >> for that
> >> >>>>>> resource, and 3) The recipient of the permission, which may be
> >> either a
> >> >>>>>> User, Group or Role.
> >> >>>>>>
> >> >>>>>> For example, if we wish to check whether the user has permission
> to
> >> >>>>>> edit a Customer instance the code might look like this:
> >> >>>>>>
> >> >>>>>> @Inject Identity identity;
> >> >>>>>>
> >> >>>>>> public void editCustomer(Customer customer)
> >> >>>>>> {
> >> >>>>>>    if (!identity.hasPermission(customer, "EDIT"))
> >> >>>>>>    {
> >> >>>>>>        throw new AuthorizationException("Insufficient privileges
> to
> >> >>>>>> edit this customer");
> >> >>>>>>    }
> >> >>>>>>
> >> >>>>>>    // snip code
> >> >>>>>> }
> >> >>>>>>
> >> >>>>>> We could potentially also do some clever stuff with method-level
> >> >>>>>> annotations here.  Off the top of my head, something like this
> >> might work:
> >> >>>>>>
> >> >>>>>> @CheckPermissions
> >> >>>>>> public void editCustomer(@CheckPermission("EDIT") Customer
> customer)
> >> >>>>>>
> >> >>>>>> The @CheckPermissions annotation would be a security binding type
> >> with
> >> >>>>>> a matching authorizer that would scan the parameter list and
> >> perform any
> >> >>>>>> checks on parameters annotated with @CheckPermission.  Anyway we
> >> can refine
> >> >>>>>> this idea in ongoing discussions.
> >> >>>>>>
> >> >>>>>> In the default implementation, the Identity.hasPermission()
> method
> >> >>>>>> essentially contains no code, instead it delegates the permission
> >> check to
> >> >>>>>> the PermissionMapper bean, an implementation-only bean which
> >> contains a list
> >> >>>>>> of PermissionResolver instances that are used to perform the
> >> permission
> >> >>>>>> check.
> >> >>>>>>
> >> >>>>>> public class DefaultIdentity implements Identity
> >> >>>>>> {
> >> >>>>>>    // snip code
> >> >>>>>>
> >> >>>>>>    public boolean hasPermission(Object resource, String
> operation)
> >> >>>>>>    {
> >> >>>>>>        return permissionMapper.resolvePermission(resource,
> >> operation);
> >> >>>>>>    }
> >> >>>>>> }
> >> >>>>>>
> >> >>>>>> The PermissionMapper bean provides the resolvePermission()
> method,
> >> >>>>>> which basically iterates through the known PermissionResolvers,
> and
> >> if one
> >> >>>>>> of them returns true for the permission check, then a true
> result is
> >> >>>>>> returned.
> >> >>>>>>
> >> >>>>>> public class PermissionMapper
> >> >>>>>> {
> >> >>>>>>    @Inject Instance<PermissionResolver> resolvers;
> >> >>>>>>
> >> >>>>>>    public boolean resolvePermission(Object resource, String
> >> operation)
> >> >>>>>>    {
> >> >>>>>>        for (PermissionResolver resolver : resolvers)
> >> >>>>>>        {
> >> >>>>>>            if (resolver.hasPermission(resource, operation))
> return
> >> >>>>>> true;
> >> >>>>>>        }
> >> >>>>>>        return false;
> >> >>>>>>    }
> >> >>>>>> }
> >> >>>>>>
> >> >>>>>> We can do some clever stuff here, like caching which permission
> >> >>>>>> resolver returns a true result for a particular class of
> resource,
> >> and then
> >> >>>>>> always using that resolver for that class, etc.
> >> >>>>>>
> >> >>>>>> PermissionResolver is an API interface, the implementations of
> which
> >> >>>>>> do the actual work of checking the permission.
> >> >>>>>>
> >> >>>>>> public interface PermissionResolver
> >> >>>>>> {
> >> >>>>>>    boolean hasPermission(Object resource, String operation);
> >> >>>>>> }
> >> >>>>>
> >> >>>>> I think that it might be useful if PermissionResolver can also
> deny
> >> >>>>> permissions? So it may look like:
> >> >>>>>
> >> >>>>> public interface PermissionResolver
> >> >>>>> {
> >> >>>>>   PermissionType hasPermission(Object resource, String operation);
> >> >>>>> }
> >> >>>>>
> >> >>>>> where PermissionType can be:
> >> >>>>>
> >> >>>>> public enum PermissionType
> >> >>>>> {
> >> >>>>>   PERMIT, DENY, NOT_APPLICABLE
> >> >>>>> }
> >> >>>>>
> >> >>>>> and PermissionMapper implementation can look like:
> >> >>>>>
> >> >>>>> public class PermissionMapper
> >> >>>>> {
> >> >>>>>    @Inject Instance<PermissionResolver> resolvers;
> >> >>>>>
> >> >>>>>    public boolean resolvePermission(Object resource, String
> >> operation)
> >> >>>>>    {
> >> >>>>>        for (PermissionResolver resolver : resolvers)
> >> >>>>>        {
> >> >>>>>            PermissionType permissionType =
> >> >>>>> resolver.hasPermission(resource, operation);
> >> >>>>>            if (permissionType == PERMIT)
> >> >>>>>            {
> >> >>>>>              return true;
> >> >>>>>            }
> >> >>>>>            else if (permissionType == DENY)
> >> >>>>>            {
> >> >>>>>              return false;
> >> >>>>>            }
> >> >>>>>        }
> >> >>>>>
> >> >>>>>        return false;
> >> >>>>>    }
> >> >>>>> }
> >> >>>>>
> >> >>>>> And order of PermissionResolvers inside PermissionMapper can be
> >> ordered
> >> >>>>> according to some priorities. This design may be more flexible.
> For
> >> example,
> >> >>>>> it will allow you to quickly or temporarily revoke all permissions
> >> for
> >> >>>>> particular user.
> >> >>>>
> >> >>>>
> >> >>>> This is also a good use case.  We would need to iterate through all
> >> >>>> PermissionResolvers though and if any of them returned a DENY then
> our
> >> >>>> permission check would return a final result of false.
> >> >>>>
> >> >>>>>
> >> >>>>>>
> >> >>>>>> We would provide one PermissionResolver implementation out of the
> >> box
> >> >>>>>> with DeltaSpike; PersistentPermissionResolver would provide
> >> permission
> >> >>>>>> checks for ACL-style object permissions and provide a key piece
> of
> >> >>>>>> functionality required by a complete permission management API.
> >>  Developers
> >> >>>>>> can easily provide their own PermissionResolver implementations
> >> with custom
> >> >>>>>> business logic by simply deploying a PermissionResolver bean in
> >> their own
> >> >>>>>> application.
> >> >>>>>>
> >> >>>>>> This pretty much covers the basics of the object permission API,
> at
> >> >>>>>> least on the consuming side.  We can discuss this area first
> before
> >> moving
> >> >>>>>> onto the permission management API shortly.
> >> >>>>>>
> >> >>>>>>
> >> >>>>>
> >> >>>>
> >> >>>>
> >> >>>
> >> >>
> >> >>
> >> >
> >>
> >>
> >>
> >> --
> >> Christian Kaltepoth
> >> Blog: http://chkal.blogspot.com/
> >> Twitter: http://twitter.com/chkal
> >>
>
>
>
> --
> Christian Kaltepoth
> Blog: http://chkal.blogspot.com/
> Twitter: http://twitter.com/chkal
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Christian Kaltepoth <ch...@kaltepoth.de>.
Hey Gerhard,

sorry, I missed your mail because we wrote to the list nearly at the
same time. :)

I like your idea of a more type-safe way to specify the operation. I
think using an enum with an marker interface will work out fine. This
is much nicer then having a String to identify the operation. Perhaps
we could provide a default implementation for Operation so that people
can use this one for simple cases. This would simplify it for users
who want to get started quickly.

But what about the resource? Do you want to use an interface here too?

Christian


2012/4/22 Gerhard Petracek <ge...@gmail.com>:
> hi christian,
>
> you can do the same (but type-safe) with my suggestion.
>
> regards,
> gerhard
>
>
>
> 2012/4/22 Christian Kaltepoth <ch...@kaltepoth.de>
>
>> I prefer the method signature Shane suggested in his initial mail.
>> Providing a single method with an object parameter makes most sense to
>> me:
>>
>>  Identity.hasPermission(Object resource, String operation)
>>
>> This way the developer is completely free to choose how to identify
>> the resource. I think in most cases the object itself will be used.
>> And the JSF example Shane showed is a very good example for this.
>>
>> Of cause people may need other ways to refer to resources like in the
>> ResourceIdentifier concept Marek showed. But actually I see something
>> like this more as an usage pattern and I don't think it belongs
>> directly into the API. Perhaps we could provide something like
>> ResourceIdentifier as an utility class that people can use if they
>> want to refer to resources this way. But the API of Identity should be
>> as simple as possible. And the object parameter will allow people to
>> use any object they want. Even something like ResourceIdentifier.
>>
>> Christian
>>
>>
>> 2012/4/19 Marek Posolda <mp...@redhat.com>:
>> > On 19.4.2012 13:56, Shane Bryzak wrote:
>> >>
>> >> On 19/04/12 20:15, Marek Posolda wrote:
>> >>>
>> >>> On 19.4.2012 00:56, Shane Bryzak wrote:
>> >>>>
>> >>>> On 19/04/12 08:03, Marek Posolda wrote:
>> >>>>>
>> >>>>> Hi all,
>> >>>>>
>> >>>>> I am Marek Posolda and i am working on GateIn portal project together
>> >>>>> wth Bolek Dawidowicz. Some of my notes inline:
>> >>>>>
>> >>>>> On 18.4.2012 00:52, Shane Bryzak wrote:
>> >>>>>>
>> >>>>>> I'd like to kick off a discussion around the Authorization API,
>> >>>>>> specifically object permissions.  This API is used to determine
>> whether the
>> >>>>>> currently authenticated user has the necessary privileges to
>> perform an
>> >>>>>> operation on a particular domain object.  I'll start by outlining my
>> >>>>>> proposal covering the simple use cases of this API and we can then
>> proceed
>> >>>>>> from there.
>> >>>>>>
>> >>>>>> First of all, the developer needs a gateway into the permission API
>> so
>> >>>>>> that they can perform permission checks within their own
>> application.  This
>> >>>>>> is provided by the hasPermission() method:
>> >>>>>>
>> >>>>>>
>> >>>>>> Identity.hasPermission(Object resource, String operation)
>> >>>>>
>> >>>>> How about having additional method like:
>> >>>>>
>> >>>>>
>> >>>>> Identity.hasPermission(String resourceType, Serializable resourceId,
>> >>>>> String operation);
>> >>>>>
>> >>>>> This have one advantage that you don't need to obtain instance of
>> >>>>> Object from DB when you have only ID of this resource. Use case
>> example can
>> >>>>> be:
>> >>>>>
>> >>>>> public Customer loadCustomer(Long customerId)
>> >>>>> {
>> >>>>>  if (!identity.hasPermission("org.myorganization.Customer",
>> customerId,
>> >>>>> "READ")
>> >>>>>  {
>> >>>>>    throw new AuthorizationException("Insufficient privileges to load
>> >>>>> this customer");
>> >>>>>  }
>> >>>>>
>> >>>>>  // snip code
>> >>>>> }
>> >>>>>
>> >>>>
>> >>>> This is a good use case, I'm wondering though if we could still wrap
>> >>>> this up as a resource object to keep the API clean.  Perhaps
>> something like
>> >>>> this:
>> >>>>
>> >>>> public Customer loadCustomer(Long customerId)
>> >>>> {
>> >>>>  if (!identity.hasPermission(new
>> >>>> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
>> >>>>  {
>> >>>>    throw new AuthorizationException("Insufficient privileges to load
>> >>>> this customer");
>> >>>>  }
>> >>>>
>> >>>>  // snip code
>> >>>> }
>> >>>
>> >>> Yes, that should work too. Question is if we still have two methods
>> then
>> >>> or only single method with signature:
>> >>> Identity.hasPermission(ResourceIdentifier identifier, String action)
>> throws
>> >>> AuthorizationException;  ?
>> >>>
>> >>> Seems that one method may be sufficient as long as you can always
>> obtain
>> >>> ResourceIdentifier for particular object when you have Type and ID of
>> this
>> >>> object. So your edit method can then looks like:
>> >>>
>> >>> @Inject Identity identity;
>> >>>
>> >>> public void editCustomer(Customer customer)
>> >>> {
>> >>>    if (!identity.hasPermission(new
>> >>> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
>> >>>    {
>> >>>        throw new AuthorizationException("Insufficient privileges to
>> edit
>> >>> this customer");
>> >>>    }
>> >>>
>> >>>    // snip code
>> >>> }
>> >>>
>> >>>
>> >>> Marek
>> >>
>> >>
>> >>
>> >> Except that this makes it harder to consume - for example, think of a
>> JSF
>> >> page with a table of records.  For each row in the table, you may wish
>> to
>> >> display action buttons etc depending on the privileges that the user
>> has for
>> >> that row.
>> >>
>> >> <h:dataTable value="#{customerSearch.searchResults}" var="customer">
>> >> <h:column>
>> >> <h:outputText value="#{customer.name}"/>
>> >> </h:column>
>> >> <h:column>
>> >> <ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
>> >> <!-- action buttons go here -->
>> >> </ui:fragment>
>> >> </h:column>
>> >> </h:dataTable>
>> >>
>> >> This wouldn't be possibly if Identity.hasPermission() required a
>> >> ResourceIdentifier parameter.  I think an Object parameter is going to
>> be
>> >> the most versatile for our requirements, and we can just have special
>> >> handling when the resource type is a ResourceIdentifier.
>> >
>> > True. So having two methods:
>> > - Identity.hasPermission(Object resource, String action) throws
>> > AuthorizationException;
>> > - Identity.hasPermission(ResourceIdentifier resourceIdentifier, String
>> > action) throws AuthorizationException;
>> >
>> > should work better then.
>> >
>> >
>> >>
>> >>>
>> >>>
>> >>>>
>> >>>>
>> >>>>
>> >>>>>>
>> >>>>>>
>> >>>>>> A permission has three aspects; 1) The application resource for
>> which
>> >>>>>> the permission is granted, 2) The operation that has been granted
>> for that
>> >>>>>> resource, and 3) The recipient of the permission, which may be
>> either a
>> >>>>>> User, Group or Role.
>> >>>>>>
>> >>>>>> For example, if we wish to check whether the user has permission to
>> >>>>>> edit a Customer instance the code might look like this:
>> >>>>>>
>> >>>>>> @Inject Identity identity;
>> >>>>>>
>> >>>>>> public void editCustomer(Customer customer)
>> >>>>>> {
>> >>>>>>    if (!identity.hasPermission(customer, "EDIT"))
>> >>>>>>    {
>> >>>>>>        throw new AuthorizationException("Insufficient privileges to
>> >>>>>> edit this customer");
>> >>>>>>    }
>> >>>>>>
>> >>>>>>    // snip code
>> >>>>>> }
>> >>>>>>
>> >>>>>> We could potentially also do some clever stuff with method-level
>> >>>>>> annotations here.  Off the top of my head, something like this
>> might work:
>> >>>>>>
>> >>>>>> @CheckPermissions
>> >>>>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>> >>>>>>
>> >>>>>> The @CheckPermissions annotation would be a security binding type
>> with
>> >>>>>> a matching authorizer that would scan the parameter list and
>> perform any
>> >>>>>> checks on parameters annotated with @CheckPermission.  Anyway we
>> can refine
>> >>>>>> this idea in ongoing discussions.
>> >>>>>>
>> >>>>>> In the default implementation, the Identity.hasPermission() method
>> >>>>>> essentially contains no code, instead it delegates the permission
>> check to
>> >>>>>> the PermissionMapper bean, an implementation-only bean which
>> contains a list
>> >>>>>> of PermissionResolver instances that are used to perform the
>> permission
>> >>>>>> check.
>> >>>>>>
>> >>>>>> public class DefaultIdentity implements Identity
>> >>>>>> {
>> >>>>>>    // snip code
>> >>>>>>
>> >>>>>>    public boolean hasPermission(Object resource, String operation)
>> >>>>>>    {
>> >>>>>>        return permissionMapper.resolvePermission(resource,
>> operation);
>> >>>>>>    }
>> >>>>>> }
>> >>>>>>
>> >>>>>> The PermissionMapper bean provides the resolvePermission() method,
>> >>>>>> which basically iterates through the known PermissionResolvers, and
>> if one
>> >>>>>> of them returns true for the permission check, then a true result is
>> >>>>>> returned.
>> >>>>>>
>> >>>>>> public class PermissionMapper
>> >>>>>> {
>> >>>>>>    @Inject Instance<PermissionResolver> resolvers;
>> >>>>>>
>> >>>>>>    public boolean resolvePermission(Object resource, String
>> operation)
>> >>>>>>    {
>> >>>>>>        for (PermissionResolver resolver : resolvers)
>> >>>>>>        {
>> >>>>>>            if (resolver.hasPermission(resource, operation)) return
>> >>>>>> true;
>> >>>>>>        }
>> >>>>>>        return false;
>> >>>>>>    }
>> >>>>>> }
>> >>>>>>
>> >>>>>> We can do some clever stuff here, like caching which permission
>> >>>>>> resolver returns a true result for a particular class of resource,
>> and then
>> >>>>>> always using that resolver for that class, etc.
>> >>>>>>
>> >>>>>> PermissionResolver is an API interface, the implementations of which
>> >>>>>> do the actual work of checking the permission.
>> >>>>>>
>> >>>>>> public interface PermissionResolver
>> >>>>>> {
>> >>>>>>    boolean hasPermission(Object resource, String operation);
>> >>>>>> }
>> >>>>>
>> >>>>> I think that it might be useful if PermissionResolver can also deny
>> >>>>> permissions? So it may look like:
>> >>>>>
>> >>>>> public interface PermissionResolver
>> >>>>> {
>> >>>>>   PermissionType hasPermission(Object resource, String operation);
>> >>>>> }
>> >>>>>
>> >>>>> where PermissionType can be:
>> >>>>>
>> >>>>> public enum PermissionType
>> >>>>> {
>> >>>>>   PERMIT, DENY, NOT_APPLICABLE
>> >>>>> }
>> >>>>>
>> >>>>> and PermissionMapper implementation can look like:
>> >>>>>
>> >>>>> public class PermissionMapper
>> >>>>> {
>> >>>>>    @Inject Instance<PermissionResolver> resolvers;
>> >>>>>
>> >>>>>    public boolean resolvePermission(Object resource, String
>> operation)
>> >>>>>    {
>> >>>>>        for (PermissionResolver resolver : resolvers)
>> >>>>>        {
>> >>>>>            PermissionType permissionType =
>> >>>>> resolver.hasPermission(resource, operation);
>> >>>>>            if (permissionType == PERMIT)
>> >>>>>            {
>> >>>>>              return true;
>> >>>>>            }
>> >>>>>            else if (permissionType == DENY)
>> >>>>>            {
>> >>>>>              return false;
>> >>>>>            }
>> >>>>>        }
>> >>>>>
>> >>>>>        return false;
>> >>>>>    }
>> >>>>> }
>> >>>>>
>> >>>>> And order of PermissionResolvers inside PermissionMapper can be
>> ordered
>> >>>>> according to some priorities. This design may be more flexible. For
>> example,
>> >>>>> it will allow you to quickly or temporarily revoke all permissions
>> for
>> >>>>> particular user.
>> >>>>
>> >>>>
>> >>>> This is also a good use case.  We would need to iterate through all
>> >>>> PermissionResolvers though and if any of them returned a DENY then our
>> >>>> permission check would return a final result of false.
>> >>>>
>> >>>>>
>> >>>>>>
>> >>>>>> We would provide one PermissionResolver implementation out of the
>> box
>> >>>>>> with DeltaSpike; PersistentPermissionResolver would provide
>> permission
>> >>>>>> checks for ACL-style object permissions and provide a key piece of
>> >>>>>> functionality required by a complete permission management API.
>>  Developers
>> >>>>>> can easily provide their own PermissionResolver implementations
>> with custom
>> >>>>>> business logic by simply deploying a PermissionResolver bean in
>> their own
>> >>>>>> application.
>> >>>>>>
>> >>>>>> This pretty much covers the basics of the object permission API, at
>> >>>>>> least on the consuming side.  We can discuss this area first before
>> moving
>> >>>>>> onto the permission management API shortly.
>> >>>>>>
>> >>>>>>
>> >>>>>
>> >>>>
>> >>>>
>> >>>
>> >>
>> >>
>> >
>>
>>
>>
>> --
>> Christian Kaltepoth
>> Blog: http://chkal.blogspot.com/
>> Twitter: http://twitter.com/chkal
>>



-- 
Christian Kaltepoth
Blog: http://chkal.blogspot.com/
Twitter: http://twitter.com/chkal

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Gerhard Petracek <ge...@gmail.com>.
hi christian,

you can do the same (but type-safe) with my suggestion.

regards,
gerhard



2012/4/22 Christian Kaltepoth <ch...@kaltepoth.de>

> I prefer the method signature Shane suggested in his initial mail.
> Providing a single method with an object parameter makes most sense to
> me:
>
>  Identity.hasPermission(Object resource, String operation)
>
> This way the developer is completely free to choose how to identify
> the resource. I think in most cases the object itself will be used.
> And the JSF example Shane showed is a very good example for this.
>
> Of cause people may need other ways to refer to resources like in the
> ResourceIdentifier concept Marek showed. But actually I see something
> like this more as an usage pattern and I don't think it belongs
> directly into the API. Perhaps we could provide something like
> ResourceIdentifier as an utility class that people can use if they
> want to refer to resources this way. But the API of Identity should be
> as simple as possible. And the object parameter will allow people to
> use any object they want. Even something like ResourceIdentifier.
>
> Christian
>
>
> 2012/4/19 Marek Posolda <mp...@redhat.com>:
> > On 19.4.2012 13:56, Shane Bryzak wrote:
> >>
> >> On 19/04/12 20:15, Marek Posolda wrote:
> >>>
> >>> On 19.4.2012 00:56, Shane Bryzak wrote:
> >>>>
> >>>> On 19/04/12 08:03, Marek Posolda wrote:
> >>>>>
> >>>>> Hi all,
> >>>>>
> >>>>> I am Marek Posolda and i am working on GateIn portal project together
> >>>>> wth Bolek Dawidowicz. Some of my notes inline:
> >>>>>
> >>>>> On 18.4.2012 00:52, Shane Bryzak wrote:
> >>>>>>
> >>>>>> I'd like to kick off a discussion around the Authorization API,
> >>>>>> specifically object permissions.  This API is used to determine
> whether the
> >>>>>> currently authenticated user has the necessary privileges to
> perform an
> >>>>>> operation on a particular domain object.  I'll start by outlining my
> >>>>>> proposal covering the simple use cases of this API and we can then
> proceed
> >>>>>> from there.
> >>>>>>
> >>>>>> First of all, the developer needs a gateway into the permission API
> so
> >>>>>> that they can perform permission checks within their own
> application.  This
> >>>>>> is provided by the hasPermission() method:
> >>>>>>
> >>>>>>
> >>>>>> Identity.hasPermission(Object resource, String operation)
> >>>>>
> >>>>> How about having additional method like:
> >>>>>
> >>>>>
> >>>>> Identity.hasPermission(String resourceType, Serializable resourceId,
> >>>>> String operation);
> >>>>>
> >>>>> This have one advantage that you don't need to obtain instance of
> >>>>> Object from DB when you have only ID of this resource. Use case
> example can
> >>>>> be:
> >>>>>
> >>>>> public Customer loadCustomer(Long customerId)
> >>>>> {
> >>>>>  if (!identity.hasPermission("org.myorganization.Customer",
> customerId,
> >>>>> "READ")
> >>>>>  {
> >>>>>    throw new AuthorizationException("Insufficient privileges to load
> >>>>> this customer");
> >>>>>  }
> >>>>>
> >>>>>  // snip code
> >>>>> }
> >>>>>
> >>>>
> >>>> This is a good use case, I'm wondering though if we could still wrap
> >>>> this up as a resource object to keep the API clean.  Perhaps
> something like
> >>>> this:
> >>>>
> >>>> public Customer loadCustomer(Long customerId)
> >>>> {
> >>>>  if (!identity.hasPermission(new
> >>>> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
> >>>>  {
> >>>>    throw new AuthorizationException("Insufficient privileges to load
> >>>> this customer");
> >>>>  }
> >>>>
> >>>>  // snip code
> >>>> }
> >>>
> >>> Yes, that should work too. Question is if we still have two methods
> then
> >>> or only single method with signature:
> >>> Identity.hasPermission(ResourceIdentifier identifier, String action)
> throws
> >>> AuthorizationException;  ?
> >>>
> >>> Seems that one method may be sufficient as long as you can always
> obtain
> >>> ResourceIdentifier for particular object when you have Type and ID of
> this
> >>> object. So your edit method can then looks like:
> >>>
> >>> @Inject Identity identity;
> >>>
> >>> public void editCustomer(Customer customer)
> >>> {
> >>>    if (!identity.hasPermission(new
> >>> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
> >>>    {
> >>>        throw new AuthorizationException("Insufficient privileges to
> edit
> >>> this customer");
> >>>    }
> >>>
> >>>    // snip code
> >>> }
> >>>
> >>>
> >>> Marek
> >>
> >>
> >>
> >> Except that this makes it harder to consume - for example, think of a
> JSF
> >> page with a table of records.  For each row in the table, you may wish
> to
> >> display action buttons etc depending on the privileges that the user
> has for
> >> that row.
> >>
> >> <h:dataTable value="#{customerSearch.searchResults}" var="customer">
> >> <h:column>
> >> <h:outputText value="#{customer.name}"/>
> >> </h:column>
> >> <h:column>
> >> <ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
> >> <!-- action buttons go here -->
> >> </ui:fragment>
> >> </h:column>
> >> </h:dataTable>
> >>
> >> This wouldn't be possibly if Identity.hasPermission() required a
> >> ResourceIdentifier parameter.  I think an Object parameter is going to
> be
> >> the most versatile for our requirements, and we can just have special
> >> handling when the resource type is a ResourceIdentifier.
> >
> > True. So having two methods:
> > - Identity.hasPermission(Object resource, String action) throws
> > AuthorizationException;
> > - Identity.hasPermission(ResourceIdentifier resourceIdentifier, String
> > action) throws AuthorizationException;
> >
> > should work better then.
> >
> >
> >>
> >>>
> >>>
> >>>>
> >>>>
> >>>>
> >>>>>>
> >>>>>>
> >>>>>> A permission has three aspects; 1) The application resource for
> which
> >>>>>> the permission is granted, 2) The operation that has been granted
> for that
> >>>>>> resource, and 3) The recipient of the permission, which may be
> either a
> >>>>>> User, Group or Role.
> >>>>>>
> >>>>>> For example, if we wish to check whether the user has permission to
> >>>>>> edit a Customer instance the code might look like this:
> >>>>>>
> >>>>>> @Inject Identity identity;
> >>>>>>
> >>>>>> public void editCustomer(Customer customer)
> >>>>>> {
> >>>>>>    if (!identity.hasPermission(customer, "EDIT"))
> >>>>>>    {
> >>>>>>        throw new AuthorizationException("Insufficient privileges to
> >>>>>> edit this customer");
> >>>>>>    }
> >>>>>>
> >>>>>>    // snip code
> >>>>>> }
> >>>>>>
> >>>>>> We could potentially also do some clever stuff with method-level
> >>>>>> annotations here.  Off the top of my head, something like this
> might work:
> >>>>>>
> >>>>>> @CheckPermissions
> >>>>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
> >>>>>>
> >>>>>> The @CheckPermissions annotation would be a security binding type
> with
> >>>>>> a matching authorizer that would scan the parameter list and
> perform any
> >>>>>> checks on parameters annotated with @CheckPermission.  Anyway we
> can refine
> >>>>>> this idea in ongoing discussions.
> >>>>>>
> >>>>>> In the default implementation, the Identity.hasPermission() method
> >>>>>> essentially contains no code, instead it delegates the permission
> check to
> >>>>>> the PermissionMapper bean, an implementation-only bean which
> contains a list
> >>>>>> of PermissionResolver instances that are used to perform the
> permission
> >>>>>> check.
> >>>>>>
> >>>>>> public class DefaultIdentity implements Identity
> >>>>>> {
> >>>>>>    // snip code
> >>>>>>
> >>>>>>    public boolean hasPermission(Object resource, String operation)
> >>>>>>    {
> >>>>>>        return permissionMapper.resolvePermission(resource,
> operation);
> >>>>>>    }
> >>>>>> }
> >>>>>>
> >>>>>> The PermissionMapper bean provides the resolvePermission() method,
> >>>>>> which basically iterates through the known PermissionResolvers, and
> if one
> >>>>>> of them returns true for the permission check, then a true result is
> >>>>>> returned.
> >>>>>>
> >>>>>> public class PermissionMapper
> >>>>>> {
> >>>>>>    @Inject Instance<PermissionResolver> resolvers;
> >>>>>>
> >>>>>>    public boolean resolvePermission(Object resource, String
> operation)
> >>>>>>    {
> >>>>>>        for (PermissionResolver resolver : resolvers)
> >>>>>>        {
> >>>>>>            if (resolver.hasPermission(resource, operation)) return
> >>>>>> true;
> >>>>>>        }
> >>>>>>        return false;
> >>>>>>    }
> >>>>>> }
> >>>>>>
> >>>>>> We can do some clever stuff here, like caching which permission
> >>>>>> resolver returns a true result for a particular class of resource,
> and then
> >>>>>> always using that resolver for that class, etc.
> >>>>>>
> >>>>>> PermissionResolver is an API interface, the implementations of which
> >>>>>> do the actual work of checking the permission.
> >>>>>>
> >>>>>> public interface PermissionResolver
> >>>>>> {
> >>>>>>    boolean hasPermission(Object resource, String operation);
> >>>>>> }
> >>>>>
> >>>>> I think that it might be useful if PermissionResolver can also deny
> >>>>> permissions? So it may look like:
> >>>>>
> >>>>> public interface PermissionResolver
> >>>>> {
> >>>>>   PermissionType hasPermission(Object resource, String operation);
> >>>>> }
> >>>>>
> >>>>> where PermissionType can be:
> >>>>>
> >>>>> public enum PermissionType
> >>>>> {
> >>>>>   PERMIT, DENY, NOT_APPLICABLE
> >>>>> }
> >>>>>
> >>>>> and PermissionMapper implementation can look like:
> >>>>>
> >>>>> public class PermissionMapper
> >>>>> {
> >>>>>    @Inject Instance<PermissionResolver> resolvers;
> >>>>>
> >>>>>    public boolean resolvePermission(Object resource, String
> operation)
> >>>>>    {
> >>>>>        for (PermissionResolver resolver : resolvers)
> >>>>>        {
> >>>>>            PermissionType permissionType =
> >>>>> resolver.hasPermission(resource, operation);
> >>>>>            if (permissionType == PERMIT)
> >>>>>            {
> >>>>>              return true;
> >>>>>            }
> >>>>>            else if (permissionType == DENY)
> >>>>>            {
> >>>>>              return false;
> >>>>>            }
> >>>>>        }
> >>>>>
> >>>>>        return false;
> >>>>>    }
> >>>>> }
> >>>>>
> >>>>> And order of PermissionResolvers inside PermissionMapper can be
> ordered
> >>>>> according to some priorities. This design may be more flexible. For
> example,
> >>>>> it will allow you to quickly or temporarily revoke all permissions
> for
> >>>>> particular user.
> >>>>
> >>>>
> >>>> This is also a good use case.  We would need to iterate through all
> >>>> PermissionResolvers though and if any of them returned a DENY then our
> >>>> permission check would return a final result of false.
> >>>>
> >>>>>
> >>>>>>
> >>>>>> We would provide one PermissionResolver implementation out of the
> box
> >>>>>> with DeltaSpike; PersistentPermissionResolver would provide
> permission
> >>>>>> checks for ACL-style object permissions and provide a key piece of
> >>>>>> functionality required by a complete permission management API.
>  Developers
> >>>>>> can easily provide their own PermissionResolver implementations
> with custom
> >>>>>> business logic by simply deploying a PermissionResolver bean in
> their own
> >>>>>> application.
> >>>>>>
> >>>>>> This pretty much covers the basics of the object permission API, at
> >>>>>> least on the consuming side.  We can discuss this area first before
> moving
> >>>>>> onto the permission management API shortly.
> >>>>>>
> >>>>>>
> >>>>>
> >>>>
> >>>>
> >>>
> >>
> >>
> >
>
>
>
> --
> Christian Kaltepoth
> Blog: http://chkal.blogspot.com/
> Twitter: http://twitter.com/chkal
>

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Christian Kaltepoth <ch...@kaltepoth.de>.
I prefer the method signature Shane suggested in his initial mail.
Providing a single method with an object parameter makes most sense to
me:

  Identity.hasPermission(Object resource, String operation)

This way the developer is completely free to choose how to identify
the resource. I think in most cases the object itself will be used.
And the JSF example Shane showed is a very good example for this.

Of cause people may need other ways to refer to resources like in the
ResourceIdentifier concept Marek showed. But actually I see something
like this more as an usage pattern and I don't think it belongs
directly into the API. Perhaps we could provide something like
ResourceIdentifier as an utility class that people can use if they
want to refer to resources this way. But the API of Identity should be
as simple as possible. And the object parameter will allow people to
use any object they want. Even something like ResourceIdentifier.

Christian


2012/4/19 Marek Posolda <mp...@redhat.com>:
> On 19.4.2012 13:56, Shane Bryzak wrote:
>>
>> On 19/04/12 20:15, Marek Posolda wrote:
>>>
>>> On 19.4.2012 00:56, Shane Bryzak wrote:
>>>>
>>>> On 19/04/12 08:03, Marek Posolda wrote:
>>>>>
>>>>> Hi all,
>>>>>
>>>>> I am Marek Posolda and i am working on GateIn portal project together
>>>>> wth Bolek Dawidowicz. Some of my notes inline:
>>>>>
>>>>> On 18.4.2012 00:52, Shane Bryzak wrote:
>>>>>>
>>>>>> I'd like to kick off a discussion around the Authorization API,
>>>>>> specifically object permissions.  This API is used to determine whether the
>>>>>> currently authenticated user has the necessary privileges to perform an
>>>>>> operation on a particular domain object.  I'll start by outlining my
>>>>>> proposal covering the simple use cases of this API and we can then proceed
>>>>>> from there.
>>>>>>
>>>>>> First of all, the developer needs a gateway into the permission API so
>>>>>> that they can perform permission checks within their own application.  This
>>>>>> is provided by the hasPermission() method:
>>>>>>
>>>>>>
>>>>>> Identity.hasPermission(Object resource, String operation)
>>>>>
>>>>> How about having additional method like:
>>>>>
>>>>>
>>>>> Identity.hasPermission(String resourceType, Serializable resourceId,
>>>>> String operation);
>>>>>
>>>>> This have one advantage that you don't need to obtain instance of
>>>>> Object from DB when you have only ID of this resource. Use case example can
>>>>> be:
>>>>>
>>>>> public Customer loadCustomer(Long customerId)
>>>>> {
>>>>>  if (!identity.hasPermission("org.myorganization.Customer", customerId,
>>>>> "READ")
>>>>>  {
>>>>>    throw new AuthorizationException("Insufficient privileges to load
>>>>> this customer");
>>>>>  }
>>>>>
>>>>>  // snip code
>>>>> }
>>>>>
>>>>
>>>> This is a good use case, I'm wondering though if we could still wrap
>>>> this up as a resource object to keep the API clean.  Perhaps something like
>>>> this:
>>>>
>>>> public Customer loadCustomer(Long customerId)
>>>> {
>>>>  if (!identity.hasPermission(new
>>>> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
>>>>  {
>>>>    throw new AuthorizationException("Insufficient privileges to load
>>>> this customer");
>>>>  }
>>>>
>>>>  // snip code
>>>> }
>>>
>>> Yes, that should work too. Question is if we still have two methods then
>>> or only single method with signature:
>>> Identity.hasPermission(ResourceIdentifier identifier, String action) throws
>>> AuthorizationException;  ?
>>>
>>> Seems that one method may be sufficient as long as you can always obtain
>>> ResourceIdentifier for particular object when you have Type and ID of this
>>> object. So your edit method can then looks like:
>>>
>>> @Inject Identity identity;
>>>
>>> public void editCustomer(Customer customer)
>>> {
>>>    if (!identity.hasPermission(new
>>> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
>>>    {
>>>        throw new AuthorizationException("Insufficient privileges to edit
>>> this customer");
>>>    }
>>>
>>>    // snip code
>>> }
>>>
>>>
>>> Marek
>>
>>
>>
>> Except that this makes it harder to consume - for example, think of a JSF
>> page with a table of records.  For each row in the table, you may wish to
>> display action buttons etc depending on the privileges that the user has for
>> that row.
>>
>> <h:dataTable value="#{customerSearch.searchResults}" var="customer">
>> <h:column>
>> <h:outputText value="#{customer.name}"/>
>> </h:column>
>> <h:column>
>> <ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
>> <!-- action buttons go here -->
>> </ui:fragment>
>> </h:column>
>> </h:dataTable>
>>
>> This wouldn't be possibly if Identity.hasPermission() required a
>> ResourceIdentifier parameter.  I think an Object parameter is going to be
>> the most versatile for our requirements, and we can just have special
>> handling when the resource type is a ResourceIdentifier.
>
> True. So having two methods:
> - Identity.hasPermission(Object resource, String action) throws
> AuthorizationException;
> - Identity.hasPermission(ResourceIdentifier resourceIdentifier, String
> action) throws AuthorizationException;
>
> should work better then.
>
>
>>
>>>
>>>
>>>>
>>>>
>>>>
>>>>>>
>>>>>>
>>>>>> A permission has three aspects; 1) The application resource for which
>>>>>> the permission is granted, 2) The operation that has been granted for that
>>>>>> resource, and 3) The recipient of the permission, which may be either a
>>>>>> User, Group or Role.
>>>>>>
>>>>>> For example, if we wish to check whether the user has permission to
>>>>>> edit a Customer instance the code might look like this:
>>>>>>
>>>>>> @Inject Identity identity;
>>>>>>
>>>>>> public void editCustomer(Customer customer)
>>>>>> {
>>>>>>    if (!identity.hasPermission(customer, "EDIT"))
>>>>>>    {
>>>>>>        throw new AuthorizationException("Insufficient privileges to
>>>>>> edit this customer");
>>>>>>    }
>>>>>>
>>>>>>    // snip code
>>>>>> }
>>>>>>
>>>>>> We could potentially also do some clever stuff with method-level
>>>>>> annotations here.  Off the top of my head, something like this might work:
>>>>>>
>>>>>> @CheckPermissions
>>>>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>>>>>>
>>>>>> The @CheckPermissions annotation would be a security binding type with
>>>>>> a matching authorizer that would scan the parameter list and perform any
>>>>>> checks on parameters annotated with @CheckPermission.  Anyway we can refine
>>>>>> this idea in ongoing discussions.
>>>>>>
>>>>>> In the default implementation, the Identity.hasPermission() method
>>>>>> essentially contains no code, instead it delegates the permission check to
>>>>>> the PermissionMapper bean, an implementation-only bean which contains a list
>>>>>> of PermissionResolver instances that are used to perform the permission
>>>>>> check.
>>>>>>
>>>>>> public class DefaultIdentity implements Identity
>>>>>> {
>>>>>>    // snip code
>>>>>>
>>>>>>    public boolean hasPermission(Object resource, String operation)
>>>>>>    {
>>>>>>        return permissionMapper.resolvePermission(resource, operation);
>>>>>>    }
>>>>>> }
>>>>>>
>>>>>> The PermissionMapper bean provides the resolvePermission() method,
>>>>>> which basically iterates through the known PermissionResolvers, and if one
>>>>>> of them returns true for the permission check, then a true result is
>>>>>> returned.
>>>>>>
>>>>>> public class PermissionMapper
>>>>>> {
>>>>>>    @Inject Instance<PermissionResolver> resolvers;
>>>>>>
>>>>>>    public boolean resolvePermission(Object resource, String operation)
>>>>>>    {
>>>>>>        for (PermissionResolver resolver : resolvers)
>>>>>>        {
>>>>>>            if (resolver.hasPermission(resource, operation)) return
>>>>>> true;
>>>>>>        }
>>>>>>        return false;
>>>>>>    }
>>>>>> }
>>>>>>
>>>>>> We can do some clever stuff here, like caching which permission
>>>>>> resolver returns a true result for a particular class of resource, and then
>>>>>> always using that resolver for that class, etc.
>>>>>>
>>>>>> PermissionResolver is an API interface, the implementations of which
>>>>>> do the actual work of checking the permission.
>>>>>>
>>>>>> public interface PermissionResolver
>>>>>> {
>>>>>>    boolean hasPermission(Object resource, String operation);
>>>>>> }
>>>>>
>>>>> I think that it might be useful if PermissionResolver can also deny
>>>>> permissions? So it may look like:
>>>>>
>>>>> public interface PermissionResolver
>>>>> {
>>>>>   PermissionType hasPermission(Object resource, String operation);
>>>>> }
>>>>>
>>>>> where PermissionType can be:
>>>>>
>>>>> public enum PermissionType
>>>>> {
>>>>>   PERMIT, DENY, NOT_APPLICABLE
>>>>> }
>>>>>
>>>>> and PermissionMapper implementation can look like:
>>>>>
>>>>> public class PermissionMapper
>>>>> {
>>>>>    @Inject Instance<PermissionResolver> resolvers;
>>>>>
>>>>>    public boolean resolvePermission(Object resource, String operation)
>>>>>    {
>>>>>        for (PermissionResolver resolver : resolvers)
>>>>>        {
>>>>>            PermissionType permissionType =
>>>>> resolver.hasPermission(resource, operation);
>>>>>            if (permissionType == PERMIT)
>>>>>            {
>>>>>              return true;
>>>>>            }
>>>>>            else if (permissionType == DENY)
>>>>>            {
>>>>>              return false;
>>>>>            }
>>>>>        }
>>>>>
>>>>>        return false;
>>>>>    }
>>>>> }
>>>>>
>>>>> And order of PermissionResolvers inside PermissionMapper can be ordered
>>>>> according to some priorities. This design may be more flexible. For example,
>>>>> it will allow you to quickly or temporarily revoke all permissions for
>>>>> particular user.
>>>>
>>>>
>>>> This is also a good use case.  We would need to iterate through all
>>>> PermissionResolvers though and if any of them returned a DENY then our
>>>> permission check would return a final result of false.
>>>>
>>>>>
>>>>>>
>>>>>> We would provide one PermissionResolver implementation out of the box
>>>>>> with DeltaSpike; PersistentPermissionResolver would provide permission
>>>>>> checks for ACL-style object permissions and provide a key piece of
>>>>>> functionality required by a complete permission management API.  Developers
>>>>>> can easily provide their own PermissionResolver implementations with custom
>>>>>> business logic by simply deploying a PermissionResolver bean in their own
>>>>>> application.
>>>>>>
>>>>>> This pretty much covers the basics of the object permission API, at
>>>>>> least on the consuming side.  We can discuss this area first before moving
>>>>>> onto the permission management API shortly.
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>



-- 
Christian Kaltepoth
Blog: http://chkal.blogspot.com/
Twitter: http://twitter.com/chkal

Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Marek Posolda <mp...@redhat.com>.
On 19.4.2012 13:56, Shane Bryzak wrote:
> On 19/04/12 20:15, Marek Posolda wrote:
>> On 19.4.2012 00:56, Shane Bryzak wrote:
>>> On 19/04/12 08:03, Marek Posolda wrote:
>>>> Hi all,
>>>>
>>>> I am Marek Posolda and i am working on GateIn portal project 
>>>> together wth Bolek Dawidowicz. Some of my notes inline:
>>>>
>>>> On 18.4.2012 00:52, Shane Bryzak wrote:
>>>>> I'd like to kick off a discussion around the Authorization API, 
>>>>> specifically object permissions.  This API is used to determine 
>>>>> whether the currently authenticated user has the necessary 
>>>>> privileges to perform an operation on a particular domain object.  
>>>>> I'll start by outlining my proposal covering the simple use cases 
>>>>> of this API and we can then proceed from there.
>>>>>
>>>>> First of all, the developer needs a gateway into the permission 
>>>>> API so that they can perform permission checks within their own 
>>>>> application.  This is provided by the hasPermission() method:
>>>>>
>>>>>
>>>>> Identity.hasPermission(Object resource, String operation)
>>>> How about having additional method like:
>>>>
>>>>
>>>> Identity.hasPermission(String resourceType, Serializable 
>>>> resourceId, String operation);
>>>>
>>>> This have one advantage that you don't need to obtain instance of 
>>>> Object from DB when you have only ID of this resource. Use case 
>>>> example can be:
>>>>
>>>> public Customer loadCustomer(Long customerId)
>>>> {
>>>>   if (!identity.hasPermission("org.myorganization.Customer", 
>>>> customerId, "READ")
>>>>   {
>>>>     throw new AuthorizationException("Insufficient privileges to 
>>>> load this customer");
>>>>   }
>>>>
>>>>   // snip code
>>>> }
>>>>
>>>
>>> This is a good use case, I'm wondering though if we could still wrap 
>>> this up as a resource object to keep the API clean.  Perhaps 
>>> something like this:
>>>
>>> public Customer loadCustomer(Long customerId)
>>> {
>>>   if (!identity.hasPermission(new 
>>> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
>>>   {
>>>     throw new AuthorizationException("Insufficient privileges to 
>>> load this customer");
>>>   }
>>>
>>>   // snip code
>>> }
>> Yes, that should work too. Question is if we still have two methods 
>> then or only single method with signature: 
>> Identity.hasPermission(ResourceIdentifier identifier, String action) 
>> throws AuthorizationException;  ?
>>
>> Seems that one method may be sufficient as long as you can always 
>> obtain ResourceIdentifier for particular object when you have Type 
>> and ID of this object. So your edit method can then looks like:
>>
>> @Inject Identity identity;
>>
>> public void editCustomer(Customer customer)
>> {
>>     if (!identity.hasPermission(new 
>> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
>>     {
>>         throw new AuthorizationException("Insufficient privileges to 
>> edit this customer");
>>     }
>>
>>     // snip code
>> }
>>
>>
>> Marek
>
>
> Except that this makes it harder to consume - for example, think of a 
> JSF page with a table of records.  For each row in the table, you may 
> wish to display action buttons etc depending on the privileges that 
> the user has for that row.
>
> <h:dataTable value="#{customerSearch.searchResults}" var="customer">
> <h:column>
> <h:outputText value="#{customer.name}"/>
> </h:column>
> <h:column>
> <ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
> <!-- action buttons go here -->
> </ui:fragment>
> </h:column>
> </h:dataTable>
>
> This wouldn't be possibly if Identity.hasPermission() required a 
> ResourceIdentifier parameter.  I think an Object parameter is going to 
> be the most versatile for our requirements, and we can just have 
> special handling when the resource type is a ResourceIdentifier.
True. So having two methods:
- Identity.hasPermission(Object resource, String action) throws 
AuthorizationException;
- Identity.hasPermission(ResourceIdentifier resourceIdentifier, String 
action) throws AuthorizationException;

should work better then.

>
>>
>>
>>>
>>>
>>>
>>>>>
>>>>>
>>>>> A permission has three aspects; 1) The application resource for 
>>>>> which the permission is granted, 2) The operation that has been 
>>>>> granted for that resource, and 3) The recipient of the permission, 
>>>>> which may be either a User, Group or Role.
>>>>>
>>>>> For example, if we wish to check whether the user has permission 
>>>>> to edit a Customer instance the code might look like this:
>>>>>
>>>>> @Inject Identity identity;
>>>>>
>>>>> public void editCustomer(Customer customer)
>>>>> {
>>>>>     if (!identity.hasPermission(customer, "EDIT"))
>>>>>     {
>>>>>         throw new AuthorizationException("Insufficient privileges 
>>>>> to edit this customer");
>>>>>     }
>>>>>
>>>>>     // snip code
>>>>> }
>>>>>
>>>>> We could potentially also do some clever stuff with method-level 
>>>>> annotations here.  Off the top of my head, something like this 
>>>>> might work:
>>>>>
>>>>> @CheckPermissions
>>>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>>>>>
>>>>> The @CheckPermissions annotation would be a security binding type 
>>>>> with a matching authorizer that would scan the parameter list and 
>>>>> perform any checks on parameters annotated with @CheckPermission.  
>>>>> Anyway we can refine this idea in ongoing discussions.
>>>>>
>>>>> In the default implementation, the Identity.hasPermission() method 
>>>>> essentially contains no code, instead it delegates the permission 
>>>>> check to the PermissionMapper bean, an implementation-only bean 
>>>>> which contains a list of PermissionResolver instances that are 
>>>>> used to perform the permission check.
>>>>>
>>>>> public class DefaultIdentity implements Identity
>>>>> {
>>>>>     // snip code
>>>>>
>>>>>     public boolean hasPermission(Object resource, String operation)
>>>>>     {
>>>>>         return permissionMapper.resolvePermission(resource, 
>>>>> operation);
>>>>>     }
>>>>> }
>>>>>
>>>>> The PermissionMapper bean provides the resolvePermission() method, 
>>>>> which basically iterates through the known PermissionResolvers, 
>>>>> and if one of them returns true for the permission check, then a 
>>>>> true result is returned.
>>>>>
>>>>> public class PermissionMapper
>>>>> {
>>>>>     @Inject Instance<PermissionResolver> resolvers;
>>>>>
>>>>>     public boolean resolvePermission(Object resource, String 
>>>>> operation)
>>>>>     {
>>>>>         for (PermissionResolver resolver : resolvers)
>>>>>         {
>>>>>             if (resolver.hasPermission(resource, operation)) 
>>>>> return true;
>>>>>         }
>>>>>         return false;
>>>>>     }
>>>>> }
>>>>>
>>>>> We can do some clever stuff here, like caching which permission 
>>>>> resolver returns a true result for a particular class of resource, 
>>>>> and then always using that resolver for that class, etc.
>>>>>
>>>>> PermissionResolver is an API interface, the implementations of 
>>>>> which do the actual work of checking the permission.
>>>>>
>>>>> public interface PermissionResolver
>>>>> {
>>>>>     boolean hasPermission(Object resource, String operation);
>>>>> }
>>>> I think that it might be useful if PermissionResolver can also deny 
>>>> permissions? So it may look like:
>>>>
>>>> public interface PermissionResolver
>>>> {
>>>>    PermissionType hasPermission(Object resource, String operation);
>>>> }
>>>>
>>>> where PermissionType can be:
>>>>
>>>> public enum PermissionType
>>>> {
>>>>    PERMIT, DENY, NOT_APPLICABLE
>>>> }
>>>>
>>>> and PermissionMapper implementation can look like:
>>>>
>>>> public class PermissionMapper
>>>> {
>>>>     @Inject Instance<PermissionResolver> resolvers;
>>>>
>>>>     public boolean resolvePermission(Object resource, String 
>>>> operation)
>>>>     {
>>>>         for (PermissionResolver resolver : resolvers)
>>>>         {
>>>>             PermissionType permissionType = 
>>>> resolver.hasPermission(resource, operation);
>>>>             if (permissionType == PERMIT)
>>>>             {
>>>>               return true;
>>>>             }
>>>>             else if (permissionType == DENY)
>>>>             {
>>>>               return false;
>>>>             }
>>>>         }
>>>>
>>>>         return false;
>>>>     }
>>>> }
>>>>
>>>> And order of PermissionResolvers inside PermissionMapper can be 
>>>> ordered according to some priorities. This design may be more 
>>>> flexible. For example, it will allow you to quickly or temporarily 
>>>> revoke all permissions for particular user.
>>>
>>> This is also a good use case.  We would need to iterate through all 
>>> PermissionResolvers though and if any of them returned a DENY then 
>>> our permission check would return a final result of false.
>>>
>>>>
>>>>>
>>>>> We would provide one PermissionResolver implementation out of the 
>>>>> box with DeltaSpike; PersistentPermissionResolver would provide 
>>>>> permission checks for ACL-style object permissions and provide a 
>>>>> key piece of functionality required by a complete permission 
>>>>> management API.  Developers can easily provide their own 
>>>>> PermissionResolver implementations with custom business logic by 
>>>>> simply deploying a PermissionResolver bean in their own application.
>>>>>
>>>>> This pretty much covers the basics of the object permission API, 
>>>>> at least on the consuming side.  We can discuss this area first 
>>>>> before moving onto the permission management API shortly.
>>>>>
>>>>>
>>>>
>>>
>>>
>>
>
>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Shane Bryzak <sb...@redhat.com>.
On 19/04/12 20:15, Marek Posolda wrote:
> On 19.4.2012 00:56, Shane Bryzak wrote:
>> On 19/04/12 08:03, Marek Posolda wrote:
>>> Hi all,
>>>
>>> I am Marek Posolda and i am working on GateIn portal project 
>>> together wth Bolek Dawidowicz. Some of my notes inline:
>>>
>>> On 18.4.2012 00:52, Shane Bryzak wrote:
>>>> I'd like to kick off a discussion around the Authorization API, 
>>>> specifically object permissions.  This API is used to determine 
>>>> whether the currently authenticated user has the necessary 
>>>> privileges to perform an operation on a particular domain object.  
>>>> I'll start by outlining my proposal covering the simple use cases 
>>>> of this API and we can then proceed from there.
>>>>
>>>> First of all, the developer needs a gateway into the permission API 
>>>> so that they can perform permission checks within their own 
>>>> application.  This is provided by the hasPermission() method:
>>>>
>>>>
>>>> Identity.hasPermission(Object resource, String operation)
>>> How about having additional method like:
>>>
>>>
>>> Identity.hasPermission(String resourceType, Serializable resourceId, 
>>> String operation);
>>>
>>> This have one advantage that you don't need to obtain instance of 
>>> Object from DB when you have only ID of this resource. Use case 
>>> example can be:
>>>
>>> public Customer loadCustomer(Long customerId)
>>> {
>>>   if (!identity.hasPermission("org.myorganization.Customer", 
>>> customerId, "READ")
>>>   {
>>>     throw new AuthorizationException("Insufficient privileges to 
>>> load this customer");
>>>   }
>>>
>>>   // snip code
>>> }
>>>
>>
>> This is a good use case, I'm wondering though if we could still wrap 
>> this up as a resource object to keep the API clean.  Perhaps 
>> something like this:
>>
>> public Customer loadCustomer(Long customerId)
>> {
>>   if (!identity.hasPermission(new 
>> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
>>   {
>>     throw new AuthorizationException("Insufficient privileges to load 
>> this customer");
>>   }
>>
>>   // snip code
>> }
> Yes, that should work too. Question is if we still have two methods 
> then or only single method with signature: 
> Identity.hasPermission(ResourceIdentifier identifier, String action) 
> throws AuthorizationException;  ?
>
> Seems that one method may be sufficient as long as you can always 
> obtain ResourceIdentifier for particular object when you have Type and 
> ID of this object. So your edit method can then looks like:
>
> @Inject Identity identity;
>
> public void editCustomer(Customer customer)
> {
>     if (!identity.hasPermission(new 
> ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
>     {
>         throw new AuthorizationException("Insufficient privileges to 
> edit this customer");
>     }
>
>     // snip code
> }
>
>
> Marek


Except that this makes it harder to consume - for example, think of a 
JSF page with a table of records.  For each row in the table, you may 
wish to display action buttons etc depending on the privileges that the 
user has for that row.

<h:dataTable value="#{customerSearch.searchResults}" var="customer">
<h:column>
<h:outputText value="#{customer.name}"/>
</h:column>
<h:column>
<ui:fragment rendered="#{identity.hasPermission(customer, 'EDIT')}">
<!-- action buttons go here -->
</ui:fragment>
</h:column>
</h:dataTable>

This wouldn't be possibly if Identity.hasPermission() required a 
ResourceIdentifier parameter.  I think an Object parameter is going to 
be the most versatile for our requirements, and we can just have special 
handling when the resource type is a ResourceIdentifier.

>
>
>>
>>
>>
>>>>
>>>>
>>>> A permission has three aspects; 1) The application resource for 
>>>> which the permission is granted, 2) The operation that has been 
>>>> granted for that resource, and 3) The recipient of the permission, 
>>>> which may be either a User, Group or Role.
>>>>
>>>> For example, if we wish to check whether the user has permission to 
>>>> edit a Customer instance the code might look like this:
>>>>
>>>> @Inject Identity identity;
>>>>
>>>> public void editCustomer(Customer customer)
>>>> {
>>>>     if (!identity.hasPermission(customer, "EDIT"))
>>>>     {
>>>>         throw new AuthorizationException("Insufficient privileges 
>>>> to edit this customer");
>>>>     }
>>>>
>>>>     // snip code
>>>> }
>>>>
>>>> We could potentially also do some clever stuff with method-level 
>>>> annotations here.  Off the top of my head, something like this 
>>>> might work:
>>>>
>>>> @CheckPermissions
>>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>>>>
>>>> The @CheckPermissions annotation would be a security binding type 
>>>> with a matching authorizer that would scan the parameter list and 
>>>> perform any checks on parameters annotated with @CheckPermission.  
>>>> Anyway we can refine this idea in ongoing discussions.
>>>>
>>>> In the default implementation, the Identity.hasPermission() method 
>>>> essentially contains no code, instead it delegates the permission 
>>>> check to the PermissionMapper bean, an implementation-only bean 
>>>> which contains a list of PermissionResolver instances that are used 
>>>> to perform the permission check.
>>>>
>>>> public class DefaultIdentity implements Identity
>>>> {
>>>>     // snip code
>>>>
>>>>     public boolean hasPermission(Object resource, String operation)
>>>>     {
>>>>         return permissionMapper.resolvePermission(resource, 
>>>> operation);
>>>>     }
>>>> }
>>>>
>>>> The PermissionMapper bean provides the resolvePermission() method, 
>>>> which basically iterates through the known PermissionResolvers, and 
>>>> if one of them returns true for the permission check, then a true 
>>>> result is returned.
>>>>
>>>> public class PermissionMapper
>>>> {
>>>>     @Inject Instance<PermissionResolver> resolvers;
>>>>
>>>>     public boolean resolvePermission(Object resource, String 
>>>> operation)
>>>>     {
>>>>         for (PermissionResolver resolver : resolvers)
>>>>         {
>>>>             if (resolver.hasPermission(resource, operation)) return 
>>>> true;
>>>>         }
>>>>         return false;
>>>>     }
>>>> }
>>>>
>>>> We can do some clever stuff here, like caching which permission 
>>>> resolver returns a true result for a particular class of resource, 
>>>> and then always using that resolver for that class, etc.
>>>>
>>>> PermissionResolver is an API interface, the implementations of 
>>>> which do the actual work of checking the permission.
>>>>
>>>> public interface PermissionResolver
>>>> {
>>>>     boolean hasPermission(Object resource, String operation);
>>>> }
>>> I think that it might be useful if PermissionResolver can also deny 
>>> permissions? So it may look like:
>>>
>>> public interface PermissionResolver
>>> {
>>>    PermissionType hasPermission(Object resource, String operation);
>>> }
>>>
>>> where PermissionType can be:
>>>
>>> public enum PermissionType
>>> {
>>>    PERMIT, DENY, NOT_APPLICABLE
>>> }
>>>
>>> and PermissionMapper implementation can look like:
>>>
>>> public class PermissionMapper
>>> {
>>>     @Inject Instance<PermissionResolver> resolvers;
>>>
>>>     public boolean resolvePermission(Object resource, String operation)
>>>     {
>>>         for (PermissionResolver resolver : resolvers)
>>>         {
>>>             PermissionType permissionType = 
>>> resolver.hasPermission(resource, operation);
>>>             if (permissionType == PERMIT)
>>>             {
>>>               return true;
>>>             }
>>>             else if (permissionType == DENY)
>>>             {
>>>               return false;
>>>             }
>>>         }
>>>
>>>         return false;
>>>     }
>>> }
>>>
>>> And order of PermissionResolvers inside PermissionMapper can be 
>>> ordered according to some priorities. This design may be more 
>>> flexible. For example, it will allow you to quickly or temporarily 
>>> revoke all permissions for particular user.
>>
>> This is also a good use case.  We would need to iterate through all 
>> PermissionResolvers though and if any of them returned a DENY then 
>> our permission check would return a final result of false.
>>
>>>
>>>>
>>>> We would provide one PermissionResolver implementation out of the 
>>>> box with DeltaSpike; PersistentPermissionResolver would provide 
>>>> permission checks for ACL-style object permissions and provide a 
>>>> key piece of functionality required by a complete permission 
>>>> management API.  Developers can easily provide their own 
>>>> PermissionResolver implementations with custom business logic by 
>>>> simply deploying a PermissionResolver bean in their own application.
>>>>
>>>> This pretty much covers the basics of the object permission API, at 
>>>> least on the consuming side.  We can discuss this area first before 
>>>> moving onto the permission management API shortly.
>>>>
>>>>
>>>
>>
>>
>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Marek Posolda <mp...@redhat.com>.
On 19.4.2012 00:56, Shane Bryzak wrote:
> On 19/04/12 08:03, Marek Posolda wrote:
>> Hi all,
>>
>> I am Marek Posolda and i am working on GateIn portal project together 
>> wth Bolek Dawidowicz. Some of my notes inline:
>>
>> On 18.4.2012 00:52, Shane Bryzak wrote:
>>> I'd like to kick off a discussion around the Authorization API, 
>>> specifically object permissions.  This API is used to determine 
>>> whether the currently authenticated user has the necessary 
>>> privileges to perform an operation on a particular domain object.  
>>> I'll start by outlining my proposal covering the simple use cases of 
>>> this API and we can then proceed from there.
>>>
>>> First of all, the developer needs a gateway into the permission API 
>>> so that they can perform permission checks within their own 
>>> application.  This is provided by the hasPermission() method:
>>>
>>>
>>> Identity.hasPermission(Object resource, String operation)
>> How about having additional method like:
>>
>>
>> Identity.hasPermission(String resourceType, Serializable resourceId, 
>> String operation);
>>
>> This have one advantage that you don't need to obtain instance of 
>> Object from DB when you have only ID of this resource. Use case 
>> example can be:
>>
>> public Customer loadCustomer(Long customerId)
>> {
>>   if (!identity.hasPermission("org.myorganization.Customer", 
>> customerId, "READ")
>>   {
>>     throw new AuthorizationException("Insufficient privileges to load 
>> this customer");
>>   }
>>
>>   // snip code
>> }
>>
>
> This is a good use case, I'm wondering though if we could still wrap 
> this up as a resource object to keep the API clean.  Perhaps something 
> like this:
>
> public Customer loadCustomer(Long customerId)
> {
>   if (!identity.hasPermission(new 
> ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
>   {
>     throw new AuthorizationException("Insufficient privileges to load 
> this customer");
>   }
>
>   // snip code
> }
Yes, that should work too. Question is if we still have two methods then 
or only single method with signature: 
Identity.hasPermission(ResourceIdentifier identifier, String action) 
throws AuthorizationException;  ?

Seems that one method may be sufficient as long as you can always obtain 
ResourceIdentifier for particular object when you have Type and ID of 
this object. So your edit method can then looks like:

@Inject Identity identity;

public void editCustomer(Customer customer)
{
     if (!identity.hasPermission(new 
ResourceIdentifier(customer.getClass(), customer.getId()), "EDIT"))
     {
         throw new AuthorizationException("Insufficient privileges to 
edit this customer");
     }

     // snip code
}


Marek


>
>
>
>>>
>>>
>>> A permission has three aspects; 1) The application resource for 
>>> which the permission is granted, 2) The operation that has been 
>>> granted for that resource, and 3) The recipient of the permission, 
>>> which may be either a User, Group or Role.
>>>
>>> For example, if we wish to check whether the user has permission to 
>>> edit a Customer instance the code might look like this:
>>>
>>> @Inject Identity identity;
>>>
>>> public void editCustomer(Customer customer)
>>> {
>>>     if (!identity.hasPermission(customer, "EDIT"))
>>>     {
>>>         throw new AuthorizationException("Insufficient privileges to 
>>> edit this customer");
>>>     }
>>>
>>>     // snip code
>>> }
>>>
>>> We could potentially also do some clever stuff with method-level 
>>> annotations here.  Off the top of my head, something like this might 
>>> work:
>>>
>>> @CheckPermissions
>>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>>>
>>> The @CheckPermissions annotation would be a security binding type 
>>> with a matching authorizer that would scan the parameter list and 
>>> perform any checks on parameters annotated with @CheckPermission.  
>>> Anyway we can refine this idea in ongoing discussions.
>>>
>>> In the default implementation, the Identity.hasPermission() method 
>>> essentially contains no code, instead it delegates the permission 
>>> check to the PermissionMapper bean, an implementation-only bean 
>>> which contains a list of PermissionResolver instances that are used 
>>> to perform the permission check.
>>>
>>> public class DefaultIdentity implements Identity
>>> {
>>>     // snip code
>>>
>>>     public boolean hasPermission(Object resource, String operation)
>>>     {
>>>         return permissionMapper.resolvePermission(resource, operation);
>>>     }
>>> }
>>>
>>> The PermissionMapper bean provides the resolvePermission() method, 
>>> which basically iterates through the known PermissionResolvers, and 
>>> if one of them returns true for the permission check, then a true 
>>> result is returned.
>>>
>>> public class PermissionMapper
>>> {
>>>     @Inject Instance<PermissionResolver> resolvers;
>>>
>>>     public boolean resolvePermission(Object resource, String operation)
>>>     {
>>>         for (PermissionResolver resolver : resolvers)
>>>         {
>>>             if (resolver.hasPermission(resource, operation)) return 
>>> true;
>>>         }
>>>         return false;
>>>     }
>>> }
>>>
>>> We can do some clever stuff here, like caching which permission 
>>> resolver returns a true result for a particular class of resource, 
>>> and then always using that resolver for that class, etc.
>>>
>>> PermissionResolver is an API interface, the implementations of which 
>>> do the actual work of checking the permission.
>>>
>>> public interface PermissionResolver
>>> {
>>>     boolean hasPermission(Object resource, String operation);
>>> }
>> I think that it might be useful if PermissionResolver can also deny 
>> permissions? So it may look like:
>>
>> public interface PermissionResolver
>> {
>>    PermissionType hasPermission(Object resource, String operation);
>> }
>>
>> where PermissionType can be:
>>
>> public enum PermissionType
>> {
>>    PERMIT, DENY, NOT_APPLICABLE
>> }
>>
>> and PermissionMapper implementation can look like:
>>
>> public class PermissionMapper
>> {
>>     @Inject Instance<PermissionResolver> resolvers;
>>
>>     public boolean resolvePermission(Object resource, String operation)
>>     {
>>         for (PermissionResolver resolver : resolvers)
>>         {
>>             PermissionType permissionType = 
>> resolver.hasPermission(resource, operation);
>>             if (permissionType == PERMIT)
>>             {
>>               return true;
>>             }
>>             else if (permissionType == DENY)
>>             {
>>               return false;
>>             }
>>         }
>>
>>         return false;
>>     }
>> }
>>
>> And order of PermissionResolvers inside PermissionMapper can be 
>> ordered according to some priorities. This design may be more 
>> flexible. For example, it will allow you to quickly or temporarily 
>> revoke all permissions for particular user.
>
> This is also a good use case.  We would need to iterate through all 
> PermissionResolvers though and if any of them returned a DENY then our 
> permission check would return a final result of false.
>
>>
>>>
>>> We would provide one PermissionResolver implementation out of the 
>>> box with DeltaSpike; PersistentPermissionResolver would provide 
>>> permission checks for ACL-style object permissions and provide a key 
>>> piece of functionality required by a complete permission management 
>>> API.  Developers can easily provide their own PermissionResolver 
>>> implementations with custom business logic by simply deploying a 
>>> PermissionResolver bean in their own application.
>>>
>>> This pretty much covers the basics of the object permission API, at 
>>> least on the consuming side.  We can discuss this area first before 
>>> moving onto the permission management API shortly.
>>>
>>>
>>
>
>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Shane Bryzak <sb...@redhat.com>.
On 19/04/12 08:03, Marek Posolda wrote:
> Hi all,
>
> I am Marek Posolda and i am working on GateIn portal project together 
> wth Bolek Dawidowicz. Some of my notes inline:
>
> On 18.4.2012 00:52, Shane Bryzak wrote:
>> I'd like to kick off a discussion around the Authorization API, 
>> specifically object permissions.  This API is used to determine 
>> whether the currently authenticated user has the necessary privileges 
>> to perform an operation on a particular domain object.  I'll start by 
>> outlining my proposal covering the simple use cases of this API and 
>> we can then proceed from there.
>>
>> First of all, the developer needs a gateway into the permission API 
>> so that they can perform permission checks within their own 
>> application.  This is provided by the hasPermission() method:
>>
>>
>> Identity.hasPermission(Object resource, String operation)
> How about having additional method like:
>
>
> Identity.hasPermission(String resourceType, Serializable resourceId, 
> String operation);
>
> This have one advantage that you don't need to obtain instance of 
> Object from DB when you have only ID of this resource. Use case 
> example can be:
>
> public Customer loadCustomer(Long customerId)
> {
>   if (!identity.hasPermission("org.myorganization.Customer", 
> customerId, "READ")
>   {
>     throw new AuthorizationException("Insufficient privileges to load 
> this customer");
>   }
>
>   // snip code
> }
>

This is a good use case, I'm wondering though if we could still wrap 
this up as a resource object to keep the API clean.  Perhaps something 
like this:

public Customer loadCustomer(Long customerId)
{
   if (!identity.hasPermission(new 
ResourceIdentifier("org.myorganization.Customer", customerId), "READ")
   {
     throw new AuthorizationException("Insufficient privileges to load 
this customer");
   }

   // snip code
}



>>
>>
>> A permission has three aspects; 1) The application resource for which 
>> the permission is granted, 2) The operation that has been granted for 
>> that resource, and 3) The recipient of the permission, which may be 
>> either a User, Group or Role.
>>
>> For example, if we wish to check whether the user has permission to 
>> edit a Customer instance the code might look like this:
>>
>> @Inject Identity identity;
>>
>> public void editCustomer(Customer customer)
>> {
>>     if (!identity.hasPermission(customer, "EDIT"))
>>     {
>>         throw new AuthorizationException("Insufficient privileges to 
>> edit this customer");
>>     }
>>
>>     // snip code
>> }
>>
>> We could potentially also do some clever stuff with method-level 
>> annotations here.  Off the top of my head, something like this might 
>> work:
>>
>> @CheckPermissions
>> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>>
>> The @CheckPermissions annotation would be a security binding type 
>> with a matching authorizer that would scan the parameter list and 
>> perform any checks on parameters annotated with @CheckPermission.  
>> Anyway we can refine this idea in ongoing discussions.
>>
>> In the default implementation, the Identity.hasPermission() method 
>> essentially contains no code, instead it delegates the permission 
>> check to the PermissionMapper bean, an implementation-only bean which 
>> contains a list of PermissionResolver instances that are used to 
>> perform the permission check.
>>
>> public class DefaultIdentity implements Identity
>> {
>>     // snip code
>>
>>     public boolean hasPermission(Object resource, String operation)
>>     {
>>         return permissionMapper.resolvePermission(resource, operation);
>>     }
>> }
>>
>> The PermissionMapper bean provides the resolvePermission() method, 
>> which basically iterates through the known PermissionResolvers, and 
>> if one of them returns true for the permission check, then a true 
>> result is returned.
>>
>> public class PermissionMapper
>> {
>>     @Inject Instance<PermissionResolver> resolvers;
>>
>>     public boolean resolvePermission(Object resource, String operation)
>>     {
>>         for (PermissionResolver resolver : resolvers)
>>         {
>>             if (resolver.hasPermission(resource, operation)) return 
>> true;
>>         }
>>         return false;
>>     }
>> }
>>
>> We can do some clever stuff here, like caching which permission 
>> resolver returns a true result for a particular class of resource, 
>> and then always using that resolver for that class, etc.
>>
>> PermissionResolver is an API interface, the implementations of which 
>> do the actual work of checking the permission.
>>
>> public interface PermissionResolver
>> {
>>     boolean hasPermission(Object resource, String operation);
>> }
> I think that it might be useful if PermissionResolver can also deny 
> permissions? So it may look like:
>
> public interface PermissionResolver
> {
>    PermissionType hasPermission(Object resource, String operation);
> }
>
> where PermissionType can be:
>
> public enum PermissionType
> {
>    PERMIT, DENY, NOT_APPLICABLE
> }
>
> and PermissionMapper implementation can look like:
>
> public class PermissionMapper
> {
>     @Inject Instance<PermissionResolver> resolvers;
>
>     public boolean resolvePermission(Object resource, String operation)
>     {
>         for (PermissionResolver resolver : resolvers)
>         {
>             PermissionType permissionType = 
> resolver.hasPermission(resource, operation);
>             if (permissionType == PERMIT)
>             {
>               return true;
>             }
>             else if (permissionType == DENY)
>             {
>               return false;
>             }
>         }
>
>         return false;
>     }
> }
>
> And order of PermissionResolvers inside PermissionMapper can be 
> ordered according to some priorities. This design may be more 
> flexible. For example, it will allow you to quickly or temporarily 
> revoke all permissions for particular user.

This is also a good use case.  We would need to iterate through all 
PermissionResolvers though and if any of them returned a DENY then our 
permission check would return a final result of false.

>
>>
>> We would provide one PermissionResolver implementation out of the box 
>> with DeltaSpike; PersistentPermissionResolver would provide 
>> permission checks for ACL-style object permissions and provide a key 
>> piece of functionality required by a complete permission management 
>> API.  Developers can easily provide their own PermissionResolver 
>> implementations with custom business logic by simply deploying a 
>> PermissionResolver bean in their own application.
>>
>> This pretty much covers the basics of the object permission API, at 
>> least on the consuming side.  We can discuss this area first before 
>> moving onto the permission management API shortly.
>>
>>
>


Re: [DISCUSS] DELTASPIKE-79 Authorization API

Posted by Marek Posolda <mp...@redhat.com>.
Hi all,

I am Marek Posolda and i am working on GateIn portal project together 
wth Bolek Dawidowicz. Some of my notes inline:

On 18.4.2012 00:52, Shane Bryzak wrote:
> I'd like to kick off a discussion around the Authorization API, 
> specifically object permissions.  This API is used to determine 
> whether the currently authenticated user has the necessary privileges 
> to perform an operation on a particular domain object.  I'll start by 
> outlining my proposal covering the simple use cases of this API and we 
> can then proceed from there.
>
> First of all, the developer needs a gateway into the permission API so 
> that they can perform permission checks within their own application.  
> This is provided by the hasPermission() method:
>
>
> Identity.hasPermission(Object resource, String operation)
How about having additional method like:


Identity.hasPermission(String resourceType, Serializable resourceId, 
String operation);

This have one advantage that you don't need to obtain instance of Object 
from DB when you have only ID of this resource. Use case example can be:

public Customer loadCustomer(Long customerId)
{
   if (!identity.hasPermission("org.myorganization.Customer", 
customerId, "READ")
   {
     throw new AuthorizationException("Insufficient privileges to load 
this customer");
   }

   // snip code
}

>
>
> A permission has three aspects; 1) The application resource for which 
> the permission is granted, 2) The operation that has been granted for 
> that resource, and 3) The recipient of the permission, which may be 
> either a User, Group or Role.
>
> For example, if we wish to check whether the user has permission to 
> edit a Customer instance the code might look like this:
>
> @Inject Identity identity;
>
> public void editCustomer(Customer customer)
> {
>     if (!identity.hasPermission(customer, "EDIT"))
>     {
>         throw new AuthorizationException("Insufficient privileges to 
> edit this customer");
>     }
>
>     // snip code
> }
>
> We could potentially also do some clever stuff with method-level 
> annotations here.  Off the top of my head, something like this might 
> work:
>
> @CheckPermissions
> public void editCustomer(@CheckPermission("EDIT") Customer customer)
>
> The @CheckPermissions annotation would be a security binding type with 
> a matching authorizer that would scan the parameter list and perform 
> any checks on parameters annotated with @CheckPermission.  Anyway we 
> can refine this idea in ongoing discussions.
>
> In the default implementation, the Identity.hasPermission() method 
> essentially contains no code, instead it delegates the permission 
> check to the PermissionMapper bean, an implementation-only bean which 
> contains a list of PermissionResolver instances that are used to 
> perform the permission check.
>
> public class DefaultIdentity implements Identity
> {
>     // snip code
>
>     public boolean hasPermission(Object resource, String operation)
>     {
>         return permissionMapper.resolvePermission(resource, operation);
>     }
> }
>
> The PermissionMapper bean provides the resolvePermission() method, 
> which basically iterates through the known PermissionResolvers, and if 
> one of them returns true for the permission check, then a true result 
> is returned.
>
> public class PermissionMapper
> {
>     @Inject Instance<PermissionResolver> resolvers;
>
>     public boolean resolvePermission(Object resource, String operation)
>     {
>         for (PermissionResolver resolver : resolvers)
>         {
>             if (resolver.hasPermission(resource, operation)) return true;
>         }
>         return false;
>     }
> }
>
> We can do some clever stuff here, like caching which permission 
> resolver returns a true result for a particular class of resource, and 
> then always using that resolver for that class, etc.
>
> PermissionResolver is an API interface, the implementations of which 
> do the actual work of checking the permission.
>
> public interface PermissionResolver
> {
>     boolean hasPermission(Object resource, String operation);
> }
I think that it might be useful if PermissionResolver can also deny 
permissions? So it may look like:

public interface PermissionResolver
{
    PermissionType hasPermission(Object resource, String operation);
}

where PermissionType can be:

public enum PermissionType
{
    PERMIT, DENY, NOT_APPLICABLE
}

and PermissionMapper implementation can look like:

public class PermissionMapper
{
     @Inject Instance<PermissionResolver> resolvers;

     public boolean resolvePermission(Object resource, String operation)
     {
         for (PermissionResolver resolver : resolvers)
         {
             PermissionType permissionType = 
resolver.hasPermission(resource, operation);
             if (permissionType == PERMIT)
             {
               return true;
             }
             else if (permissionType == DENY)
             {
               return false;
             }
         }

         return false;
     }
}

And order of PermissionResolvers inside PermissionMapper can be ordered 
according to some priorities. This design may be more flexible. For 
example, it will allow you to quickly or temporarily revoke all 
permissions for particular user.

>
> We would provide one PermissionResolver implementation out of the box 
> with DeltaSpike; PersistentPermissionResolver would provide permission 
> checks for ACL-style object permissions and provide a key piece of 
> functionality required by a complete permission management API.  
> Developers can easily provide their own PermissionResolver 
> implementations with custom business logic by simply deploying a 
> PermissionResolver bean in their own application.
>
> This pretty much covers the basics of the object permission API, at 
> least on the consuming side.  We can discuss this area first before 
> moving onto the permission management API shortly.
>
>