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