OxdPackages/OxdClient.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« OxdPackages/UmaRsProtect.pm ↑ Summary OxdPackages/OxdRegister.pm »
1 #!/usr/bin/perl
2 # OxdClient.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 Attribute::Abstract;
44
45 package OxdClient; # This is the "Class"
46
47 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 $VERSION = '0.01';
49
50 0 ▶ use OxdPackages::OxdClientSocket;
Subroutine BEGIN: called CC=1 SCAR=2.6
51 our @ISA = qw(OxdClientSocket); # inherits from OxdClient
52 # makes all attributes available
53 use lib './modules';
54 use Attribute::Handlers;
55 use strict;
56 use warnings;
57 use JSON::PP;
58 use OxdPackages::OxdConfig;
59 use Data::Dumper qw(Dumper);
60 use utf8;
61 use Encode;
62
63
64 sub new {
65 my $class = shift;
66 my $self = {
67 # @var string $command Extend class protocol command name, for sending oxd-server
68 _command=>shift,
69
70 # @var string $httpcommand Extend class protocol command name, for sending oxd-tohttp server
71 _httpcommand=>shift,
72
73 # @var string $params Extends class sending parameters to oxd-server
74 _params => [],
75
76 # @var string $data Response data from oxd-server
77 _data => [],
78
79 # @var string $response_json Response data from oxd-server in format json
80 _response_json=>shift,
81
82 # @var object $response_object Response data from oxd-server in format object
83 _response_object=>shift,
84
85 # @var string $response_status Response status from oxd-server
86 _response_status=>shift,
87
88 # @var array $response_data Response data from oxd-server in format array
89 _response_data => [],
90
91
92 };
93 # Print all the values just for clarification.
94 #print "First Name is $self->{_firstName}\n";
95 bless $self, $class;
96
97 return $self;
98 }
99
100 # send function sends the command to the oxd server.
101 # Args:
102 # command (dict) - Dict representation of the JSON command string
103 # @return void
104 #
105
106 sub request{
107 my ($self) = @_;
108
109 # @var array $command_types Protocols commands name
110 my @command_types = ("get_authorization_url",
111 "update_site_registration",
112 "get_tokens_by_code",
113 "get_user_info",
114 "register_site",
115 "get_logout_uri",
116 "get_authorization_code",
117 "uma_rs_protect",
118 "uma_rs_check_access",
119 "uma_rp_get_rpt",
120 "uma_rp_authorize_rpt",
121 "uma_rp_get_gat");
122
123
124 $self->setCommand();
125 $self->sethttpCommand();
126
127 my $exist = 'false';
128 for (my $i=0; $i <= scalar @command_types; $i++) {
129 if ($command_types[$i] eq $self->getCommand()) {
130 my $exist = 'true';
131 last;
132 }
133 }
134
135
136 if (!$exist) {
137 $self->log('Command: ' . $self->getCommand() . ' is not exist!','Exiting process.');
138 $self->error_message('Command: ' . $self->getCommand() . ' is not exist!');
139 }
140
141 $self->setParams();
142
143 my $oxdConfig = OxdConfig->new();
144 my $connectionType = $oxdConfig->{'_connection_type'};
145
146 my $json_array = $self->getData();
147 my $http_json = $self->getParams();
148 my $httpcommand = $self->gethttpCommand();
149
150 my $json = JSON::PP->new;
151
152 my $jsondata = $json->encode($json_array);
153 my $httpParams = $json->encode($http_json);
154
155
156 if(!$self->is_JSON($jsondata)){
157 $self->log("Sending parameters must be JSON.",'Exiting process.');
158 $self->error_message('Sending parameters must be JSON.');
159 }
160
161 my $lenght = length $jsondata;
162
163 if($lenght<=0){
164 $self->log("Length must be more than zero.",'Exiting process.');
165 $self->error_message("Length must be more than zero.");
166 }else{
167 $lenght = $lenght <= 999 ? "0" . $lenght : $lenght;
168 }
169
170 my $lenght_jsondata = encode('UTF-8', $lenght . $jsondata);
171
172 my $response_json = "";
173
174 if($connectionType eq 'local') {
175 $response_json = $self->oxd_socket_request($lenght_jsondata);
176 my $char_count = substr($response_json, 0, 4);
177 $response_json =~ s/$char_count//g;
178 }
179 elsif($connectionType eq 'web') {
180 $response_json = $self->oxd_http_request($httpParams, $httpcommand);
181 $response_json = $response_json->{_content};
182 }
183
184 $self->{_response_json} = $response_json if defined($response_json);
185 if ( $response_json) {
186 my $object = JSON::PP->new->utf8->decode($response_json);
187 #print $object->{data}->{oxd_id};
188 if ($object->{status} eq 'error') {
189 $self->error_message($object->{data}->{error} . ' : ' . $object->{data}->{error_description});
190 } elsif ($object->{status} eq 'ok') {
191 $self->setResponseObject( $object );
192 }
193 } else {
194 print "I am here";
195 $self->log("Response is empty...",'Exiting process.');
196 $self->error_message('Response is empty...');
197 }
198 }
199
200
201 # Response status
202 # @return string, OK on success, error on failure
203 sub getResponseStatus
204 {
205 my ($self) = @_;
206 return $self->{_response_status};
207 }
208
209
210 # Setting response status
211 # @return void
212 sub setResponseStatus
213 {
214 my ( $self) = @_;
215 $self->{_response_status} = $self->getResponseObject()->{status};
216 return $self->{_response_status};
217 }
218
219
220 # If data is not empty it is returning response data from oxd-server in format array.
221 # If data empty or error , you have problem with parameter or protocol.
222 # @return array
223 sub getResponseData{
224 my ($self) = @_;
225 if (!$self->getResponseObject()) {
226 $self->{_response_data} = 'Data is empty';
227 $self->error_message($self->{_response_data});
228 } else {
229 $self->{_response_data} = $self->getResponseObject()->{data};
230 }
231 return $self->{_response_data};
232 }
233
234 # Data which need to send oxd server.
235 # @return array
236 sub getData{
237 my ($self) = @_;
238
239 my $data = {
240 "command" => $self->getCommand(),
241 "params" => $self->getParams(),
242 };
243
244 #my @data = ('command' => $self->getCommand(), 'params' => $self->getParams());
245 return $data;
246 }
247
248
249 # Protocol name for request.
250 # @return string
251
252 sub getCommand{
253 my ($self) = @_;
254 return $self->{_command};
255 #return 'register_site';
256 }
257
258 # Protocol name for request.
259 # @return string
260
261 sub gethttpCommand{
262 my ($self) = @_;
263 return $self->{_httpcommand};
264 }
265
266 # Setting protocol name for request.
267 # @return void
268 #sub setCommand : Abstract;
269
270 # If response data is not empty it is returning response data from oxd-server in format object.
271 # If response data empty or error , you have problem with parameter or protocol.
272 #
273 # @return object
274
275 sub setResponseObject{
276 my ( $self, $response_object ) = @_;
277 $self->{_response_object} = $response_object if defined($response_object);
278 return $self->{_response_object};
279 }
280
281
282 # If response data is not empty it is returning response data from oxd-server in format object.
283 # If response data empty or error , you have problem with parameter or protocol.
284 #
285 # @return object
286
287 sub getResponseObject{
288 my ($self) = @_;
289 return $self->{_response_object};
290 }
291
292
293 # If response data is not empty it is returning response data from oxd-server in format json.
294 # If response data empty or error , you have problem with parameter or protocol.
295 # @return string
296
297 sub getResponseJSON{
298 my ($self) = @_;
299 return $self->{_response_json};
300 }
301
302
303 # Setting parameters for request.
304 # @return void
305 #sub setParams : Abstract;
306
307
308 # Parameters for request.
309 # @return array
310 sub getParams{
311 my ($self) = @_;
312 return $self->{_params};
313 }
314
315
316 # Checking format string.
317 # @param string $string
318 # @return bool
319 sub is_JSON{
320 # Get passed arguments
321 my($self,$jsondata) = @_;
322 my $json_out = eval { decode_json($jsondata) };
323 if ($@){
324 #print "Error: $@";
325 return 0;
326 }else{
327 return 1;
328 #print "OK!\n";
329 }
330 }
331
332 1; # this 1; is neccessary for our class to work
« OxdPackages/UmaRsProtect.pm ↑ Summary OxdPackages/OxdRegister.pm »