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 10     10   254 use strict;
  10         14  
  10         332  
3 10     10   57 use warnings;
  10         14  
  10         421  
4              
5 10     10   44 use Moo;
  10         20  
  10         83  
6 10     10   3397 use overload bool => sub { shift->accepted }, '0+' => sub { shift->rejected };
  10     0   19  
  10         109  
  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 10     10   695 use Exporter::Declare;
  10         16  
  10         61  
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             {
32             fields => (
33             ( ref $_[0] eq ref {} )
34             ? $_[0]
35             : {
36             user_id => shift,
37             password => shift,
38             }
39             ),
40             }
41             );
42             }
43              
44             =head1 METHODS
45              
46             =head3 new
47              
48             An unremarkable Moo constructor.
49              
50             =cut
51              
52             =head3 accept
53              
54             $credentials->accept('Password matched');
55              
56             Declares that the credentials are valid, for the reason given; sets C and C and populates the stack trace.
57              
58             =cut
59              
60             sub accept {
61 0     0 1   my $self = shift;
62 0           my $reason = shift;
63              
64             # die if granted or denied are already set?
65 0           $self->accepted(1);
66 0           $self->reason($reason);
67 0           $self->stack_trace( Devel::StackTrace->new );
68 0           return $self;
69             }
70              
71             =head3 reject
72              
73             $credentials->reject('User not found');
74              
75             Declares that the credentials are invalid, for the reason given; sets C and C and populates the stack trace.
76              
77             =cut
78              
79             sub reject {
80 0     0 1   my $self = shift;
81 0           my $reason = shift;
82              
83             # die if granted or denied are already set?
84 0           $self->accepted(0);
85 0           $self->rejected(1);
86 0           $self->reason($reason);
87 0           $self->stack_trace( Devel::StackTrace->new );
88 0           return $self;
89             }
90              
91             =head1 ATTRIBUTES
92              
93             =head3 fields
94              
95             The credentials provided, typically user_id and password.
96              
97             =cut
98              
99             has fields => (
100             is => 'rw',
101             default => sub { {} },
102             );
103              
104             =head3 accepted
105              
106             Whether or not the credentials have been explicitly accepted. The value of this is used for overload behaviour.
107              
108             Please do not explicitly set this. Use C instead.
109              
110             =cut
111              
112             has accepted => (
113             is => 'rw',
114             default => sub { 0 },
115             );
116              
117             =head3 rejected
118              
119             Whether the credentials have been explicitly rejected.
120              
121             Please do not explicitly set this. Use C instead.
122              
123             =cut
124              
125             has rejected => (
126             is => 'rw',
127             default => sub { 0 },
128             );
129              
130             =head3 reason
131              
132             The reason for the acceptance or rejection of credentials.
133              
134             Please do not explicitly set this. Use C or C instead.
135              
136             =cut
137              
138             has reason => (
139             is => 'rw',
140             default => sub { '' },
141             );
142              
143             =head3 stack_trace
144              
145             The stack trace at the point of acceptance or rejection of credentials.
146              
147             Please do not explicitly set this. Use C or C instead.
148              
149             =cut
150              
151             has stack_trace => (
152             is => 'rw',
153             default => sub { '' },
154             );
155              
156             =head1 SEE ALSO
157              
158             =over
159              
160             =item * L
161              
162             =item * L (which performs a similar function for L)
163              
164             =back
165              
166             =cut
167              
168             1;