You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Saminda Abeyruwan <sa...@opensource.lk> on 2006/09/13 06:38:53 UTC

Automatic switch between Basic Auth and NTLM Auth

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Devs,

I'm Saminda Abeyruwan, Axis2 committer, design and contributed to the
http/https transport in Axis2. In the process I've been able to
contribute to Basic and NTLM Auth based on the sample you guys have put
on the site. But we do (Axis2) handle Basic and NTLM separately. Thus,
we configure httpclient manually to do basic or NTLM auth. IMHO This is
wrong. I've been found that this switch is automatically handled by
commons-httpclient-3.0.

I would kindly asked, how could i write a codebase to support auth
without knowing what auth mechanism commons-httpclient uses. I tried
mayer times to grasp this idea and did some research. But i still need
some help. Commons-httpclient is a powerful tool, and Axis2 really want
to explore its capacity.

Please do help me on this.

Looking forward to hearing from you.

Saminda

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFB4tcYmklbLuW6wYRAma5AJ98rt7t5dkFisTKNIqNkjOBZBIoqgCgxw1e
N9AB7MzrymdcX2hgDL3DYsc=
=pu7A
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2006-09-14 at 11:49 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
...
> 
> Hi Oleg,
> 
> Thank you for the response. You are absolutely right. Axis2 should
> change the "realm" in NTC to "domain" and should keep the "realm" in
> Authscope.
> 
> As a separate question, if Axis2 client uses NTC as credentials, and if
> the server it connects give a challenge as Digest, it will still work
> right.
> 

Absolutely.

Oleg

> Saminda
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Saminda Abeyruwan <sa...@opensource.lk>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oleg Kalnichevski wrote:
> On Wed, 2006-09-13 at 22:38 +0530, Saminda Abeyruwan wrote:
> Oleg Kalnichevski wrote:
>>>> On Wed, 2006-09-13 at 21:08 +0530, Saminda Abeyruwan wrote:
>>>> Oleg Kalnichevski wrote:
>>>>>>> On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:
>>>>> ...
>>>>>>>> Saminda,
>>>>>>>> This credentials provider will cause HttpClient to enter an infinite
>>>>>>>> loop if the given credentials are not valid for some reason (due to a
>>>>>>>> type, for instance).
>>>>>>>> Essentially all you have to do to get the same net effect is this:
>>>>>>>> String username = "username";
>>>>>>>> String password = "password";
>>>>>>>> String host = "somehost";
>>>>>>>> String realm = "realm";
>>>>>>>> NTCredentials creds = new NTCredentials(
>>>>>>>>   username, password, host, realm);
>>>>>>>> httpclient.getState().setCredentials(
>>>>>>>>  new AuthScope(host, AuthScope.ANY_PORT), 
>>>>>>>>  creds);
>>>>>>>> NTCredentials is a super class of UsernamePasswordCredentials, so the
>>>>>>>> same set of credentials will work with Basic and Digest schemes.
>>>>>>>> Give it a shot.
>>>>>>>> Hope this helps
>>>>>>>> Oleg 
>>>> Hi Oleg,
>>>>
>>>> Thank you very much Oleg for such a quick response. Axis2 team really
>>>> appreciate this aid you have provided.
>>>>
>>>>
>>>>> Any time.
>>>> Since we are using NTCredentials, if the host required would be any host
>>>> and if the realm(domain) would be any, could we just pass null to host
>>>> and domain in NTCredentials object, and in scope it would be AuthScop.ANY.
>>>>
>>>> Please do assist us on this.
>>>>
>>>>
>>>>> No, you can't. The host and the domain attributes are required by the
>>>>> NTLM auth scheme, as they signify which NT domain the user is
>>>>> authenticating against. The AuthScope is used to signify what target
>>>>> hosts a given set of credentials applies to. If you want credentials to
>>>>> apply to any host and any realm, you can set either host or realm (or
>>>>> both) to null.
>>>>> Oleg
> Hi Oleg,
> 
> A quick question, if i use the following
> Credentials creds = new NTCredentials(username, password, host, realm);
>                     httpclien.getState().setCredentials(new
> AuthScope(host, port, realm), creds);
> 
> The *host* and *realm*  in NTC and setCredentials should be same right?
> 
>> Not really. "Realm" in the context of NTCredentials is the NT domain. It
>> is actually unfortunate that Axis2 uses this term referring to NT
>> domains, whereas the NTLM authentication scheme does not support the
>> notion of a realm in terms of RFC 2617. The realm attribute of the
>> NTLMAuthScheme is _always_ null. 
> 
>> So, if you want an AuthScope to apply to NTLM authentication schemes
>> make sure its realm is always set to ANY.
> 
>> Credentials creds = new NTCredentials(username, password, host, realm);
>> httpclien.getState().setCredentials(
>>  new AuthScope(host, port, AuthScope.ANY_REALM), creds);
> 
>> Oleg

