You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by nmetzger <nm...@odu.edu> on 2010/06/22 14:14:43 UTC

authentication as user and admin

Hi all,

my scenario is the following: I have a user that logs into my application
with a regular username/password combination. This user has permission to
view the admin pages of the application. Once he clicks on a link to an
admin page, he is prompted for his admin username and password. Upon
authentication he can access all admin pages without ever having to sign in
as an admin again. 

Is there any way to store this information in a Subject?  

Thanks,
Natalie
-- 
View this message in context: http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5208454.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: authentication as user and admin

Posted by nmetzger <nm...@odu.edu>.
Hi Les,

that sounds good, I'll try it.

Thanks for all your help!!!

Natalie
-- 
View this message in context: http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5221871.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: authentication as user and admin

Posted by Les Hazlewood <lh...@apache.org>.
Oops, I meant:

Subject anotherSubject = Subject.Builder().buildSubject();

The Subject.Builder is covered here:

http://incubator.apache.org/shiro/subject.html

Cheers,

Les

On Tue, Jun 22, 2010 at 6:47 PM, Les Hazlewood <lh...@apache.org>wrote:

> Ah, ok - the way I would do this is to create a new Subject temporarily for
> the 2nd login using the Subject.Builder:
>
> Subject currentSubject = SecurityUtils.getSubject();
>
> Subject anotherSubject = new Subject.Builder();
> anotherSubject.login(new UsernamePasswordToken(adminUsername,
> adminPassword));
> PrincipalCollection adminPrincipals = anotherSubject.getPrincipals();
>
> PrincipalCollection currentPrincipals = currentSubject.getPrincipals();
> ((MutablePrincipalCollection)currentPrincipals).addAll(adminPrincipals);
>
> Also you will probably want 2 Realm instances - one for each database.  A
> normal authentication attempt will try to call both Realms, and use the data
> only from the Realm(s) that succeeds.  This is perfectly fine for most
> apps.
>
> However, if you want to restrict which realm will process each type of
> authentication attempt, you will need to have a custom AuthenticationToken
> subclass with a flag that the Realm can inspect.  Then the 'supports'
> implementation on your Realm can return true or false if it wants to process
> the token or not.  For example:
>
> e.g. MyRealm#supports(AuthenticationToken token) {
>     return token instanceof MyAuthenticationToken &&
> ((MyAuthenticationToken)token).getDatasourceName().equals(getDatasourceName())
> }
>
> If you don't want to do this, or if you want even more control over exactly
> how the authentication attempt takes place, you can implement your own
> AuthenticationStrategy and plug that in to the security manager
> configuration (e.g. via INI):
>
> securityManager.authenticator.authenticationStrategy =
> com.whatever.MyAuthenticationStrategy
>
> The default strategy is the AtLeastOneSuccessfulStrategy.
>
> Best,
>
> Les
>
> On Tue, Jun 22, 2010 at 5:34 PM, nmetzger <nm...@odu.edu> wrote:
>
>>
>> Hi Les,
>>
>> Thanks for replying so quickly.
>>
>> In my application a user  with admin permissions logs into the system that
>> is connected to database number 1. If the same user needs to access the
>> admin functionality, he needs to log into database number 2 to do admin
>> tasks.
>> I guess I'm just looking for the most elegant solution, trying not to
>> reinvent the wheel.
>>
>> Natalie
>>
>>
>> --
>> View this message in context:
>> http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5211341.html
>> Sent from the Shiro User mailing list archive at Nabble.com.
>>
>
>

Re: authentication as user and admin

Posted by Les Hazlewood <lh...@apache.org>.
Ah, ok - the way I would do this is to create a new Subject temporarily for
the 2nd login using the Subject.Builder:

Subject currentSubject = SecurityUtils.getSubject();

Subject anotherSubject = new Subject.Builder();
anotherSubject.login(new UsernamePasswordToken(adminUsername,
adminPassword));
PrincipalCollection adminPrincipals = anotherSubject.getPrincipals();

PrincipalCollection currentPrincipals = currentSubject.getPrincipals();
((MutablePrincipalCollection)currentPrincipals).addAll(adminPrincipals);

Also you will probably want 2 Realm instances - one for each database.  A
normal authentication attempt will try to call both Realms, and use the data
only from the Realm(s) that succeeds.  This is perfectly fine for most
apps.

However, if you want to restrict which realm will process each type of
authentication attempt, you will need to have a custom AuthenticationToken
subclass with a flag that the Realm can inspect.  Then the 'supports'
implementation on your Realm can return true or false if it wants to process
the token or not.  For example:

e.g. MyRealm#supports(AuthenticationToken token) {
    return token instanceof MyAuthenticationToken &&
((MyAuthenticationToken)token).getDatasourceName().equals(getDatasourceName())
}

If you don't want to do this, or if you want even more control over exactly
how the authentication attempt takes place, you can implement your own
AuthenticationStrategy and plug that in to the security manager
configuration (e.g. via INI):

securityManager.authenticator.authenticationStrategy =
com.whatever.MyAuthenticationStrategy

