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