File Coverage

blib/lib/Articulate/Credentials.pm
Criterion Covered Total %
statement 15 32 46.8
branch 0 4 0.0
condition n/a
subroutine 5 9 55.5
pod 3 3 100.0
total 23 48 47.9


line stmt bran cond sub pod time code
1             package Articulate::Credentials;
2 9     9   41 use strict;
  9         15  
  9         330  
3 9     9   44 use warnings;
  9         15  
  9         229  
4              
5 9     9   42 use Moo;
  9         17  
  9         78  
6 9     9   3064 use overload bool => sub { shift->accepted }, '0+' => sub { shift->rejected };
  9     0   18  
  9         107  
  0         0  
  0         0  
7              
8             =head1 NAME
9              
10             Articulate::Credentials - represent an authentication request/response
11              
12             =cut
13              
14             =head1 FUNCTIONS
15              
16             =head3 credentials
17              
18             my $credentials = credentials $user_id, $password;
19             my $credentials = credentials { email => $email, api_key => $key };
20              
21             Creates a new request, using the user_id and password supplied as the respective arguments; or other fields if they are supplied instead.
22              
23             =cut
24              
25 9     9   734 use Exporter::Declare;
  9         19  
  9         57  
26             default_exports qw(credentials);
27              
28             sub credentials {
29 0 0   0 1   return shift if ref $_[0] eq __PACKAGE__;
30 0 0         __PACKAGE__->new( {
31             fields => ref $_[0] eq ref {} ? $_[0] : {
32             user_id => shift,
33             password => shift,
34             },
35             } );
36             };
37              
38             =head1 METHODS
39              
40             =head3 new
41              
42             An unremarkable Moo constructor.
43              
44             =cut
45              
46             =head3 accept
47              
48             $credentials->accept('Password matched');
49              
50             Declares that the credentials are valid, for the reason given; sets C and C and populates the stack trace.
51              
52             =cut
53              
54             sub accept {
55 0     0 1   my $self = shift;
56 0           my $reason = shift;
57             # die if granted or denied are already set?
58 0           $self->accepted(1);
59 0           $self->reason($reason);
60 0           $self->stack_trace(Devel::StackTrace->new);
61 0           return $self;
62             }
63              
64             =head3 reject
65              
66             $credentials->reject('User not found');
67              
68             Declares that the credentials are invalid, for the reason given; sets C and C and populates the stack trace.
69              
70             =cut
71              
72             sub reject {
73 0     0 1   my $self = shift;
74 0           my $reason = shift;
75             # die if granted or denied are already set?
76 0           $self->accepted(0);
77 0           $self->rejected(1);
78 0           $self->reason($reason);
79 0           $self->stack_trace(Devel::StackTrace->new);
80 0           return $self;
81             }
82              
83             =head1 ATTRIBUTES
84              
85             =head3 fields
86              
87             The credentials provided, typically user_id and password.
88              
89             =cut
90              
91             has fields =>
92             is => 'rw',
93             default => sub { {} };
94              
95             =head3 accepted
96              
97             Whether or not the credentials have been explicitly accepted. The value of this is used for overload behaviour.
98              
99             Please do not explicitly set this. Use C instead.
100              
101             =cut
102              
103             has accepted =>
104             is => 'rw',
105             default => sub { 0 };
106              
107             =head3 rejected
108              
109             Whether the credentials have been explicitly rejected.
110              
111             Please do not explicitly set this. Use C instead.
112              
113             =cut
114              
115             has rejected =>
116             is => 'rw',
117             default => sub { 0 };
118              
119             =head3 reason
120              
121             The reason for the acceptance or rejection of credentials.
122              
123             Please do not explicitly set this. Use C or C instead.
124              
125             =cut
126              
127             has reason =>
128             is => 'rw',
129             default => sub { '' };
130              
131             =head3 stack_trace
132              
133             The stack trace at the point of acceptance or rejection of credentials.
134              
135             Please do not explicitly set this. Use C or C instead.
136              
137             =cut
138              
139             has stack_trace =>
140             is => 'rw',
141             default => sub { '' };
142              
143             =head1 SEE ALSO
144              
145             =over
146              
147             =item * L
148              
149             =item * L (which performs a similar function for L)
150              
151             =back
152              
153             =cut
154              
155             1;