You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by "David G." <da...@gmail.com> on 2012/03/27 16:40:42 UTC

Sling Auth Handlers - Best practices/conventions

Is it customary for a Sling Auth Handler's extractCredentials(..) method to
be responsible for

1) validating human-supplied credentials (username, password)
2) and issuing a authentication token (ex. a secure auth Cookie)

My understanding was a Auth Handler merely authenticates credentials, and
generally doesnt actively issue tokens.

An example flow might be:

1) User POSTs username/password to servlet.
2) Servlet authenticates credentials (via whatever means necessary, may
need to reach out to 3rd party system)
3) Servlet adds a secure auth cookie to response object
4) Servlet redirects to resource /page/b.html
5) Authentication Handler matches on /page/b, extracts the secure auth
cookie, validates, and "logs" the user in for that request

This makes sense to me, but
org.apache.sling.commons.auth.Authenticator.login(request, response) seems
to imply they can/should be used to authenticate requests that come in as
unauthenticated, and the servlet then issues an auth request. I believe a
sample flow would be:

1) User POSTs username/password to servlet.
2) Servlet authenticates credentials (via whatever means necessary, may
need to reach out to 3rd party system)
3) Servlet adds a secure auth cookie to response object
*4) Servlet calls Authenticator.login(..)*
*5) Servlet then can perform actions as the new user (not-anonymous anymore)
*4) Servlet redirects to resource /page/b.html
5) Authentication Handler matches on /page/b, extracts the secure auth
cookie, validates, and "logs" the user in for that request

Should a Sling Auth Handler ever be used to handle Steps 2 and 3,
effectively having two Sling Auth Handlers: One to authenticate the human
supplied username/password and to issue a secure auth cookie, and Another
to authenticate the secure auth cookie on subsequent requests?

Re: Sling Auth Handlers - Best practices/conventions

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Am 27.03.2012 um 11:56 schrieb David G.:

> Hi Felix - thanks for the info. have a few more Qs inline.
> 
> On Tue, Mar 27, 2012 at 11:18 AM, Felix Meschberger <fm...@adobe.com>wrote:
> 
>> 
>>> My understanding was a Auth Handler merely authenticates credentials, and
>>> generally doesnt actively issue tokens.
>>> 
>>> An example flow might be:
>>> 
>>> 1) User POSTs username/password to servlet.
>> 
>> There is no servlet involved in the authentication process (other than for
>> providing the HTML form for the user to enter user name and password)
> 
> 
> The Login Form has to POST to *something* .. what does it point at?

If you do form based authentication, you will also  have an authentication handler. The form and the handler will agree on the URL. Generally its just an URL with /j_security_check appended (as stipulated by the Servlet Spec). See for example the Sling Form Authentication Handler.

> 
> 
>>> 2) Servlet authenticates credentials (via whatever means necessary, may
>>> need to reach out to 3rd party system)
>>> 3) Servlet adds a secure auth cookie to response object
>>> 4) Servlet redirects to resource /page/b.html
>>> 5) Authentication Handler matches on /page/b, extracts the secure auth
>>> cookie, validates, and "logs" the user in for that request
>> 
>> What's happening is:
>> 
>> 1. client sends request with username and password
> 
> 2. SlingAuthenticator calls AuthenticationHandler
>> 
> 
> So there you could have 2 auth handlers: 1 whose extractCreds looks for
> POST params username/password and 2nd who looks for the existance of a
> Cookie. Or optionally combine them into a single AuthHandler.

This can be the same.

> 
> 3. AuthenticationHandler returns AuthenticationInfo with username and
>> password
> 
> 4. SlingAuthenticator calls RepositoryFactory with AuthenticationInfo to
>> get resource resolver and validate the credentials
>> 
> 
> If you want to do something "special" during  validation (such as reach out
> to a 3rd party system and check some state or send to a registration page
> if the username doesnt exist in the user list) -- where would this come
> into play?

Unless this is being done as part of the Jackrabbit LoginModule, I would probably do this as part of the extractCredentials step. For example in the OpenID case, you redirect the user to the OpenID provider and only if the identity is validated you will check whether a corresponding user exists in the repository and create it if not.