Hi Oleg,

Thank you for the response. You are absolutely right. Axis2 should
change the "realm" in NTC to "domain" and should keep the "realm" in
Authscope.

As a separate question, if Axis2 client uses NTC as credentials, and if
the server it connects give a challenge as Digest, it will still work
right.

Saminda




> 
> Saminda
> 
>>>> Saminda
>>>>
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
>>>>>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
>>
- ---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>
>>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCPRUYmklbLuW6wYRAnrsAKC18oRonfaPadJa6/4NZhP36D0huQCfTFGo
J0ol4OZFI1hOBq5xcRvwzsw=
=xGjc
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2006-09-13 at 22:38 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Oleg Kalnichevski wrote:
> > On Wed, 2006-09-13 at 21:08 +0530, Saminda Abeyruwan wrote:
> > Oleg Kalnichevski wrote:
> >>>> On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:
> > 
> >> ...
> > 
> >>>>> Saminda,
> >>>>> This credentials provider will cause HttpClient to enter an infinite
> >>>>> loop if the given credentials are not valid for some reason (due to a
> >>>>> type, for instance).
> >>>>> Essentially all you have to do to get the same net effect is this:
> >>>>> String username = "username";
> >>>>> String password = "password";
> >>>>> String host = "somehost";
> >>>>> String realm = "realm";
> >>>>> NTCredentials creds = new NTCredentials(
> >>>>>   username, password, host, realm);
> >>>>> httpclient.getState().setCredentials(
> >>>>>  new AuthScope(host, AuthScope.ANY_PORT), 
> >>>>>  creds);
> >>>>> NTCredentials is a super class of UsernamePasswordCredentials, so the
> >>>>> same set of credentials will work with Basic and Digest schemes.
> >>>>> Give it a shot.
> >>>>> Hope this helps
> >>>>> Oleg 
> > Hi Oleg,
> > 
> > Thank you very much Oleg for such a quick response. Axis2 team really
> > appreciate this aid you have provided.
> > 
> > 
> >> Any time.
> > 
> > Since we are using NTCredentials, if the host required would be any host
> > and if the realm(domain) would be any, could we just pass null to host
> > and domain in NTCredentials object, and in scope it would be AuthScop.ANY.
> > 
> > Please do assist us on this.
> > 
> > 
> >> No, you can't. The host and the domain attributes are required by the
> >> NTLM auth scheme, as they signify which NT domain the user is
> >> authenticating against. The AuthScope is used to signify what target
> >> hosts a given set of credentials applies to. If you want credentials to
> >> apply to any host and any realm, you can set either host or realm (or
> >> both) to null.
> > 
> >> Oleg
> Hi Oleg,
> 
> A quick question, if i use the following
> Credentials creds = new NTCredentials(username, password, host, realm);
>                     httpclien.getState().setCredentials(new
> AuthScope(host, port, realm), creds);
> 
> The *host* and *realm*  in NTC and setCredentials should be same right?

Not really. "Realm" in the context of NTCredentials is the NT domain. It
is actually unfortunate that Axis2 uses this term referring to NT
domains, whereas the NTLM authentication scheme does not support the
notion of a realm in terms of RFC 2617. The realm attribute of the
NTLMAuthScheme is _always_ null. 

So, if you want an AuthScope to apply to NTLM authentication schemes
make sure its realm is always set to ANY.

Credentials creds = new NTCredentials(username, password, host, realm);
httpclien.getState().setCredentials(
 new AuthScope(host, port, AuthScope.ANY_REALM), creds);

Oleg

