File Coverage

blib/lib/Authen/Pluggable/JSON.pm
Criterion Covered Total %
statement 17 37 45.9
branch 1 6 16.6
condition n/a
subroutine 4 6 66.6
pod 1 3 33.3
total 23 52 44.2


line stmt bran cond sub pod time code
1             package Authen::Pluggable::JSON;
2             $Authen::Pluggable::JSON::VERSION = '0.03';
3 2     2   949 use Mojo::Base -base, -signatures;
  2         4  
  2         11  
4 2     2   534 use Mojo::UserAgent;
  2         4  
  2         14  
5 2     2   39 use Mojo::URL;
  2         3  
  2         10  
6              
7             has 'parent' => undef, weak => 1;
8              
9             has _cfg => sub {
10             return {
11             url => new Mojo::URL('http://127.0.0.1:3000/api/auth'),
12             query_builder => sub {
13             Mojo::URL->new(shift)->query( user => shift, pass => shift );
14             },
15             res_builder => sub($json) {return %$json ? $json : undef },
16             };
17             };
18              
19 0     0 0 0 sub authen ( $s, $user, $pass ) {
  0         0  
  0         0  
  0         0  
  0         0  
20 0         0 my $ub = $s->_cfg->{query_builder};
21 0         0 my $ut = $s->_cfg->{url};
22 0         0 my $rb = $s->_cfg->{res_builder};
23              
24 0         0 my $url = $ub->( $ut, $user, $pass );
25              
26 0         0 my $ua = Mojo::UserAgent->new;
27 0         0 my $res = $ua->get($url)->result;
28              
29 0 0       0 return $rb->( $res->json ) if ( $res->is_success );
30              
31 0         0 return undef;
32             }
33              
34 1     1 1 5 sub cfg ( $s, %cfg ) {
  1         2  
  1         2  
  1         1  
35 1 50       3 if (%cfg) {
36 1         4 while ( my ( $k, $v ) = each %cfg ) {
37 1         2 $s->_cfg->{$k} = $v;
38             }
39             }
40 1         75 return $s->parent;
41             }
42              
43 0     0 0   sub log ( $s, $type, $msg ) {
  0            
  0            
  0            
  0            
44 0 0         return unless $s->parent->log;
45 0           $s->parent->log->$type($msg);
46             }
47              
48             1;
49              
50             =pod
51              
52             =head1 NAME
53              
54             Authen::Pluggable::JSON - Authentication via external json
55              
56             =for html

57            
58             github workflow tests
59            
60             Top language:
61             github last commit
62            

63              
64             =head1 VERSION
65              
66             version 0.03
67              
68             =head1 SYNOPSIS
69              
70             use Authen::Pluggable;
71              
72             my $auth = Authen::Pluggable->new();
73              
74             $auth->provider('JSON')->cfg({
75             url => ...,
76             query_builder => ...,
77             res_builder => ...,
78             });
79              
80             my $user_info = $auth->authen($username, $password) || die "Login failed";
81              
82             =head1 DESCRIPTION
83              
84             Authen::Pluggable::JSON is a L plugin to authenticate users
85             via JSON calls.
86             You can personalize url, query parameters and returned JSON via configuration.
87              
88             =encoding UTF-8
89              
90             =head1 METHODS
91              
92             =head2 cfg
93              
94             This method takes a hash of parameters. The following options are valid:
95              
96             =over
97              
98             =item url
99              
100             Url to JSON service. Default: a L istance to
101             C
102              
103             =item query_builder
104              
105             A subroutine ref for appending query string to url. The sub is called like this:
106              
107             $qb->($url, $username, $password);
108              
109             Default appends C to url.
110              
111             =item res_builder
112              
113             A subroutine ref for altering JSON structure returned by remote service to be
114             compliance with L structure. The sub is called like this:
115              
116             $rb->($json);
117              
118             Default: return JSON structure returned by remote service as is or undef if
119             remote service return C<{}>.
120              
121             =back
122              
123             =head1 BUGS/CONTRIBUTING
124              
125             Please report any bugs through the web interface at L
126              
127             If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from
128             L.
129              
130             =head1 SUPPORT
131              
132             You can find this documentation with the perldoc command too.
133              
134             perldoc Authen::Pluggable::JSON
135              
136             =head1 AUTHOR
137              
138             Emiliano Bruni
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2022 by Emiliano Bruni.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut
148              
149             __END__