> 
> 
>> 5. SlingAuthenticator calls
>> AuthenticationFeedbackHandler.authenticationSucceeded which may set cookies
>> 6. request continues to be processed (or is redirected)
>> 
> 
> Where/when/why would Authenticator.login(request, response) get called.
> This seems to be be a mechanism to be called from a servlet (or someplace
> other than the sling auth "ecosystem")

This is a helper service for servlets deciding to require authentication. So they call login to start the authentication process. 

Regards
Felix

Re: Sling Auth Handlers - Best practices/conventions

Posted by "David G." <da...@gmail.com>.
Hi Felix - thanks for the info. have a few more Qs inline.

On Tue, Mar 27, 2012 at 11:18 AM, Felix Meschberger <fm...@adobe.com>wrote:

> Hi,
>
> Am 27.03.2012 um 10:40 schrieb David G.:
>
> > Is it customary for a Sling Auth Handler's extractCredentials(..) method
> to
> > be responsible for
> >
> > 1) validating human-supplied credentials (username, password)
> > 2) and issuing a authentication token (ex. a secure auth Cookie)
>
> The extractCredentials method does neither of those tasks. That method
> just extracts the credentials from the request and created an
> AuthenticationInfo object which is later used by the Sling Authenticator to
> authenticate against the repository.
>
> When authenticationis successfull, the (optional) authenticationSucceeded
> method is called at which time any auth cookie can be created. This method
> is defined by the AuthenticationFeedbackHandler interface. If you
> AuthenticationHandler implements this interface it is called back.
>
> Please refer to http://sling.apache.org/site/authentication.html for full
> details.
>
>
This makes sense and is inline w my understanding. Thanks for the info on
the authenticationSucceeded()

>
> > My understanding was a Auth Handler merely authenticates credentials, and
> > generally doesnt actively issue tokens.
> >
> > An example flow might be:
> >
> > 1) User POSTs username/password to servlet.
>
> There is no servlet involved in the authentication process (other than for
> providing the HTML form for the user to enter user name and password)


The Login Form has to POST to *something* .. what does it point at?


> > 2) Servlet authenticates credentials (via whatever means necessary, may
> > need to reach out to 3rd party system)
> > 3) Servlet adds a secure auth cookie to response object
> > 4) Servlet redirects to resource /page/b.html
> > 5) Authentication Handler matches on /page/b, extracts the secure auth
> > cookie, validates, and "logs" the user in for that request
>
> What's happening is:
>
> 1. client sends request with username and password

2. SlingAuthenticator calls AuthenticationHandler
>

So there you could have 2 auth handlers: 1 whose extractCreds looks for
POST params username/password and 2nd who looks for the existance of a
Cookie. Or optionally combine them into a single AuthHandler.

3. AuthenticationHandler returns AuthenticationInfo with username and
> password

4. SlingAuthenticator calls RepositoryFactory with AuthenticationInfo to
> get resource resolver and validate the credentials
>

If you want to do something "special" during  validation (such as reach out
to a 3rd party system and check some state or send to a registration page
if the username doesnt exist in the user list) -- where would this come
into play?


> 5. SlingAuthenticator calls
> AuthenticationFeedbackHandler.authenticationSucceeded which may set cookies
> 6. request continues to be processed (or is redirected)
>

Where/when/why would Authenticator.login(request, response) get called.
This seems to be be a mechanism to be called from a servlet (or someplace
other than the sling auth "ecosystem")

>

In subsequent requests, the AuthenticationHandler will in step 2 create an
> AuthenticationInfo from the cookie which has been set in step 5. But the
> rest of the processing will remain the same.
>

Yup - makes perfect sense.

>
> Important is, that this all takes place before any servlet is ever hit.
>

Yup.