> 
> Saminda
> 
> > 
> > Saminda
> > 
> >>>>
> >>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> >>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> > 
> > 
> >>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> >>
> >>
> 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFFCDsAYmklbLuW6wYRAm84AJ9qStVp1d0rOb9PlcpE8GPHx54VtgCeKa+5
> ylQ0NmL7tjU2pwXRxunhsZQ=
> =O6Rr
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Saminda Abeyruwan <sa...@opensource.lk>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oleg Kalnichevski wrote:
> On Wed, 2006-09-13 at 21:08 +0530, Saminda Abeyruwan wrote:
> Oleg Kalnichevski wrote:
>>>> On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:
> 
>> ...
> 
>>>>> Saminda,
>>>>> This credentials provider will cause HttpClient to enter an infinite
>>>>> loop if the given credentials are not valid for some reason (due to a
>>>>> type, for instance).
>>>>> Essentially all you have to do to get the same net effect is this:
>>>>> String username = "username";
>>>>> String password = "password";
>>>>> String host = "somehost";
>>>>> String realm = "realm";
>>>>> NTCredentials creds = new NTCredentials(
>>>>>   username, password, host, realm);
>>>>> httpclient.getState().setCredentials(
>>>>>  new AuthScope(host, AuthScope.ANY_PORT), 
>>>>>  creds);
>>>>> NTCredentials is a super class of UsernamePasswordCredentials, so the
>>>>> same set of credentials will work with Basic and Digest schemes.
>>>>> Give it a shot.
>>>>> Hope this helps
>>>>> Oleg 
> Hi Oleg,
> 
> Thank you very much Oleg for such a quick response. Axis2 team really
> appreciate this aid you have provided.
> 
> 
>> Any time.
> 
> Since we are using NTCredentials, if the host required would be any host
> and if the realm(domain) would be any, could we just pass null to host
> and domain in NTCredentials object, and in scope it would be AuthScop.ANY.
> 
> Please do assist us on this.
> 
> 
>> No, you can't. The host and the domain attributes are required by the
>> NTLM auth scheme, as they signify which NT domain the user is
>> authenticating against. The AuthScope is used to signify what target
>> hosts a given set of credentials applies to. If you want credentials to
>> apply to any host and any realm, you can set either host or realm (or
>> both) to null.
> 
>> Oleg
Hi Oleg,

A quick question, if i use the following
Credentials creds = new NTCredentials(username, password, host, realm);
                    httpclien.getState().setCredentials(new
AuthScope(host, port, realm), creds);

The *host* and *realm*  in NTC and setCredentials should be same right?

Saminda

> 
> Saminda
> 
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
>>
- ---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>
>>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCDsAYmklbLuW6wYRAm84AJ9qStVp1d0rOb9PlcpE8GPHx54VtgCeKa+5
ylQ0NmL7tjU2pwXRxunhsZQ=
=O6Rr
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2006-09-13 at 21:08 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Oleg Kalnichevski wrote:
> > On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:

...

> >> Saminda,
> > 
> >> This credentials provider will cause HttpClient to enter an infinite
> >> loop if the given credentials are not valid for some reason (due to a
> >> type, for instance).
> > 
> >> Essentially all you have to do to get the same net effect is this:
> > 
> >> String username = "username";
> >> String password = "password";
> >> String host = "somehost";
> >> String realm = "realm";
> >> NTCredentials creds = new NTCredentials(
> >>   username, password, host, realm);
> >> httpclient.getState().setCredentials(
> >>  new AuthScope(host, AuthScope.ANY_PORT), 
> >>  creds);
> > 
> >> NTCredentials is a super class of UsernamePasswordCredentials, so the
> >> same set of credentials will work with Basic and Digest schemes.
> > 
> >> Give it a shot.
> > 
> >> Hope this helps
> > 
> >> Oleg 
> 
> Hi Oleg,
> 
> Thank you very much Oleg for such a quick response. Axis2 team really
> appreciate this aid you have provided.
> 

Any time.

> Since we are using NTCredentials, if the host required would be any host
> and if the realm(domain) would be any, could we just pass null to host
> and domain in NTCredentials object, and in scope it would be AuthScop.ANY.
> 
> Please do assist us on this.
> 

No, you can't. The host and the domain attributes are required by the
NTLM auth scheme, as they signify which NT domain the user is
authenticating against. The AuthScope is used to signify what target
hosts a given set of credentials applies to. If you want credentials to
apply to any host and any realm, you can set either host or realm (or
both) to null.

Oleg

