File Coverage

blib/lib/WWW/Suffit/Client/V1.pm
Criterion Covered Total %
statement 18 57 31.5
branch 0 30 0.0
condition 0 13 0.0
subroutine 6 9 66.6
pod 3 3 100.0
total 27 112 24.1


line stmt bran cond sub pod time code
1             package WWW::Suffit::Client::V1;
2 1     1   114199 use warnings;
  1         2  
  1         96  
3 1     1   7 use strict;
  1         3  
  1         45  
4 1     1   558 use utf8;
  1         301  
  1         6  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             WWW::Suffit::Client::V1 - The Suffit API client library for V1 methods
11              
12             =head1 SYNOPSIS
13              
14             use WWW::Suffit::Client::V1;
15              
16             =head1 DESCRIPTION
17              
18             This library provides V1 methods for access to Suffit API servers
19              
20             =head1 API METHODS
21              
22             List of predefined the Suffit API methods
23              
24             =head2 authn
25              
26             my $status = $client->authn($username, $password, $address);
27              
28             Performs user authentication on the OWL system
29              
30             =head2 authz
31              
32             my $status = $client->authz(GET => "https://bob@owl.localhost:8695/stuff");
33             my $status = $client->authz(GET => "https://owl.localhost:8695/stuff",
34             { # Options
35             verbose => \1,
36             username => "bob",
37             address => "127.0.0.1",
38             headers => { # Headers
39             Accept => "text/html,text/plain",
40             Connection => "keep-alive",
41             Host => "owl.localhost:8695",
42             },
43             },
44             );
45              
46             Performs user authorization on the OWL system
47              
48             =head2 pubkey
49              
50             my $status = $client->pubkey();
51             my $status = $client->pubkey(1); # Set public_key to object
52              
53             Returns RSA public key of the token owner
54              
55             =head1 DEPENDENCIES
56              
57             L, L
58              
59             =head1 TO DO
60              
61             See C file
62              
63             =head1 SEE ALSO
64              
65             L, L
66              
67             =head1 AUTHOR
68              
69             Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE
70              
71             =head1 COPYRIGHT
72              
73             Copyright (C) 1998-2026 D&D Corporation
74              
75             =head1 LICENSE
76              
77             This program is distributed under the terms of the Artistic License Version 2.0
78              
79             See the C file or L for details
80              
81             =cut
82              
83 1     1   539 use parent qw/ WWW::Suffit::Client /;
  1         336  
  1         6  
84              
85 1     1   71 use WWW::Suffit::Const qw/ :MIME /;
  1         16  
  1         130  
86 1     1   846 use WWW::Suffit::RSA;
  1         6324  
  1         12  
87              
88             ## SUFFIT API V1 METHODS
89              
90             sub authn {
91 0     0 1   my $self = shift;
92 0           my $username = shift;
93 0           my $password = shift;
94 0           my $address = shift;
95 0           my $encrypted = 0;
96              
97 0 0         if (length($self->public_key)) {
98 0           my $rsa = WWW::Suffit::RSA->new(public_key => $self->public_key);
99 0           $password = $rsa->encrypt($password); # Encrypt password
100 0 0         if ($rsa->error) {
101 0           $self->error($rsa->error);
102 0           $self->status(0);
103 0           return 0;
104             }
105 0           $encrypted = 1;
106             }
107              
108 0           my %data = ();
109 0 0         $data{username} = $username if defined $username;
110 0 0         $data{password} = $password if defined $password;
111 0 0         $data{address} = $address if defined $address;
112 0           $data{encrypted} = \$encrypted,
113              
114             # Request
115             return $self->request(POST => $self->str2url("v1/authn"),
116             { # Headers
117             Accept => CONTENT_TYPE_JSON, # "*/*"
118             },
119             json => {%data},
120             );
121             }
122             sub authz {
123 0     0 1   my $self = shift;
124 0   0       my $method = shift // '';
125 0   0       my $url = shift // '';
126 0   0       my $options = shift || {};
127 0 0         $options = {} unless ref($options) eq 'HASH';
128 0           my %data = ();
129 0 0         $data{method} = $method if length($method);
130 0 0         $data{url} = $url if length($url);
131 0 0         $data{username} = $options->{username} if exists $options->{username};
132 0 0         $data{verbose} = $options->{verbose} if exists $options->{verbose};
133 0 0         $data{address} = $options->{address} if exists $options->{address};
134 0           my $headers = $options->{headers};
135 0 0         $data{headers} = $headers if ref($headers) eq 'HASH';
136              
137             # Request
138 0           return $self->request(POST => $self->str2url("v1/authz"),
139             { # Headers
140             Accept => CONTENT_TYPE_JSON, # "*/*"
141             },
142             json => {%data},
143             );
144             }
145             sub pubkey {
146 0     0 1   my $self = shift;
147 0   0       my $set = shift || 0;
148              
149             # Request
150 0           my $status = $self->request(GET => $self->str2url("v1/publicKey"),
151             { # Headers
152             Accept => CONTENT_TYPE_JSON, # "*/*"
153             },
154             );
155 0 0         return 0 unless $status;
156              
157             # Get public_key
158 0 0         my $public_key = $self->res->json("/public_key") if $self->res->json("/status");
159 0 0 0       $self->public_key($public_key) if $set && length($public_key // '');
      0        
160              
161 0           return $status;
162             }
163              
164             1;
165              
166             __END__