a088c306e8b077606954b57b1f7312ffb4db81e4
[catagits/Catalyst-Authentication-Store-Htpasswd.git] / lib / Catalyst / 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 =head2 new $hash_ref
83
84 Constructs a new store object, which uses the supplied hash ref as it's backing
85 structure.
86
87 =head2 get_user $id
88
89 Keys the hash by $id and returns the value.
90
91 If the return value is unblessed it will be blessed as
92 L<Catalyst::Plugin::Authentication::User::Hash>.
93
94 =head2 user_supports
95
96 Chooses a random user from the hash and delegates to it.
97
98 =head1 COPYRIGHT & LICENSE
99
100         Copyright (c) 2005 the aforementioned authors. All rights
101         reserved. This program is free software; you can redistribute
102         it and/or modify it under the same terms as Perl itself.
103
104 =cut
105
106