File Coverage

blib/lib/OxdPackages/OxdClientSocket.pm
Criterion Covered Total %
statement 39 41 95.1
branch n/a
condition n/a
subroutine 14 14 100.0
pod n/a
total 53 55 96.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # OxdClientSocket.pm, a number as an object
3            
4             #
5             # Gluu-oxd-library
6             #
7             # An open source application library for Perl
8             #
9             # This content is released under the MIT License (MIT)
10             #
11             # Copyright (c) 2017, Gluu inc, USA, Austin
12             #
13             # Permission is hereby granted, free of charge, to any person obtaining a copy
14             # of this software and associated documentation files (the "Software"), to deal
15             # in the Software without restriction, including without limitation the rights
16             # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17             # copies of the Software, and to permit persons to whom the Software is
18             # furnished to do so, subject to the following conditions:
19             #
20             # The above copyright notice and this permission notice shall be included in
21             # all copies or substantial portions of the Software.
22             #
23             # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24             # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25             # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26             # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27             # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28             # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29             # THE SOFTWARE.
30             #
31             # @package Gluu-oxd-library
32             # @version 3.1.0
33             # @author Sobhan Panda
34             # @author_email sobhan@centroxy.com
35             # @copyright Copyright (c) 2017, Gluu inc federation (https://gluu.org/)
36             # @license http://opensource.org/licenses/MIT MIT License
37             # @link https://gluu.org/
38             # @since Version 3.1.0
39             # @filesource
40             #/
41            
42            
43             package OxdClientSocket; # This is the "Class"
44            
45 1     1   7 use vars qw($VERSION);
  1         4  
  1         57  
46             $VERSION = '0.01';
47             # makes all attributes available
48 1     1   439 use Time::Piece;
  1         11698  
  1         6  
49 1     1   478 use lib './modules';
  1         801  
  1         8  
50 1     1   595 use Attribute::Handlers;
  1         5603  
  1         8  
51             #use strict;
52 1     1   50 use warnings;
  1         3  
  1         38  
53 1     1   21 use 5.010;
  1         10  
54 1     1   8 use JSON::PP;
  1         15  
  1         91  
55 1     1   529 use Data::Dumper qw(Dumper);
  1         7521  
  1         85  
56 1     1   414 use utf8;
  1         12  
  1         6  
57 1     1   328 use Encode;
  1         6985  
  1         68  
58 1     1   8 use File::Basename;
  1         1  
  1         49  
59 1     1   5 use warnings;
  1         2  
  1         24  
60 1     1   5 use OxdPackages::OxdConfig;
  1         2  
  1         21  
61             #use IO::Socket::Socks;
62 1     1   188 use CGI::Session;#Test
  0            
  0            
63             use IO::Socket::SSL qw(debug0);
64             use LWP::UserAgent;
65             use Crypt::SSLeay;
66             use Net::SSL ();
67             #use Sys::Hostname;
68             #$| = 1;
69             use constant BUFSIZE => 1024;
70            
71             sub new {
72             my $class = shift;
73             my $self = {
74             # @static
75             # @var object $socket Socket connection
76             _socket => shift,
77            
78             # @var string $base_url Base url for log file directory and oxd-rp-setting.json file.
79             _base_url => dirname(__FILE__)
80            
81             };
82            
83             bless $self, $class;
84             return $self;
85             }
86            
87             # Sending request to oxd server via socket
88             #
89             # @param string $data
90             # @param int $char_count
91             # @return object
92             sub oxd_socket_request{
93             my ($self,$data, $char_count) = @_;
94             $char_count = $char_count ? $char_count : 8192;
95            
96             #print $data;#Test
97            
98             my $oxdConfig = OxdConfig->new();
99             my $op_host = $oxdConfig->{'_op_host'};
100             my $oxd_host_port = $oxdConfig->{'_oxd_host_port'};
101            
102             $socket = new IO::Socket::INET ( PeerHost => '127.0.0.1', PeerPort => $oxd_host_port, Proto => 'tcp', Reuse => 1) or die "$!\n";
103             if (!$socket) {
104             $self->log("Client: socket::socket_connect is not connected, error: ",$!);
105             die $!;
106             }else{
107             $self->log("Client: socket::socket_connect ", "socket connected");
108             }
109            
110             if(!($socket->syswrite($data, length($data)))){
111             $self->log("Client: socket::socket_connect ", "Error writing");
112             }
113            
114             $self->log("Client: oxd_socket_request", $socket->syswrite($data, length($data)));
115             $socket->syswrite($data, length($data));
116            
117             sysread($socket, $result, $char_count);
118            
119             if($result){
120             $self->log("Client: oxd_socket_response", $result);
121             }else{
122             $self->log("Client: oxd_socket_response", 'Error socket reading process.');
123             }
124             if(close($socket)){
125             $self->log("Client: oxd_socket_connection", "disconnected.");
126             }
127             return $result;
128             }
129            
130            
131             sub oxd_http_request {
132            
133             my ($self,$data, $httpcommand, $char_count) = @_;
134             $char_count = $char_count ? $char_count : 8192;
135            
136             my $json = JSON::PP->new;
137             my $jsonContent = $json->decode($data);
138            
139             my $accessToken = '';
140             if ($jsonContent->{protection_access_token}) {
141             $accessToken = "Bearer ".$jsonContent->{protection_access_token};
142             }
143            
144            
145             my $oxdConfig = OxdConfig->new();
146             my $rest_service_url = $oxdConfig->{'_rest_service_url'};
147            
148             my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });# 0: Do not verify SSL, 1: verify SSL
149             $ua->protocols_allowed( [ 'http','https'] );
150            
151            
152             ## If the URL does not contain '/' as the last character, then add '/' at the last
153             my $lc = substr($rest_service_url, -1);
154             if($lc ne '/')
155             {
156             $rest_service_url = $rest_service_url.'/';
157             }
158            
159             my $url = $rest_service_url.$httpcommand;
160            
161             my $header = HTTP::Headers->new;
162             $header->header(Accept => 'application/json', Authorization => $accessToken);
163            
164             my $req = HTTP::Request->new('POST', $url, $header);
165            
166             $req->content($data);
167             #Pass request to the user agent and get a response back
168             my $response = $ua->request($req);
169            
170            
171             #Check the outcome of the response
172             if ($response->is_success) {
173             $self->log("Client: oxd_http_response", $response);
174             } else {
175             $self->log("Client: oxd_http_response", 'Error while processing.');
176             }
177            
178             return $response;
179             }
180            
181            
182            
183             # Showing errors and exit.
184             # @param string $error
185             # @return void
186             sub error_message{
187             my ($self, $error) = @_;
188             print '
' . $error.'
';
189             exit($error);
190             }
191            
192             # Saving process in log file.
193             # @param string $process
194             # @param string $message
195             # @return void
196             sub log{
197             my ($self, $process, $message) = @_;
198            
199             my $t = localtime;
200             my $timeStamp = $t->mdy("-");# 02/29/2000
201            
202             my $fileName = "oxd-perl-server-$timeStamp.log";
203            
204             my $datestring = localtime();
205            
206             my $filename = "logs/$fileName";
207             open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
208             my $person = "\n".$datestring."\n".$process." ".$message."\n";
209             say $fh "$person\n";
210             close $fh;
211             #say "done\n";
212             }
213            
214             1; # this 1; is neccessary for our class to work