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