File Coverage

blib/lib/Dancer2/Plugin/Auth/Extensible/Provider/IMAP.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition 3 3 100.0
subroutine 6 6 100.0
pod 1 1 100.0
total 40 41 97.5


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::Auth::Extensible::Provider::IMAP;
2              
3 3     3   1148246 use Carp qw/croak/;
  3         7  
  3         240  
4 3     3   548 use Dancer2::Core::Types qw/HashRef Str/;
  3         11229  
  3         229  
5 3     3   745 use Net::IMAP::Simple;
  3         44911  
  3         116  
6              
7 3     3   689 use Moo;
  3         9220  
  3         25  
8             with "Dancer2::Plugin::Auth::Extensible::Role::Provider";
9 3     3   2594 use namespace::clean;
  3         10824  
  3         22  
10              
11             our $VERSION = '0.003';
12              
13             =head1 NAME
14              
15             Dancer2::Plugin::Auth::Extensible::Provider::IMAP - IMAP authentication provider for Dancer2::Plugin::Auth::Extensible
16              
17             =head1 DESCRIPTION
18              
19             This class is a generic IMAP authentication provider.
20              
21             See L<Dancer2::Plugin::Auth::Extensible> for details on how to use the
22             authentication framework.
23              
24             =head1 ATTRIBUTES
25              
26             =head2 host
27              
28             IMAP server name or IP address. Required.
29              
30             =cut
31              
32             has host => (
33             is => 'ro',
34             isa => Str,
35             required => 1,
36             );
37              
38             =head2 options
39              
40             A hash reference of options to be passed to L<Net::IMAP::Simple/new>.
41              
42             Defaults to:
43              
44             {
45             port => 993,
46             use_ssl => 1,
47             ssl_version => 'TLSv1',
48             }
49              
50             =cut
51              
52             has options => (
53             is => 'ro',
54             isa => HashRef,
55             default => sub { +{ port => 993, use_ssl => 1, ssl_version => 'TLSv1' } },
56             );
57              
58             =head1 METHODS
59              
60             =head2 authenticate_user $username, $password
61              
62             =cut
63              
64             sub authenticate_user {
65 88     88 1 2575085 my ( $self, $username, $password ) = @_;
66 88 100 100     1439 croak "username and password must be defined"
67             unless defined $username && defined $password;
68              
69 82         261 my $imap = Net::IMAP::Simple->new( $self->host, %{ $self->options } );
  82         2221  
70 82 50       5498 croak "IMAP connect failed: $Net::IMAP::Simple::errstr"
71             unless $imap;
72              
73 82         269 my $ret = $imap->login($username, $password);
74 82 100       1053 if ( $ret ) {
75 18         63 $imap->logout;
76             }
77             else {
78 64         401 $self->plugin->app->log(
79             debug => "IMAP login failed: $Net::IMAP::Simple::errstr" );
80             }
81 82         35743 return $ret;
82             }
83              
84             =head1 SEE ALSO
85              
86             L<Dancer2>, L<Dancer2::Plugin::Auth::Extensible>, L<Net::IMAP::Simple>.
87              
88             =head1 AUTHOR
89              
90             Peter Mottram (SysPete), C<< <peter at sysnix.com> >>
91              
92             =head1 BUGS
93              
94             Please report any bugs or feature requests via the project's GitHub
95             issue tracker:
96              
97             L<https://github.com/SysPete/Dancer2-Plugin--Auth-Extensible-Provider-IMAP/issues>
98              
99             I will be notified, and then you'll automatically be notified of
100             progress on your bug as I make changes. PRs are always welcome.
101              
102             =head1 SUPPORT
103              
104             You can find documentation for this module with the perldoc command.
105              
106             perldoc Dancer2::Plugin::Auth::Extensible::Provider::IMAP
107              
108             You can also look for information at:
109              
110             =over 4
111              
112             =item * L<GitHub repository|https://github.com/PerlDancer/Dancer2-Plugin-Auth-Extensible-Provider-IMAP>
113              
114             =item * L<meta::cpan|https://metacpan.org/pod/Dancer2::Plugin::Auth::Extensible::Provider::IMAP>
115              
116             =back
117              
118             =head1 LICENSE AND COPYRIGHT
119              
120             Copyright 2016 Peter Mottram (SysPete).
121              
122             This program is free software; you can redistribute it and/or modify it
123             under the terms of the the Artistic License (2.0). You may obtain a
124             copy of the full license at:
125              
126             L<http://www.perlfoundation.org/artistic_license_2_0>
127              
128             Any use, modification, and distribution of the Standard or Modified
129             Versions is governed by this Artistic License. By using, modifying or
130             distributing the Package, you accept this license. Do not use, modify,
131             or distribute the Package, if you do not accept this license.
132              
133             If your Modified Version has been derived from a Modified Version made
134             by someone other than you, you are nevertheless required to ensure that
135             your Modified Version complies with the requirements of this license.
136              
137             This license does not grant you the right to use any trademark, service
138             mark, tradename, or logo of the Copyright Holder.
139              
140             This license includes the non-exclusive, worldwide, free-of-charge
141             patent license to make, have made, use, offer to sell, sell, import and
142             otherwise transfer the Package with respect to any patent claims
143             licensable by the Copyright Holder that are necessarily infringed by the
144             Package. If you institute patent litigation (including a cross-claim or
145             counterclaim) against any party alleging that the Package constitutes
146             direct or contributory patent infringement, then this Artistic License
147             to you shall terminate on the date that such litigation is filed.
148              
149             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
150             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
151             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
152             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
153             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
154             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
155             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
156             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
157              
158              
159             =cut
160              
161             1; # End of Dancer2::Plugin::Auth::Extensible::Provider::IMAP