Catalyst::Authentication::Credential::HTTP - HTTP Basic and Digest authentication for Catalyst.
use Catalyst qw/ Authentication /; __PACKAGE__->config( authentication => { default_realm => 'example', realms => { example => { credential => { class => 'HTTP', type => 'any', # or 'digest' or 'basic' password_type => 'clear', password_field => 'password' }, store => { class => 'Minimal', users => { Mufasa => { password => "Circle Of Life", }, }, }, }, } }); sub foo : Local { my ( $self, $c ) = @_; $c->authenticate({ realm => "example" }); # either user gets authenticated or 401 is sent # Note that the authentication realm sent to the client (in the # RFC 2617 sense) is overridden here, but this *does not* # effect the Catalyst::Authentication::Realm used for # authentication - to do that, you need # $c->authenticate({}, 'otherrealm') do_stuff(); } sub always_auth : Local { my ( $self, $c ) = @_; # Force authorization headers onto the response so that the user # is asked again for authentication, even if they successfully # authenticated. my $realm = $c->get_auth_realm('example'); $realm->credential->authorization_required_response($c, $realm); } # with ACL plugin __PACKAGE__->deny_access_unless("/path", sub { $_[0]->authenticate });
This module lets you use HTTP authentication with Catalyst::Plugin::Authentication. Both basic and digest authentication are currently supported.
When authentication is required, this module sets a status of 401, and the body of the response to 'Authorization required.'. To override this and set your own content, check for the $c->res->status == 401 in your end action, and change the body accordingly.
$c->res->status == 401
end
A nonce is a one-time value sent with each digest authentication request header. The value must always be unique, so per default the last value of the nonce is kept using Catalyst::Plugin::Cache. To change this behaviour, override the store_digest_authorization_nonce and get_digest_authorization_nonce methods as shown below.
store_digest_authorization_nonce
get_digest_authorization_nonce
Simple constructor.
Validates that $config is ok.
Tries to authenticate the user, and if that fails calls authorization_required_response and detaches the current action call stack.
authorization_required_response
Looks inside $c->request->headers and processes the digest and basic (badly named) authorization header.
$c->request->headers
This will only try the methods set in the configuration. First digest, then basic.
The %auth_info hash can contain a number of keys which control the authentication behaviour:
Sets the HTTP authentication realm presented to the client. Note this does not alter the Catalyst::Authentication::Realm object used for the authentication.
Array reference to domains used to build the authorization headers.
This list of domains defines the protection space. If a domain URI is an absolute path (starts with /), it is relative to the root URL of the server being accessed. An absolute URI in this list may refer to a different server than the one being accessed.
The client will use this list to determine the set of URIs for which the same authentication information may be sent.
If this is omitted or its value is empty, the client will assume that the protection space consists of all URIs on the responding server.
Therefore, if your application is not hosted at the root of this domain, and you want to prevent the authentication credentials for this application being sent to any other applications. then you should use the use_uri_for configuration option, and pass a domain of /.
Performs HTTP basic authentication.
Performs HTTP digest authentication. Note that the password_type must by clear for digest authentication to succeed, and you must have Catalyst::Plugin::Session in your application as digest authentication needs to store persistent data.
Note - if you do not want to store your user passwords as clear text, then it is possible to store instead the MD5 digest in hex of the string '$username:$realm:$password'
Takes an additional parameter of algorithm, the possible values of which are 'MD5' (the default) and 'MD5-sess'. For more information about 'MD5-sess', see section 3.2.2.2 in RFC 2617.
Sets $c->response to the correct status code, and adds the correct header to demand authentication data from the user agent.
$c->response
Typically used by authenticate, but may be invoked manually.
authenticate
%opts can contain domain and algorithm, which are used to build %the digest header.
domain
algorithm
Set or get the $nonce object used by the digest auth mode.
$nonce
You may override these methods. By default they will call get and set on $c->cache.
get
set
$c->cache
All configuration is stored in YourApp->config(authentication => { yourrealm => { credential => { class => 'HTTP', %config } } }.
YourApp->config(authentication => { yourrealm => { credential => { class => 'HTTP', %config } } }
This should be a hash, and it can contain the following entries:
Can be either any (the default), basic or digest.
any
basic
digest
This controls authorization_required_response and authenticate, but not the "manual" methods.
Set this to a string to override the default body content "Authorization required.", or set to undef to suppress body content being generated.
The type of password returned by the user object. Same usage as in Catalyst::Authentication::Credential::Password
The name of accessor used to retrieve the value of the password field from the user object. Same usage as in Catalyst::Authentication::Credential::Password
The field name that the user's username is mapped into when finding the user from the realm. Defaults to 'username'.
If this configuration key has a true value, then the domain(s) for the authorization header will be run through $c->uri_for(). Use this configuration option if your application is not running at the root of your domain, and you want to ensure that authentication credentials from your application are not shared with other applications on the same server.
When using digest authentication, this module will only work together with authentication stores whose User objects have a password method that returns the plain-text password. It will not work together with Catalyst::Authentication::Store::Htpasswd, or Catalyst::Authentication::Store::DBIC stores whose password methods return a hashed or salted version of the password.
password
Updated to current name space and currently maintained by: Tomas Doran bobtfish@bobtfish.net.
bobtfish@bobtfish.net
Original module by:
nothingmuch@woobling.org
esskar@cpan.org
Patches contributed by:
dhoss@cpan.org
RFC 2617 (or its successors), Catalyst::Plugin::Cache, Catalyst::Plugin::Authentication
Copyright (c) 2005-2008 the aforementioned authors. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Catalyst::Authentication::Credential::HTTP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Authentication::Credential::HTTP
CPAN shell
perl -MCPAN -e shell install Catalyst::Authentication::Credential::HTTP
For more information on module installation, please visit the detailed CPAN module installation guide.