3 package Catalyst::Plugin::Authentication::Credential::HTTP;
4 use base qw/Catalyst::Plugin::Authentication::Credential::Password/;
12 our $VERSION = "0.01";
14 sub authenticate_http {
17 my $headers = $c->req->headers;
19 if ( my ( $user, $password ) = $headers->authorization_basic ) {
21 if ( my $store = $c->config->{authentication}{http}{store} ) {
22 $user = $store->get_user($user);
25 return $c->login( $user, $password );
29 sub authorization_required {
30 my ( $c, %opts ) = @_;
32 return 1 if $c->authenticate_http;
34 $c->authorization_required_response( %opts );
36 die $Catalyst::DETACH;
39 sub authorization_required_response {
40 my ( $c, %opts ) = @_;
46 if ( my $realm = $opts{realm} ) {
47 push @opts, sprintf 'realm=%s', String::Escape::qprintable($realm);
50 if ( my $domain = $opts{domain} ) {
51 Catalyst::Excpetion->throw("domain must be an array reference")
52 unless ref($domain) && ref($domain) eq "ARRAY";
55 $c->config->{authentication}{http}{use_uri_for}
56 ? ( map { $c->uri_for($_) } @$domain )
57 : ( map { URI::Escape::uri_escape($_) } @$domain );
59 push @opts, qq{domain="@uris"};
62 $c->res->headers->www_authenticate(join " ", "Basic", @opts);
73 Catalyst::Plugin::Authentication::Credential::HTTP - HTTP Basic authentication
80 Authentication::Store::Moose
81 Authentication::Credential::HTTP
85 my ( $self, $c ) = @_;
87 $c->authorization_requried( realm => "foo" ); # named after the status code ;-)
89 # either user gets authenticated or 401 is sent
95 __PACKAGE__->deny_access_unless("/path", sub { $_[0]->authenticate_http });
98 my ( $self, $c ) = @_;
100 $c->authorization_required_response( realm => "foo" );
106 This moduule lets you use HTTP authentication with
107 L<Catalyst::Plugin::Authentication>.
109 Currently this module only supports the Basic scheme, but upon request Digest
110 will also be added. Patches welcome!
116 =item authorization_required
118 Tries to C<authenticate_http>, and if that files calls
119 C<authorization_required_response> and detaches the current action call stack.
121 =item authenticate_http
123 Looks inside C<< $c->request->headers >> and processes the basic (badly named)
124 authorization header.
126 =item authorization_required_response
128 Sets C<< $c->response >> to the correct status code, and adds the correct
129 header to demand authentication data from the user agent.