Commit | Line | Data |
0b5b70ba |
1 | #!/usr/bin/perl |
2 | |
e8a7c384 |
3 | package Catalyst::Plugin::Authentication::Store::Htpasswd::Backend; |
4 | use base qw/Class::Accessor::Fast/; |
0b5b70ba |
5 | |
6 | use strict; |
7 | use warnings; |
8 | |
3e0bbcff |
9 | use Authen::Htpasswd; |
e8a7c384 |
10 | use Catalyst::Plugin::Authentication::Store::Htpasswd::User; |
11 | |
12 | BEGIN { __PACKAGE__->mk_accessors(qw/file/) } |
0b5b70ba |
13 | |
14 | sub 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 | |
20 | sub get_user { |
21 | my ( $self, $id ) = @_; |
3e0bbcff |
22 | Catalyst::Plugin::Authentication::Store::Htpasswd::User->new( $self->file->lookup_user($id) ); |
0b5b70ba |
23 | } |
24 | |
25 | sub 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 |
40 | Catalyst::Plugin::Authentication::Store::Htpasswd::Backend - Htpasswd |
0b5b70ba |
41 | authentication 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 |
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. |
0b5b70ba |
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 | |