The default strategy is the AtLeastOneSuccessfulStrategy.

Best,

Les

On Tue, Jun 22, 2010 at 5:34 PM, nmetzger <nm...@odu.edu> wrote:

>
> Hi Les,
>
> Thanks for replying so quickly.
>
> In my application a user  with admin permissions logs into the system that
> is connected to database number 1. If the same user needs to access the
> admin functionality, he needs to log into database number 2 to do admin
> tasks.
> I guess I'm just looking for the most elegant solution, trying not to
> reinvent the wheel.
>
> Natalie
>
>
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5211341.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>

Re: authentication as user and admin

Posted by nmetzger <nm...@odu.edu>.
Hi Les,

Thanks for replying so quickly. 

In my application a user  with admin permissions logs into the system that
is connected to database number 1. If the same user needs to access the
admin functionality, he needs to log into database number 2 to do admin
tasks.
I guess I'm just looking for the most elegant solution, trying not to
reinvent the wheel.

Natalie 


-- 
View this message in context: http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5211341.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: authentication as user and admin

Posted by Les Hazlewood <lh...@apache.org>.
RunAs retains the original identity.  If you are 'running as' another
identity, you can get the previous identity by calling
Subject.getPreviousPrincipals()

Note that this will only give you the identity.  It will not make the
current runAs Subject 'act' as the previous user also (i.e. any
subject.isPermitted and hasRole calls will reflect the current runAs user's
state - not a blended view of both the runAs user and the previous
'original' user).

If this still doesn't meet your needs, have you evaluated the possibility of
the security model changing a bit to support this a little cleaner (if
possible)?  It is very odd (at least in my experience) to have a user log in
and then have to log in again to the same application with a different
account in the same session.  The requirements you hint at are almost always
handled with permission and role mappings - not with switching or merging
individual user accounts.

'Run As' as a feature is great for scenarios where an admin needs to assume
the identity of a normal user so they can experience the same thing as the
target user as if they had logged in for themselves - it helps w/ customer
support or debugging, etc.

I don't know the ins and outs of your application obviously, but it's food
for thought - only trying to help.

Les

On Tue, Jun 22, 2010 at 10:19 AM, nmetzger <nm...@odu.edu> wrote:

>
> Hi Les,
>
> I looked into both those options, but they don't seem to fit my
> requirements: In both cases the subject doesn't retain the original
> permissions, and I need those. Or did I overlook something in "runAs"? When
> I tried it, all my permissions were gone.
>
> Thanks for you help,
> Natalie
>
>
> Les Hazlewood-2 wrote:
> >
> > Hi Natalie,
> >
> > You can do this in a couple of ways:
> >
> > 1.  Use Shiro's 'runAs' feature (Subject.runAs*) to 'run as' the Admin
> > user
> > after they give the correct username/password).
> > 2.  Just call subject.login again with the admin principals and
> > credentials.
> >  Note that with this approach, the subject will 'lose' the previous
> > subject
> > identity.
> >
> > HTH,
> >
> > Les
> >
> >
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5209897.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>

Re: authentication as user and admin

Posted by nmetzger <nm...@odu.edu>.
Hi Les,

I looked into both those options, but they don't seem to fit my
requirements: In both cases the subject doesn't retain the original
permissions, and I need those. Or did I overlook something in "runAs"? When
I tried it, all my permissions were gone.

Thanks for you help,
Natalie


Les Hazlewood-2 wrote:
> 
> Hi Natalie,
> 
> You can do this in a couple of ways:
> 
> 1.  Use Shiro's 'runAs' feature (Subject.runAs*) to 'run as' the Admin
> user
> after they give the correct username/password).
> 2.  Just call subject.login again with the admin principals and
> credentials.
>  Note that with this approach, the subject will 'lose' the previous
> subject
> identity.
> 
> HTH,
> 
> Les
> 
> 
-- 
View this message in context: http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5209897.html
Sent from the Shiro User mailing list archive at Nabble.com.

Re: authentication as user and admin

Posted by Les Hazlewood <lh...@apache.org>.
Hi Natalie,

You can do this in a couple of ways:

1.  Use Shiro's 'runAs' feature (Subject.runAs*) to 'run as' the Admin user
after they give the correct username/password).
2.  Just call subject.login again with the admin principals and credentials.
 Note that with this approach, the subject will 'lose' the previous subject
identity.

HTH,

Les

On Tue, Jun 22, 2010 at 5:14 AM, nmetzger <nm...@odu.edu> wrote:

>
> Hi all,
>
> my scenario is the following: I have a user that logs into my application
> with a regular username/password combination. This user has permission to
> view the admin pages of the application. Once he clicks on a link to an
> admin page, he is prompted for his admin username and password. Upon
> authentication he can access all admin pages without ever having to sign in
> as an admin again.
>
> Is there any way to store this information in a Subject?
>
> Thanks,
> Natalie
> --
> View this message in context:
> http://shiro-user.582556.n2.nabble.com/authentication-as-user-and-admin-tp5208454p5208454.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>