> Hope this helps.
>
> Regards
> Felix
>
> >
> > This makes sense to me, but
> > org.apache.sling.commons.auth.Authenticator.login(request, response)
> seems
> > to imply they can/should be used to authenticate requests that come in as
> > unauthenticated, and the servlet then issues an auth request. I believe a
> > sample flow would be:
> >
> > 1) User POSTs username/password to servlet.
> > 2) Servlet authenticates credentials (via whatever means necessary, may
> > need to reach out to 3rd party system)
> > 3) Servlet adds a secure auth cookie to response object
> > *4) Servlet calls Authenticator.login(..)*
> > *5) Servlet then can perform actions as the new user (not-anonymous
> anymore)
> > *4) Servlet redirects to resource /page/b.html
> > 5) Authentication Handler matches on /page/b, extracts the secure auth
> > cookie, validates, and "logs" the user in for that request
> >
> > Should a Sling Auth Handler ever be used to handle Steps 2 and 3,
> > effectively having two Sling Auth Handlers: One to authenticate the human
> > supplied username/password and to issue a secure auth cookie, and Another
> > to authenticate the secure auth cookie on subsequent requests?
>
>

Re: Sling Auth Handlers - Best practices/conventions

Posted by Felix Meschberger <fm...@adobe.com>.
Hi,

Am 27.03.2012 um 10:40 schrieb David G.:

> Is it customary for a Sling Auth Handler's extractCredentials(..) method to
> be responsible for
> 
> 1) validating human-supplied credentials (username, password)
> 2) and issuing a authentication token (ex. a secure auth Cookie)

The extractCredentials method does neither of those tasks. That method just extracts the credentials from the request and created an AuthenticationInfo object which is later used by the Sling Authenticator to authenticate against the repository.

When authenticationis successfull, the (optional) authenticationSucceeded method is called at which time any auth cookie can be created. This method is defined by the AuthenticationFeedbackHandler interface. If you AuthenticationHandler implements this interface it is called back.

Please refer to http://sling.apache.org/site/authentication.html for full details.


> 
> My understanding was a Auth Handler merely authenticates credentials, and
> generally doesnt actively issue tokens.
> 
> An example flow might be:
> 
> 1) User POSTs username/password to servlet.

There is no servlet involved in the authentication process (other than for providing the HTML form for the user to enter user name and password).

> 2) Servlet authenticates credentials (via whatever means necessary, may
> need to reach out to 3rd party system)
> 3) Servlet adds a secure auth cookie to response object
> 4) Servlet redirects to resource /page/b.html
> 5) Authentication Handler matches on /page/b, extracts the secure auth
> cookie, validates, and "logs" the user in for that request

What's happening is:

1. client sends request with username and password
2. SlingAuthenticator calls AuthenticationHandler
3. AuthenticationHandler returns AuthenticationInfo with username and password
4. SlingAuthenticator calls RepositoryFactory with AuthenticationInfo to get resource resolver and validate the credentials
5. SlingAuthenticator calls AuthenticationFeedbackHandler.authenticationSucceeded which may set cookies
6. request continues to be processed (or is redirected)

In subsequent requests, the AuthenticationHandler will in step 2 create an AuthenticationInfo from the cookie which has been set in step 5. But the rest of the processing will remain the same.

Important is, that this all takes place before any servlet is ever hit.

Hope this helps.

Regards
Felix

> 
> This makes sense to me, but
> org.apache.sling.commons.auth.Authenticator.login(request, response) seems
> to imply they can/should be used to authenticate requests that come in as
> unauthenticated, and the servlet then issues an auth request. I believe a
> sample flow would be:
> 
> 1) User POSTs username/password to servlet.
> 2) Servlet authenticates credentials (via whatever means necessary, may
> need to reach out to 3rd party system)
> 3) Servlet adds a secure auth cookie to response object
> *4) Servlet calls Authenticator.login(..)*
> *5) Servlet then can perform actions as the new user (not-anonymous anymore)
> *4) Servlet redirects to resource /page/b.html
> 5) Authentication Handler matches on /page/b, extracts the secure auth
> cookie, validates, and "logs" the user in for that request
> 
> Should a Sling Auth Handler ever be used to handle Steps 2 and 3,
> effectively having two Sling Auth Handlers: One to authenticate the human
> supplied username/password and to issue a secure auth cookie, and Another
> to authenticate the secure auth cookie on subsequent requests?