File Coverage

blib/lib/Auth/Kokolores/Plugin/FileRetrieve.pm
Criterion Covered Total %
statement 6 43 13.9
branch 0 10 0.0
condition 0 6 0.0
subroutine 2 5 40.0
pod 0 3 0.0
total 8 67 11.9


line stmt bran cond sub pod time code
1             package Auth::Kokolores::Plugin::FileRetrieve;
2              
3 1     1   896 use Moose;
  1         2  
  1         5  
4 1     1   4047 use DBI;
  1         1  
  1         673  
5              
6             # ABSTRACT: kokolores plugin for retrieving users from a file
7             our $VERSION = '1.01'; # VERSION
8              
9             extends 'Auth::Kokolores::Plugin';
10              
11              
12             has 'seperator' => ( is => 'ro', isa => 'Str', default => '\s+' );
13             has sep_regex => (
14             is => 'ro', isa => 'RegexpRef', lazy => 1,
15             default => sub {
16             my $self = shift;
17             my $str = $self->seperator;
18             my $regex = eval { qr/$str/ };
19             if( $@ ) { die("invalid regex in seperator: $@") }
20             return $regex;
21             },
22             );
23              
24             has 'fields' => ( is => 'ro', isa => 'Str', default => 'username,password');
25             has '_fields' => (
26             is => 'ro', isa => 'ArrayRef[Str]', lazy => 1,
27             default => sub { [ split(/\s*,\s*/, shift->fields ) ] },
28             traits => [ 'Array' ],
29             handles => {
30             'num_fields' => 'count',
31             }
32             );
33              
34             has 'file' => ( is => 'ro', isa => 'Str', required => 1 );
35              
36             has 'fh' => (
37             is => 'ro', isa => 'IO::File', lazy => 1,
38             default => sub {
39             my $self = shift;
40             my $fh = IO::File->new( $self->file, 'r',);
41             if( ! defined $fh ) {
42             die("could not open user file: $!");
43             }
44             return $fh;
45             },
46             );
47              
48             has 'comments' => ( is => 'ro', isa => 'Bool', default => 0 );
49              
50             sub parse_line {
51 0     0 0   my ( $self, $line, $ln ) = @_;
52 0           my $data = {};
53 0 0 0       if( $self->comments && $line =~ /^\s*#/ ) {
54 0           return;
55             }
56 0           $line =~ s/[\r\n]*$//;
57 0           my $sep = $self->sep_regex;
58 0           my @values = split( $sep, $line );
59 0 0         if( scalar @values < $self->num_fields ) {
60 0           $self->server->log(2, "insufficient fields on line $ln");
61 0           return;
62             }
63 0           foreach my $fieldname ( @{$self->_fields} ) {
  0            
64 0           $data->{$fieldname} = shift( @values );
65             }
66 0           return $data;
67             }
68              
69             has 'username_field' => ( is => 'rw', isa => 'Str', default => 'username' );
70              
71             sub lookup_user {
72 0     0 0   my ( $self, $key ) = @_;
73 0           my $username_field = $self->username_field;
74 0           my $ln = 0;
75 0           $self->fh->setpos(0);
76 0           while( my $line = $self->fh->getline ) {
77 0           $ln++;
78 0           my $user = $self->parse_line( $line, $ln );
79 0 0 0       if( defined $user->{$username_field}
80             && $user->{$username_field} eq $key) {
81 0           $self->log(4, "found user on line $ln" );
82 0           return $user;
83             }
84             }
85 0           return;
86             }
87              
88             sub authenticate {
89 0     0 0   my ( $self, $r ) = @_;
90            
91 0           $self->log(4, "searching for user ".$r->username );
92 0           my $user = $self->lookup_user( $r->username );
93 0 0         if( ! defined $user ) {
94 0           $r->log(3, 'could not find user '.$r->username);
95 0           return 0;
96             }
97              
98 0           foreach my $field ( keys %$user ) {
99 0 0         if( ! defined $user->{$field} ) {
100 0           next;
101             }
102 0           $r->log(4, 'retrieved userinfo '.$field.'='.$user->{$field});
103 0           $r->set_info( $field, $user->{$field} );
104             }
105              
106 0           return 1;
107             }
108              
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Auth::Kokolores::Plugin::FileRetrieve - kokolores plugin for retrieving users from a file
120              
121             =head1 VERSION
122              
123             version 1.01
124              
125             =head1 DESCRIPTION
126              
127             Retrieve a user from a line based password file.
128              
129             Will fail if no user is found.
130              
131             =head1 EXAMPLE
132              
133             <Plugin retrieve-user>
134             module = "FileRetrieve"
135             file = "users.txt"
136             seperator = "\s+"
137             fields = "username,password"
138             </Plugin>
139              
140             =head1 MODULE PARAMETERS
141              
142             =head1 AUTHOR
143              
144             Markus Benning <ich@markusbenning.de>
145              
146             =head1 COPYRIGHT AND LICENSE
147              
148             This software is Copyright (c) 2016 by Markus Benning <ich@markusbenning.de>.
149              
150             This is free software, licensed under:
151              
152             The GNU General Public License, Version 2, June 1991
153              
154             =cut