htpasswd backend works
[catagits/Catalyst-Authentication-Store-Htpasswd.git] / lib / Catalyst / Plugin / Authentication / Store / Htpasswd / Backend.pm
CommitLineData
0b5b70ba 1#!/usr/bin/perl
2
e8a7c384 3package Catalyst::Plugin::Authentication::Store::Htpasswd::Backend;
4use base qw/Class::Accessor::Fast/;
0b5b70ba 5
6use strict;
7use warnings;
8
e8a7c384 9use Apache::Htpasswd;
10use Catalyst::Plugin::Authentication::Store::Htpasswd::User;
11
12BEGIN { __PACKAGE__->mk_accessors(qw/file/) }
0b5b70ba 13
14sub new {
e8a7c384 15 my ( $class, $file, @extra) = @_;
0b5b70ba 16
e8a7c384 17 bless { file => ( ref($file) ? $file : Apache::Htpasswd->new($file, @extra) ) },
18 $class;
0b5b70ba 19}
20
21sub get_user {
22 my ( $self, $id ) = @_;
e8a7c384 23 Catalyst::Plugin::Authentication::Store::Htpasswd::User->new( $id,
24 $self->file );
0b5b70ba 25}
26
27sub user_supports {
28 my $self = shift;
29
e8a7c384 30 # this can work as a class method
31 Catalyst::Plugin::Authentication::Store::Htpasswd::User->supports(@_);
0b5b70ba 32}
33
34__PACKAGE__;
35
36__END__
37
38=pod
39
40=head1 NAME
41
e8a7c384 42Catalyst::Plugin::Authentication::Store::Htpasswd::Backend - Htpasswd
0b5b70ba 43authentication storage backend.
44
45=head1 SYNOPSIS
46
e8a7c384 47 # you probably just want Store::Htpasswd under most cases,
0b5b70ba 48 # but if you insist you can instantiate your own store:
49
e8a7c384 50 use Catalyst::Plugin::Authentication::Store::Htpasswd::Backend;
0b5b70ba 51
52 use Catalyst qw/
53 Authentication
54 Authentication::Credential::Password
55 /;
56
57 my %users = (
58 user => { password => "s3cr3t" },
59 );
60
e8a7c384 61 our $users = Catalyst::Plugin::Authentication::Store::Htpasswd::Backend->new(\%users);
0b5b70ba 62
63 sub action : Local {
64 my ( $self, $c ) = @_;
65
66 $c->login( $users->get_user( $c->req->param("login") ),
67 $c->req->param("password") );
68 }
69
70=head1 DESCRIPTION
71
e8a7c384 72You probably want L<Catalyst::Plugin::Authentication::Store::Htpasswd>, unless
73you are mixing several stores in a single app and one of them is Htpasswd.
0b5b70ba 74
75Otherwise, this lets you create a store manually.
76
77=head1 METHODS
78
79=over 4
80
81=item new $hash_ref
82
83Constructs a new store object, which uses the supplied hash ref as it's backing
84structure.
85
86=item get_user $id
87
88Keys the hash by $id and returns the value.
89
90If the return value is unblessed it will be blessed as
91L<Catalyst::Plugin::Authentication::User::Hash>.
92
93=item user_supports
94
95Chooses a random user from the hash and delegates to it.
96
97=back
98
99=cut
100
101