> Saminda
> 
> > 
> > 
> >
> 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFFCCXsYmklbLuW6wYRAl1XAJ99aC/ARAcY5qCqADvpQxepldSotgCgpq4l
> Ija/dWhweklFfzMIA02WDdA=
> =/cLi
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Saminda Abeyruwan <sa...@opensource.lk>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oleg Kalnichevski wrote:
> On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:
>> ...
> 
> Hi Oleg
> 
> Thank you very much for the response.
> 
> My custom credential provider is as follows,
> 
> ===========================================================================
> import org.apache.commons.httpclient.auth.*;
> import org.apache.commons.httpclient.Credentials;
> import org.apache.commons.httpclient.NTCredentials;
> import org.apache.commons.httpclient.UsernamePasswordCredentials;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> 
> import java.io.IOException;
> /*
>  *
>  */
> 
> public class HTTPCredentialProvider implements CredentialsProvider {
> 
>     private static Log log =
> LogFactory.getLog(HTTPCredentialProvider.class);
> 
>     private String host;
>     private String realm;
>     private String username;
>     private String password;
> 
> 
>     public HTTPCredentialProvider(String host, String realm, String
> username, String password) {
>         this.host = host;
>         this.realm = realm;
>         this.username = username;
>         this.password = password;
> 
>     }
> 
>     public Credentials getCredentials(AuthScheme authscheme, String
> string, int i, boolean b)
>             throws CredentialsNotAvailableException {
>         if (authscheme == null) {
>             return null;
>         }
>         try {
>             if (authscheme instanceof NTLMScheme) {
>                 log.debug("NTLM Authentication authentication");
>                 if (username == null || password == null || host == null
> || realm == null) {
>                     throw new CredentialsNotAvailableException(
>                             "user or password or host or realm cannot be
> Null");
>                 }
>                 return new NTCredentials(username, password, host, realm);
>             } else if (authscheme instanceof RFC2617Scheme) {
>                 log.debug(host + " : " + " requires authentication with
> the realm '"
>                           + authscheme.getRealm() + "'");
>                 return new UsernamePasswordCredentials(username, password);
>             } else {
>                 throw new CredentialsNotAvailableException("Unsupported
> authentication scheme: " +
> 
> authscheme.getSchemeName());
>             }
>         } catch (IOException e) {
>             throw new CredentialsNotAvailableException(e.getMessage(), e);
>         }
> 
>     }
> 
> }
> ==========================================================================
> Axis2 just need to work with NTLM, Digest and Basic Auth. Would the
> above custom credential class satisfy the necessary requirement as you
> mentioned. Is there any improvement i should do.
> 
> Clients of Axis2, just say "Turn on Auth", and set host,..etc, where
> they set to HTTPCredentialProvider internally and httpclient magically
> do the auth.  Is this the correct way to handle this situation.
> 
> Saminda
> 
>> Saminda,
> 
>> This credentials provider will cause HttpClient to enter an infinite
>> loop if the given credentials are not valid for some reason (due to a
>> type, for instance).
> 
>> Essentially all you have to do to get the same net effect is this:
> 
>> String username = "username";
>> String password = "password";
>> String host = "somehost";
>> String realm = "realm";
>> NTCredentials creds = new NTCredentials(
>>   username, password, host, realm);
>> httpclient.getState().setCredentials(
>>  new AuthScope(host, AuthScope.ANY_PORT), 
>>  creds);
> 
>> NTCredentials is a super class of UsernamePasswordCredentials, so the
>> same set of credentials will work with Basic and Digest schemes.
> 
>> Give it a shot.
> 
>> Hope this helps
> 
>> Oleg 

Hi Oleg,

Thank you very much Oleg for such a quick response. Axis2 team really
appreciate this aid you have provided.

Since we are using NTCredentials, if the host required would be any host
and if the realm(domain) would be any, could we just pass null to host
and domain in NTCredentials object, and in scope it would be AuthScop.ANY.

Please do assist us on this.

Saminda

> 
> 
>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCCXsYmklbLuW6wYRAl1XAJ99aC/ARAcY5qCqADvpQxepldSotgCgpq4l
Ija/dWhweklFfzMIA02WDdA=
=/cLi
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2006-09-13 at 19:23 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----

...

> Hi Oleg
> 
> Thank you very much for the response.
> 
> My custom credential provider is as follows,
> 
> ===========================================================================
> import org.apache.commons.httpclient.auth.*;
> import org.apache.commons.httpclient.Credentials;
> import org.apache.commons.httpclient.NTCredentials;
> import org.apache.commons.httpclient.UsernamePasswordCredentials;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> 
> import java.io.IOException;
> /*
>  *
>  */
> 
> public class HTTPCredentialProvider implements CredentialsProvider {
> 
>     private static Log log =
> LogFactory.getLog(HTTPCredentialProvider.class);
> 
>     private String host;
>     private String realm;
>     private String username;
>     private String password;
> 
> 
>     public HTTPCredentialProvider(String host, String realm, String
> username, String password) {
>         this.host = host;
>         this.realm = realm;
>         this.username = username;
>         this.password = password;
> 
>     }
> 
>     public Credentials getCredentials(AuthScheme authscheme, String
> string, int i, boolean b)
>             throws CredentialsNotAvailableException {
>         if (authscheme == null) {
>             return null;
>         }
>         try {
>             if (authscheme instanceof NTLMScheme) {
>                 log.debug("NTLM Authentication authentication");
>                 if (username == null || password == null || host == null
> || realm == null) {
>                     throw new CredentialsNotAvailableException(
>                             "user or password or host or realm cannot be
> Null");
>                 }
>                 return new NTCredentials(username, password, host, realm);
>             } else if (authscheme instanceof RFC2617Scheme) {
>                 log.debug(host + " : " + " requires authentication with
> the realm '"
>                           + authscheme.getRealm() + "'");
>                 return new UsernamePasswordCredentials(username, password);
>             } else {
>                 throw new CredentialsNotAvailableException("Unsupported
> authentication scheme: " +
> 
> authscheme.getSchemeName());
>             }
>         } catch (IOException e) {
>             throw new CredentialsNotAvailableException(e.getMessage(), e);
>         }
> 
>     }
> 
> }
> ==========================================================================
> Axis2 just need to work with NTLM, Digest and Basic Auth. Would the
> above custom credential class satisfy the necessary requirement as you
> mentioned. Is there any improvement i should do.
> 
> Clients of Axis2, just say "Turn on Auth", and set host,..etc, where
> they set to HTTPCredentialProvider internally and httpclient magically
> do the auth.  Is this the correct way to handle this situation.
> 
> Saminda

Saminda,

This credentials provider will cause HttpClient to enter an infinite
loop if the given credentials are not valid for some reason (due to a
type, for instance).

Essentially all you have to do to get the same net effect is this:

String username = "username";
String password = "password";
String host = "somehost";
String realm = "realm";
NTCredentials creds = new NTCredentials(
  username, password, host, realm);
httpclient.getState().setCredentials(
 new AuthScope(host, AuthScope.ANY_PORT), 
 creds);

NTCredentials is a super class of UsernamePasswordCredentials, so the
same set of credentials will work with Basic and Digest schemes.

Give it a shot.

Hope this helps

Oleg 


> > 
> >>>>
> >>>> Please do help me on this.
> >>>>
> >>>> Looking forward to hearing from you.
> >>>>
> >>>> Saminda
> >>>>
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> >>>>>
> >>>> ---------------------------------------------------------------------
> >>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> >>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> > 
> > 
> >>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> >>
> >>
> 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFFCA1DYmklbLuW6wYRAhslAJ933HFsDmBHh/85h1H/Tui/+Qb9zwCgtZ4C
> 3W1ENt4dXTEHuRx7x+kRUT0=
> =od2a
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Saminda Abeyruwan <sa...@opensource.lk>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oleg Kalnichevski wrote:
> On Wed, 2006-09-13 at 16:26 +0530, Saminda Abeyruwan wrote:
> Oleg Kalnichevski wrote:
>>>> On Wed, 2006-09-13 at 10:08 +0530, Saminda Abeyruwan wrote:
>>>> Hi Devs,
>>>>
>>>> I'm Saminda Abeyruwan, Axis2 committer, design and contributed to the
>>>> http/https transport in Axis2. In the process I've been able to
>>>> contribute to Basic and NTLM Auth based on the sample you guys have put
>>>> on the site. But we do (Axis2) handle Basic and NTLM separately. Thus,
>>>> we configure httpclient manually to do basic or NTLM auth. IMHO This is
>>>> wrong. I've been found that this switch is automatically handled by
>>>> commons-httpclient-3.0.
>>>>
>>>> I would kindly asked, how could i write a codebase to support auth
>>>> without knowing what auth mechanism commons-httpclient uses. I tried
>>>> mayer times to grasp this idea and did some research. But i still need
>>>> some help. Commons-httpclient is a powerful tool, and Axis2 really want
>>>> to explore its capacity.
>>>>
>>>>
>>>>> Hi Saminda,
>>>>> HttpClient is designed to automatically select an authentication scheme
>>>>> based on the challenge returned by the target server. It is always the
>>>>> server side of HTTP that determines what kind of authentication
>>>>> mechanism should be used. In those cases when the target server supports
>>>>> multiple authentication schemes HttpClient makes an effort to select the
>>>>> most secure one. The default auth scheme order of preference is NTLM,
>>>>> Digest, Basic. In other words per default HttpClient will always pick
>>>>> NTLM over Basic given a choice. One can set a different order of
>>>>> preference if so is desired. For instance, if you want to make sure
>>>>> HttpClient does not use Basic auth at all, just exclude Basic from the
>>>>> order of preference. 
>>>>> If you need more details, just let me know.
>>>>> Hope this helps somewhat.
>>>>> Oleg
> Hi Oleg,
> 
> Thank you for the quick response. Based on the
> InteractiveAuthenticationExample sample, I've written my own credential
> provider to provide either UsernamePasswordCredentials or NTCredentials.
> 
> What would be the based way to set the AuthScop, and where should I set
> it, if I'm using the following.
>     ....
>     HttpClient client = new HttpClient();
>         client.getParams().setParameter(
>             CredentialsProvider.PROVIDER, new MyCredentialProvider());
>         GetMethod httpget = new GetMethod("http://foo.com");
>         httpget.setDoAuthentication(true);
>         try {
>             // execute the GET
>             int status = client.executeMethod(httpget);
>             // print the status and response
>             System.out.println(httpget.getStatusLine().toString());
>             System.out.println(httpget.getResponseBodyAsString());
>         } finally {
>             // release any connection resources used by the method
>             httpget.releaseConnection();
>         }
>       ....
> 
> Thank you
> 
> Saminda
> 
> 
>> Saminda,
> 
>> You no longer have to specify an auth scope when using a custom
>> credentials provider. HttpClient will automatically add credentials to
>> the HttpState based on the authentication context. 
> 
>> Be _EXTRA_ careful when implementing a custom credentials provider,
>> though. HttpClient makes no provisions to check whether the same
>> credentials have been tried already. It is a responsibility of the
>> custom credentials provider to keep track of authentication attempts and
>> to ensure that credentials known to be invalid are not retried. Do make
>> sure that your credentials provider can gracefully handle invalid
>> credentials. 
> 
>> I would go as far as advise against using CredentialProvider interface
>> unless you are not trying to implement an interactive authentication
>> dialog of some sort.
> 
>> Oleg

Hi Oleg

Thank you very much for the response.

My custom credential provider is as follows,

===========================================================================
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
/*
 *
 */

public class HTTPCredentialProvider implements CredentialsProvider {

    private static Log log =
LogFactory.getLog(HTTPCredentialProvider.class);

    private String host;
    private String realm;
    private String username;
    private String password;


    public HTTPCredentialProvider(String host, String realm, String
username, String password) {
        this.host = host;
        this.realm = realm;
        this.username = username;
        this.password = password;

    }

    public Credentials getCredentials(AuthScheme authscheme, String
string, int i, boolean b)
            throws CredentialsNotAvailableException {
        if (authscheme == null) {
            return null;
        }
        try {
            if (authscheme instanceof NTLMScheme) {
                log.debug("NTLM Authentication authentication");
                if (username == null || password == null || host == null
|| realm == null) {
                    throw new CredentialsNotAvailableException(
                            "user or password or host or realm cannot be
Null");
                }
                return new NTCredentials(username, password, host, realm);
            } else if (authscheme instanceof RFC2617Scheme) {
                log.debug(host + " : " + " requires authentication with
the realm '"
                          + authscheme.getRealm() + "'");
                return new UsernamePasswordCredentials(username, password);
            } else {
                throw new CredentialsNotAvailableException("Unsupported
authentication scheme: " +

authscheme.getSchemeName());
            }
        } catch (IOException e) {
            throw new CredentialsNotAvailableException(e.getMessage(), e);
        }

    }

}
==========================================================================
Axis2 just need to work with NTLM, Digest and Basic Auth. Would the
above custom credential class satisfy the necessary requirement as you
mentioned. Is there any improvement i should do.

Clients of Axis2, just say "Turn on Auth", and set host,..etc, where
they set to HTTPCredentialProvider internally and httpclient magically
do the auth.  Is this the correct way to handle this situation.

Saminda
> 
>>>>
>>>> Please do help me on this.
>>>>
>>>> Looking forward to hearing from you.
>>>>
>>>> Saminda
>>>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
>>
- ---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>
>>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFCA1DYmklbLuW6wYRAhslAJ933HFsDmBHh/85h1H/Tui/+Qb9zwCgtZ4C
3W1ENt4dXTEHuRx7x+kRUT0=
=od2a
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2006-09-13 at 16:26 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Oleg Kalnichevski wrote:
> > On Wed, 2006-09-13 at 10:08 +0530, Saminda Abeyruwan wrote:
> > Hi Devs,
> > 
> > I'm Saminda Abeyruwan, Axis2 committer, design and contributed to the
> > http/https transport in Axis2. In the process I've been able to
> > contribute to Basic and NTLM Auth based on the sample you guys have put
> > on the site. But we do (Axis2) handle Basic and NTLM separately. Thus,
> > we configure httpclient manually to do basic or NTLM auth. IMHO This is
> > wrong. I've been found that this switch is automatically handled by
> > commons-httpclient-3.0.
> > 
> > I would kindly asked, how could i write a codebase to support auth
> > without knowing what auth mechanism commons-httpclient uses. I tried
> > mayer times to grasp this idea and did some research. But i still need
> > some help. Commons-httpclient is a powerful tool, and Axis2 really want
> > to explore its capacity.
> > 
> > 
> >> Hi Saminda,
> > 
> >> HttpClient is designed to automatically select an authentication scheme
> >> based on the challenge returned by the target server. It is always the
> >> server side of HTTP that determines what kind of authentication
> >> mechanism should be used. In those cases when the target server supports
> >> multiple authentication schemes HttpClient makes an effort to select the
> >> most secure one. The default auth scheme order of preference is NTLM,
> >> Digest, Basic. In other words per default HttpClient will always pick
> >> NTLM over Basic given a choice. One can set a different order of
> >> preference if so is desired. For instance, if you want to make sure
> >> HttpClient does not use Basic auth at all, just exclude Basic from the
> >> order of preference. 
> > 
> >> If you need more details, just let me know.
> > 
> >> Hope this helps somewhat.
> > 
> >> Oleg
> 
> Hi Oleg,
> 
> Thank you for the quick response. Based on the
> InteractiveAuthenticationExample sample, I've written my own credential
> provider to provide either UsernamePasswordCredentials or NTCredentials.
> 
> What would be the based way to set the AuthScop, and where should I set
> it, if I'm using the following.
>     ....
>     HttpClient client = new HttpClient();
>         client.getParams().setParameter(
>             CredentialsProvider.PROVIDER, new MyCredentialProvider());
>         GetMethod httpget = new GetMethod("http://foo.com");
>         httpget.setDoAuthentication(true);
>         try {
>             // execute the GET
>             int status = client.executeMethod(httpget);
>             // print the status and response
>             System.out.println(httpget.getStatusLine().toString());
>             System.out.println(httpget.getResponseBodyAsString());
>         } finally {
>             // release any connection resources used by the method
>             httpget.releaseConnection();
>         }
>       ....
> 
> Thank you
> 
> Saminda
> 

Saminda,

You no longer have to specify an auth scope when using a custom
credentials provider. HttpClient will automatically add credentials to
the HttpState based on the authentication context. 

Be _EXTRA_ careful when implementing a custom credentials provider,
though. HttpClient makes no provisions to check whether the same
credentials have been tried already. It is a responsibility of the
custom credentials provider to keep track of authentication attempts and
to ensure that credentials known to be invalid are not retried. Do make
sure that your credentials provider can gracefully handle invalid
credentials. 

I would go as far as advise against using CredentialProvider interface
unless you are not trying to implement an interactive authentication
dialog of some sort.

Oleg

> > 
> > 
> > Please do help me on this.
> > 
> > Looking forward to hearing from you.
> > 
> > Saminda
> > 
> >>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> >>
> >>
> 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFFB+PwYmklbLuW6wYRAsFxAJ4hAnjya1b85Pek2ssrYaiiK2EK3gCcDoiO
> l8qonPO9XtzX0IE/6GNGQG4=
> =LkWj
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Saminda Abeyruwan <sa...@opensource.lk>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oleg Kalnichevski wrote:
> On Wed, 2006-09-13 at 10:08 +0530, Saminda Abeyruwan wrote:
> Hi Devs,
> 
> I'm Saminda Abeyruwan, Axis2 committer, design and contributed to the
> http/https transport in Axis2. In the process I've been able to
> contribute to Basic and NTLM Auth based on the sample you guys have put
> on the site. But we do (Axis2) handle Basic and NTLM separately. Thus,
> we configure httpclient manually to do basic or NTLM auth. IMHO This is
> wrong. I've been found that this switch is automatically handled by
> commons-httpclient-3.0.
> 
> I would kindly asked, how could i write a codebase to support auth
> without knowing what auth mechanism commons-httpclient uses. I tried
> mayer times to grasp this idea and did some research. But i still need
> some help. Commons-httpclient is a powerful tool, and Axis2 really want
> to explore its capacity.
> 
> 
>> Hi Saminda,
> 
>> HttpClient is designed to automatically select an authentication scheme
>> based on the challenge returned by the target server. It is always the
>> server side of HTTP that determines what kind of authentication
>> mechanism should be used. In those cases when the target server supports
>> multiple authentication schemes HttpClient makes an effort to select the
>> most secure one. The default auth scheme order of preference is NTLM,
>> Digest, Basic. In other words per default HttpClient will always pick
>> NTLM over Basic given a choice. One can set a different order of
>> preference if so is desired. For instance, if you want to make sure
>> HttpClient does not use Basic auth at all, just exclude Basic from the
>> order of preference. 
> 
>> If you need more details, just let me know.
> 
>> Hope this helps somewhat.
> 
>> Oleg

Hi Oleg,

Thank you for the quick response. Based on the
InteractiveAuthenticationExample sample, I've written my own credential
provider to provide either UsernamePasswordCredentials or NTCredentials.

What would be the based way to set the AuthScop, and where should I set
it, if I'm using the following.
    ....
    HttpClient client = new HttpClient();
        client.getParams().setParameter(
            CredentialsProvider.PROVIDER, new MyCredentialProvider());
        GetMethod httpget = new GetMethod("http://foo.com");
        httpget.setDoAuthentication(true);
        try {
            // execute the GET
            int status = client.executeMethod(httpget);
            // print the status and response
            System.out.println(httpget.getStatusLine().toString());
            System.out.println(httpget.getResponseBodyAsString());
        } finally {
            // release any connection resources used by the method
            httpget.releaseConnection();
        }
      ....

Thank you

Saminda

> 
> 
> Please do help me on this.
> 
> Looking forward to hearing from you.
> 
> Saminda
> 
>>
- ---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
>>
>>

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFB+PwYmklbLuW6wYRAsFxAJ4hAnjya1b85Pek2ssrYaiiK2EK3gCcDoiO
l8qonPO9XtzX0IE/6GNGQG4=
=LkWj
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Re: Automatic switch between Basic Auth and NTLM Auth

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2006-09-13 at 10:08 +0530, Saminda Abeyruwan wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Hi Devs,
> 
> I'm Saminda Abeyruwan, Axis2 committer, design and contributed to the
> http/https transport in Axis2. In the process I've been able to
> contribute to Basic and NTLM Auth based on the sample you guys have put
> on the site. But we do (Axis2) handle Basic and NTLM separately. Thus,
> we configure httpclient manually to do basic or NTLM auth. IMHO This is
> wrong. I've been found that this switch is automatically handled by
> commons-httpclient-3.0.
> 
> I would kindly asked, how could i write a codebase to support auth
> without knowing what auth mechanism commons-httpclient uses. I tried
> mayer times to grasp this idea and did some research. But i still need
> some help. Commons-httpclient is a powerful tool, and Axis2 really want
> to explore its capacity.
> 

Hi Saminda,

HttpClient is designed to automatically select an authentication scheme
based on the challenge returned by the target server. It is always the
server side of HTTP that determines what kind of authentication
mechanism should be used. In those cases when the target server supports
multiple authentication schemes HttpClient makes an effort to select the
most secure one. The default auth scheme order of preference is NTLM,
Digest, Basic. In other words per default HttpClient will always pick
NTLM over Basic given a choice. One can set a different order of
preference if so is desired. For instance, if you want to make sure
HttpClient does not use Basic auth at all, just exclude Basic from the
order of preference. 

If you need more details, just let me know.

Hope this helps somewhat.

Oleg


> Please do help me on this.
> 
> Looking forward to hearing from you.
> 
> Saminda
> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iD8DBQFFB4tcYmklbLuW6wYRAma5AJ98rt7t5dkFisTKNIqNkjOBZBIoqgCgxw1e
> N9AB7MzrymdcX2hgDL3DYsc=
> =pu7A
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org