CouchDB.pm

filter: Statementstmt 80.4% 33 / 41 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 100% 4 / 4 totaltotal 82.0% 55 / 67 CC 7 SCAR 13.4
CC
7
Cov
83%
__ANON__
7.0
from_session
2.0
for_session
2.0
CRAP 3.8
« CouchDB/User.pm ↑ Summary
1 ## no critic
2 # no critic line turns off Perl::Critic, to get rid of the warning that
3 # Dist::Zilla::Plugin::PkgVersion puts the package version straight
4 # after the package line, and thus before use strict
5 package Catalyst::Authentication::Store::CouchDB;
6 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $Catalyst::Authentication::Store::CouchDB::VERSION = '0.001';
8 }
9 # ABSTRACT: A storage class for Catalyst Authentication using CouchDB
10 ## use critic
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moose 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Catalyst::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 has 'store_user_class' => (is => 'ro', isa => 'Str', default => 'Catalyst::Authentication::Store::CouchDB::User', );
18 3 has 'config' => (is => 'ro', isa => 'HashRef', required => 1, );
19  
20 # Convert the parameters passed to ->new into the correct
21 # format for passing to our Moose generated constructor.
22 # Also, ensure that the user class is loaded.
23  
24 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
25 6 my ( $orig, $class, $config, $c ) = @_;
26  
27 # Validate the configuration
28 6 ▶ if (!$config->{couchdb_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"couchdb_uri"}) 1 5 6
29 1 Catalyst::Exception->throw("couchdb_uri required in configuration");
30 }
31  
32 5 ▶ if (!$config->{dbname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"dbname"}) 1 4 5
33 1 Catalyst::Exception->throw("dbname required in configuration");
34 }
35 4 ▶ if (!$config->{designdoc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"designdoc"}) 1 3 4
36 1 Catalyst::Exception->throw("designdoc required in configuration");
37 }
38 3 ▶ if ($config->{designdoc} !~ /^\_design\//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"designdoc"} =~ m[^\_design/]) 1 2 3
39 1 Catalyst::Exception->throw("designdoc must start with _design/ required in configuration");
40 }
41 2 ▶ if (!$config->{view}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"view"}) 1 1 2
42 1 Catalyst::Exception->throw("view required in configuration");
43 }
44  
45 ## figure out if we are overriding the default store user class
46 1 ▶ $config->{'store_user_class'} = (exists($config->{'store_user_class'})) ? $config->{'store_user_class'} :
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'store_user_class'} ? : 0 1 1
47 "Catalyst::Authentication::Store::CouchDB::User";
48  
49 ## make sure the store class is loaded.
50 1 Catalyst::Utils::ensure_class_loaded( $config->{'store_user_class'} );
51  
52 # $orig will call the superclass BUILDARGS, which
53 # will format the args hash appropriately.
54 return $class->$orig(
55 store_user_class => $config->{store_user_class},
56 1 config => $config,
57 );
58 3 };
59  
60 0 ▶sub from_session {
Subroutine from_session: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($self, $c, $frozenuser) = @_;
62  
63 0 my $user = $self->store_user_class->new($self->{'config'}, $c);
64 0 return $user->from_session($frozenuser);
65 }
66  
67 0 ▶sub for_session {
Subroutine for_session: not called CC=1 SCAR=6.9
Pod documented
68 0 my ($self, $c, $user) = @_;
69  
70 0 return $user->for_session();
71 }
72  
73 1 ▶sub find_user {
Subroutine find_user: called CC=1 SCAR=0.4
Pod documented
74 1 my ($self, $authinfo, $c) = @_;
75  
76 1 my $user = $self->store_user_class->new($self->{'config'}, $c);
77  
78 0 return $user->load($authinfo, $c);
79 }
80  
81 0 ▶sub user_supports {
Subroutine user_supports: not called CC=1 SCAR=6.9
Pod documented
82 0 my $self = shift;
83 # this can work as a class method on the user class
84 0 return $self->store_user_class->supports( @_ );
85 }
86  
87 3 1;
88  
89  
90  
91 =pod
92  
93 =head1 NAME
94  
95 Catalyst::Authentication::Store::CouchDB - A storage class for Catalyst Authentication using CouchDB
96  
97 =head1 VERSION
98  
99 version 0.001
100  
101 =head1 SYNOPSIS
102  
103 use Catalyst qw/
104 Authentication
105 Authorization::Roles/;
106  
107 __PACKAGE__->config->{authentication} =
108 {
109 default_realm => 'members',
110 realms => {
111 members => {
112 credential => {
113 class => 'Password',
114 password_field => 'password',
115 password_type => 'salted_hash',
116 password_salt_len => 4,
117 },
118 store => {
119 class => 'CouchDB',
120 couchdb_uri => 'http://localhost:5984',
121 dbname => 'demouser',
122 designdoc => '_design/user',
123 view => 'user',
124 },
125 },
126 },
127 };
128  
129 # Log a user in:
130  
131 sub login : Global {
132 my ( $self, $c ) = @_;
133  
134 $c->authenticate({
135 username => $c->req->params->{username},
136 password => $c->req->params->{password},
137 }))
138 }
139  
140 # verify a role
141  
142 if ( $c->check_user_roles( 'editor' ) ) {
143 # do editor stuff
144 }
145  
146 =head1 DESCRIPTION
147  
148 The Catalyst::Authentication::Store::CouchDB class provides access to authentication
149 information stored in a CouchDB instance.
150  
151 =head1 CONFIGURATION
152  
153 The CouchDB authentication store is activated by setting the store
154 config's B<class> element to CouchDB as shown above. See the
155 L<Catalyst::Plugin::Authentication> documentation for more details on
156 configuring the store.
157  
158 The CouchDB storage module has several configuration options
159  
160 __PACKAGE__->config->{authentication} =
161 {
162 default_realm => 'members',
163 realms => {
164 members => {
165 credential => {
166 class => 'Password',
167 password_field => 'password',
168 password_type => 'clear'
169 },
170 store => {
171 class => 'CouchDB',
172 couchdb_uri => 'http://localhost:5984',
173 dbname => 'demouser',
174 designdoc => '_design/user',
175 view => 'user',
176 },
177 },
178 },
179 };
180  
181 =over 4
182  
183 =item class
184  
185 Class is part of the core Catalyst::Plugin::Authentication module; it
186 contains the class name of the store to be used. This config item is B<REQUIRED>.
187  
188 =item couchdb_uri
189  
190 Contains the URI of the CouchDB instance to query. This config item is B<REQUIRED>.
191  
192 =item dbname
193  
194 Contains the name of the database to query. This config item is B<REQUIRED>.
195  
196 =item designdoc
197  
198 Contains the name of the CouchDB design document to query. This config item is B<REQUIRED>.
199  
200 =item view
201  
202 Contains the name of the view in the design document to query. The 'username' field
203 will be used as the key to query, and the first document retrieved will be used
204 to create the user model. This config item is B<REQUIRED>.
205  
206 =item ua
207  
208 Contains the name of a class to be used for the User Agent. This defaults
209 to LWP::UserAgent if not configured. It is passed through to CouchDB::Client.
210  
211 =back
212  
213 =head1 USAGE
214  
215 The L<Catalyst::Authentication::Store::CouchDB> storage module
216 is not called directly from application code. You interface with it
217 through the $c->authenticate() call.
218  
219 The L<Catalyst::Authentication::Store::CouchDB> fetches a user from CouchDB
220 by querying a view within a CouchDB design document. The view is queried with
221 the C<username> passed in the authenticate call hash as the key, and returns
222 a CouchDB document. This document is then passed to
223 L<Catalyst::Authentication::Store::CouchDB::User> to create the user object.
224  
225 A suitable view map function is
226  
227 function(doc) {
228 if (doc.username) {
229 emit(doc.username, null);
230 }
231 }
232  
233 =head1 METHODS
234  
235 There are no publicly exported routines in the CouchDB authentication
236 store (or indeed in most authentication stores). However, below is a
237 description of the routines required by L<Catalyst::Plugin::Authentication>
238 for all authentication stores. Please see the documentation for
239 L<Catalyst::Plugin::Authentication::Internals> for more information.
240  
241 =head2 new ( $config, $app )
242  
243 Constructs a new store object.
244  
245 =head2 find_user ( $authinfo, $c )
246  
247 Finds a user using the information provided in the $authinfo hashref and
248 returns the user, or undef on failure. This is usually called from the
249 Credential. This translates directly to a call to the User object's
250 load() method.
251  
252 =head2 for_session ( $c, $user )
253  
254 Prepares a user to be stored in the session. This is delegated to
255 the User obect for_session method.
256  
257 =head2 from_session ( $c, $frozenuser)
258  
259 Revives a user from the session based on the info provided in $frozenuser.
260 This is delegated to the User object from_session method.
261  
262 =head2 user_supports
263  
264 Provides information about what the user object supports.
265  
266 =head1 NOTES
267  
268 This module is heavily based on L<Catalyst::Authentication::Store::DBIx::Class>.
269  
270 The test scripts use clear text passwords. B<DO NOT DO THIS IN PRODUCTION.>
271 Use configuation as shown in the synopsis to use something stronger, such as
272 salted hash passwords.
273  
274 The test scripts do not connect to a CouchDB instance as standard - they
275 mock the responses that CouchDB would send. To connect to a CouchDB instance,
276 set the C<CATALYST_COUCHDB_LIVE> environment variable before running the test suite.
277 The test suite assumes that a C<demouser> database exists, with a design document
278 called C<user> that contains a C<user> view, and that a document listing a test
279 user with username C<test> and password C<test> exists. To configure this,
280 run the C<setup_database.pl> script in the C<t/script> directory on the distribution.
281 B<This script will remove any existing demouser database.>
282  
283 =head1 BUGS AND LIMITATIONS
284  
285 There are bound to be bugs - please email the author if you find any.
286  
287 =head1 SEE ALSO
288  
289 L<Catalyst::Authentication::Store::DBIx::Class>.
290 L<Catalyst::Plugin::Authentication>,
291 L<Catalyst::Plugin::Authentication::Internals>,
292 and L<Catalyst::Plugin::Authorization::Roles>
293  
294 =head1 AUTHOR
295  
296 Colin Bradford <cjbradford@gmail.com>
297  
298 =head1 COPYRIGHT AND LICENSE
299  
300 This software is copyright (c) 2011 by Colin Bradford.
301  
302 This is free software; you can redistribute it and/or modify it under
303 the same terms as the Perl 5 programming language system itself.
304  
305 =cut
306  
307  
308 __END__
« CouchDB/User.pm ↑ Summary