Hello,
Feedback is requested from the Mech dev list on "if" and "how" we want to
update the basic auth handling.
Below is a submitted proposed patch, followed by some discussion of it.
The spirit of Mech is basically to make LWP more user friendly, which I
believe is the spirit of this patch.
However, I want to make sure our API is designed well the first time
around to be sufficiently flexible. That's why I want your feedback.
Please CC'ing the bugy system on responses as well. Thanks.
Mark
--- Mechanize.pm Sat Oct 2 16:56:45 2004
+++ Mechanize.pm.1 Thu Oct 7 17:31:04 2004
@@ -1293,7 +1293,46 @@
close $fh;
}
+
+=head2 $mech->set_basic_credentials($username, $password)
+
+Allows you to set the username/password for basic authentication.
+
+ $mech->set_basic_credentials('melbrooks','12345');
+ $mech->set_basic_credentials(); #clears the credentials
+
+=cut
+
+sub set_basic_credentials {
+ my ($self, $login, $password) = @_;
+ if ($login and $password) {
+ push @{ $self->{'basic_authentication'}}, $login, $password;
+ } else {
+ delete $self->{'basic_authentication'};
+ }
+}
+
=head1 OVERRIDDEN L<LWP::UserAgent> METHODS
+
+=head2 $mech->get_basic_credentials()
+
+An overloaded version of C<get_basic_credentials()> in L<LWP::UserAgent>.
+This method returns a username and password, if supplied by
+C<set_basic_credentials()>. Normally, you will not need to
+use this function, as it is called by L<LWP::UserAgent> automatically.
+
+=cut
+
+sub get_basic_credentials {
+ my($self, $realm, $uri, $proxy) = @_;
+ return if $proxy;
+
+ if (exists $self->{'basic_authentication'}) {
+ return @{ $self->{'basic_authentication'} };
+ }
+
+ return (undef, undef);
+}
=head2 $mech->redirect_ok()
----- Forwarded message from Guest via RT <bug-WWW-Mechanize@rt.cpan.org> -----
Subject: [cpan #4642] Authentication without realm, server and port
From: Guest via RT <bug-WWW-Mechanize@rt.cpan.org>
Date: Tue, 19 Oct 2004 09:49:58 -0400 (EDT)
To: undisclosed-recipients: ;
This message about WWW-Mechanize was sent to you by guest <> via rt.cpan.org
Full context and any attached attachments can be found at:
<URL:
https://rt.cpan.org/Ticket/Display.html?id=4642 >
[MARKSTOS - Mon Oct 18 20:52:00 2004]:
Show quoted text> Thanks for the patch. Why is this necessary, though? Doesn't work
> well
> enough to just put the username and password in the URL?
>
>
http://user:pass@domain.com
Didn't know that was supported. Perhaps a documentation patch would be
in order. However, that doesn't cache the username/password, so it
would have to be issued on each request.
Show quoted text> Also, without tests, it's not clear that what you are doing will
> actually work. Your use of $self->{'basic_authentication'} is
> different
> than LWP, for example.
That caches the authentication in the Mech object. If I remember
correctly, it's exactly the way it is done in LWP::UserAgent in its
credentials() and get_basic_credentials() methods.
According to the LWP docs, get_basic_credentials() is designed to be
overridden in a subclass, much the way I did. If you think it would be
cleaner to have the new methods invoke SUPER::credentials() instead of
caching in the Mech object then I'll be glad to update the patch.
As far as whether it works, I use this patch in production and it
works fine. Or was that a hint that there should be tests? :-) Testing
basic authentication would require a 'net connection at test time and
a server with basic auth that never changes, wouldn't it? I'll have to
check how LWP does it.
Show quoted text