blib/lib/OxdPackages/OxdClientSocket.pm

filter: Statementstmt 95.1% 39 / 41 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 96.3% 53 / 55 CC 1 SCAR 0.2
CC
1
Cov
95%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/OxdPerlModule.pm ↑ Summary
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) 2018, 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.3
33 # @author Sobhan Panda
34 # @author_email sobhan@centroxy.com
35 # @copyright Copyright (c) 2018, 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.3
39 # @filesource
40 #/
41
42
43 package OxdClientSocket; # This is the "Class"
44
45 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 $VERSION = '0.01';
47 # makes all attributes available
48 1 ▶ use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶ use lib './modules';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶ use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 #use strict;
52 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶ use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶ use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶ use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶ use OxdPackages::OxdConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 #use IO::Socket::Socks;
62 1 ▶ use CGI::Session;#Test
Subroutine BEGIN: called CC=1 SCAR=2.6
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 '<div class="alert alert-danger"> ' . $error.'</div>';
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
« blib/lib/OxdPerlModule.pm ↑ Summary