Auth store registration
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / User / Hash.pm
CommitLineData
06675d2e 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication::User::Hash;
4use base qw/Catalyst::Plugin::Authentication::User/;
5
6use strict;
7use warnings;
8
9sub new {
c8cdf03d 10 my $class = shift;
06675d2e 11
0c4ddd06 12 bless { @_ }, $class;
06675d2e 13}
14
15sub AUTOLOAD {
16 my $self = shift;
17 ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ );
18
96777f3a 19 $self->_accessor( $key, @_ );
20}
21
22sub id {
23 my $self = shift;
24 $self->_accessor( "id", @_ );
25}
26
27sub store {
28 my $self = shift;
29 $self->_accessor( "store", @_ );
30}
31
32sub _accessor {
33 my $self = shift;
34 my $key = shift;
35
c8cdf03d 36 if (@_) {
37 my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1;
38 $self->{$key} = $arr ? [@_] : shift;
39 }
22be989b 40
c8cdf03d 41 my $data = $self->{$key};
0c4ddd06 42 ( $self->{__hash_obj_key_is_array}{$key} || $key =~ /roles/ )
da422b3a 43 ? @{ $data || [] }
0c4ddd06 44 : $data;
06675d2e 45}
46
47my %features = (
48 password => {
c8cdf03d 49 clear => ["password"],
50 crypted => ["crypted_password"],
51 hashed => [qw/hashed_password hash_algorithm/],
52 self_check => undef,
06675d2e 53 },
c8cdf03d 54 roles => ["roles"],
96777f3a 55 session => [qw/store id/],
06675d2e 56);
57
58sub supports {
59 my ( $self, @spec ) = @_;
60
61 my $cursor = \%features;
62
63 # traverse the feature list,
64 for (@spec) {
65 die "bad feature spec: @spec"
22be989b 66 if ref($cursor) ne "HASH";
67 $cursor = $cursor->{$_};
06675d2e 68 }
69
c8cdf03d 70 if ( ref $cursor ) {
71 die "bad feature spec: @spec" unless ref $cursor eq "ARRAY";
06675d2e 72
c8cdf03d 73 # check that all the keys required for a feature are in here
74 foreach my $key (@$cursor) {
75 return undef unless exists $self->{$key};
76 }
06675d2e 77
c8cdf03d 78 return 1;
79 }
80 else {
81 return $cursor;
82 }
06675d2e 83}
84
85sub for_session {
86 my $self = shift;
06675d2e 87 return $self; # let's hope we're serialization happy
88}
89
06675d2e 90__PACKAGE__;
91
92__END__
93
94=pod
95
96=head1 NAME
97
98Catalyst::Plugin::Authentication::User::Hash - An easy authentication user
99object based on hashes.
100
101=head1 SYNOPSIS
102
103 use Catalyst::Plugin::Authentication::User::Hash;
104
105 Catalyst::Plugin::Authentication::User::Hash->new(
106 password => "s3cr3t",
107 );
108
109=head1 DESCRIPTION
110
111This implementation of authentication user handles is supposed to go hand in
112hand with L<Catalyst::Plugin::Authentication::Store::Minimal>.
113
114=head1 METHODS
115
116=over 4
117
118=item new @pairs
119
120Create a new object with the key-value-pairs listed in the arg list.
121
122=item supports
123
124Checks for existence of keys that correspond with features.
125
126=item for_session
127
128Just returns $self, expecting it to be serializable.
129
130=item from_session
131
132Just passes returns the unserialized object, hoping it's intact.
133
134=item AUTOLOAD
135
136Accessor for the key whose name is the method.
137
138=back
139
140=head1 SEE ALSO
141
142L<Hash::AsObject>
143
144=cut
145
146