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