blib/lib/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
« blib/lib/OxdPackages/OxdRemove.pm ↑ Summary blib/lib/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) 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 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 1 ▶ 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-https-extension
71 _httpcommand=>shift,
72
73 # @var string $params Extends class sending parameters to oxd
74 _params => [],
75
76 # @var string $data Response data from oxd
77 _data => [],
78
79 # @var string $response_json Response data from oxd in format json
80 _response_json=>shift,
81
82 # @var object $response_object Response data from oxd in format object
83 _response_object=>shift,
84
85 # @var string $response_status Response status from oxd
86 _response_status=>shift,
87
88 # @var array $response_data Response data from oxd 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
107 # Method: request
108 # This method is called for sending request to oxd for all the oxd commands.
109 #
110 # Parameters:
111 #
112 # object RequestObject
113 #
114 # Returns:
115 # void
116 sub request {
117 my ($self) = @_;
118
119 # @var array $command_types Protocols commands name
120 my @command_types = ("get_authorization_url",
121 "update_site_registration",
122 "get_tokens_by_code",
123 "get_user_info",
124 "register_site",
125 "get_logout_uri",
126 "get_authorization_code",
127 "uma_rs_protect",
128 "uma_rs_check_access",
129 "uma_rp_get_rpt",
130 "uma_rp_authorize_rpt",
131 "uma_rp_get_gat");
132
133
134 $self->setCommand();
135 $self->sethttpCommand();
136
137 my $exist = 'false';
138 for (my $i=0; $i <= scalar @command_types; $i++) {
139 if ($command_types[$i] eq $self->getCommand()) {
140 my $exist = 'true';
141 last;
142 }
143 }
144
145
146 if (!$exist) {
147 $self->log('Command: ' . $self->getCommand() . ' is not exist!','Exiting process.');
148 $self->error_message('Command: ' . $self->getCommand() . ' is not exist!');
149 }
150
151 $self->setParams();
152
153 my $oxdConfig = OxdConfig->new();
154 my $connectionType = $oxdConfig->{'_connection_type'};
155
156 my $json_array = $self->getData();
157 my $http_json = $self->getParams();
158 my $httpcommand = $self->gethttpCommand();
159
160 my $json = JSON::PP->new;
161
162 my $jsondata = $json->encode($json_array);
163 my $httpParams = $json->encode($http_json);
164
165
166 if(!$self->is_JSON($jsondata)){
167 $self->log("Sending parameters must be JSON.",'Exiting process.');
168 $self->error_message('Sending parameters must be JSON.');
169 }
170
171 my $lenght = length $jsondata;
172
173 if($lenght<=0){
174 $self->log("Length must be more than zero.",'Exiting process.');
175 $self->error_message("Length must be more than zero.");
176 }else{
177 $lenght = $lenght <= 999 ? "0" . $lenght : $lenght;
178 }
179
180 my $lenght_jsondata = encode('UTF-8', $lenght . $jsondata);
181
182 my $response_json = "";
183
184 if($connectionType eq 'local') {
185 $response_json = $self->oxd_socket_request($lenght_jsondata);
186 my $char_count = substr($response_json, 0, 4);
187 $response_json =~ s/$char_count//g;
188 }
189 elsif($connectionType eq 'web') {
190 $response_json = $self->oxd_http_request($httpParams, $httpcommand);
191 $response_json = $response_json->{_content};
192 }
193
194 $self->{_response_json} = $response_json if defined($response_json);
195 if ( $response_json) {
196 my $object = JSON::PP->new->utf8->decode($response_json);
197
198 if ($object->{status} eq 'error') {
199 if($object->{data}->{error} eq 'need_info') {
200 $self->setResponseObject( $object );
201 }
202 else {
203 $self->error_message($object->{data}->{error} . ' : ' . $object->{data}->{error_description});
204 }
205 } elsif ($object->{status} eq 'ok') {
206 $self->setResponseObject( $object );
207 }
208 } else {
209 print "I am here";
210 $self->log("Response is empty...",'Exiting process.');
211 $self->error_message('Response is empty...');
212 }
213 }
214
215
216 # Response status
217 # @return string, OK on success, error on failure
218 sub getResponseStatus
219 {
220 my ($self) = @_;
221 return $self->{_response_status};
222 }
223
224
225 # Setting response status
226 # @return void
227 sub setResponseStatus
228 {
229 my ( $self) = @_;
230 $self->{_response_status} = $self->getResponseObject()->{status};
231 return $self->{_response_status};
232 }
233
234
235 # Method: getResponseData
236 # If data is not empty it is returning response data from oxd in array format.
237 # If data empty or error , you have problem with parameter or protocol.
238 #
239 # Parameters:
240 #
241 # object ResponseObject
242 #
243 # Returns:
244 # object ResponseData
245 sub getResponseData{
246 my ($self) = @_;
247 if (!$self->getResponseObject()) {
248 $self->{_response_data} = 'Data is empty';
249 $self->error_message($self->{_response_data});
250 } else {
251 $self->{_response_data} = $self->getResponseObject()->{data};
252 }
253 return $self->{_response_data};
254 }
255
256 # Method: getData
257 # Data which need to send to oxd server.
258 #
259 # Parameters:
260 #
261 # string $command - oxd-server command name
262 #
263 # dict $params - oxd-server request parameters
264 #
265 # Returns:
266 # object RequestObject
267 sub getData{
268 my ($self) = @_;
269
270 my $data = {
271 "command" => $self->getCommand(),
272 "params" => $self->getParams(),
273 };
274
275 #my @data = ('command' => $self->getCommand(), 'params' => $self->getParams());
276 return $data;
277 }
278
279
280 # Protocol name for request.
281 # @return string
282
283 sub getCommand{
284 my ($self) = @_;
285 return $self->{_command};
286 #return 'register_site';
287 }
288
289 # Protocol name for request.
290 # @return string
291
292 sub gethttpCommand{
293 my ($self) = @_;
294 return $self->{_httpcommand};
295 }
296
297 # Setting protocol name for request.
298 # @return void
299 #sub setCommand : Abstract;
300
301 # If response data is not empty it is returning response data from oxd-server in format object.
302 # If response data empty or error , you have problem with parameter or protocol.
303 #
304 # @return object
305
306 sub setResponseObject{
307 my ( $self, $response_object ) = @_;
308 $self->{_response_object} = $response_object if defined($response_object);
309 return $self->{_response_object};
310 }
311
312
313 # Method: getResponseObject
314 # It returns response object from oxd-server in json format.
315 #
316 # Returns:
317 # object ResponseObject
318 sub getResponseObject{
319 my ($self) = @_;
320 return $self->{_response_object};
321 }
322
323
324
325 # @return string
326
327 sub getResponseJSON{
328 my ($self) = @_;
329 return $self->{_response_json};
330 }
331
332
333 # Setting parameters for request.
334 # @return void
335 #sub setParams : Abstract;
336
337
338 # Parameters for request.
339 # @return array
340 sub getParams{
341 my ($self) = @_;
342 return $self->{_params};
343 }
344
345
346 # Checking format string.
347 # @param string $string
348 # @return bool
349 sub is_JSON{
350 # Get passed arguments
351 my($self,$jsondata) = @_;
352 my $json_out = eval { decode_json($jsondata) };
353 if ($@){
354 #print "Error: $@";
355 return 0;
356 }else{
357 return 1;
358 #print "OK!\n";
359 }
360 }
361
362 1; # this 1; is neccessary for our class to work
« blib/lib/OxdPackages/OxdRemove.pm ↑ Summary blib/lib/OxdPackages/OxdRegister.pm »