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/OxdRemove.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) 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 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-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 if($connectionType eq 'local') {
184 $response_json = $self->oxd_socket_request($lenght_jsondata);
185 my $char_count = substr($response_json, 0, 4);
186 $response_json =~ s/$char_count//g;
187 }
188 elsif($connectionType eq 'web') {
189 $response_json = $self->oxd_http_request($httpParams, $httpcommand);
190 $response_json = $response_json->{_content};
191 }
192
193 $self->{_response_json} = $response_json if defined($response_json);
194 if ( $response_json) {
195 my $object = JSON::PP->new->utf8->decode($response_json);
196
197 if ($object->{status} eq 'error') {
198 if($object->{data}->{error} eq 'need_info') {
199 $self->setResponseObject( $object );
200 }
201 else {
202 $self->error_message($object->{data}->{error} . ' : ' . $object->{data}->{error_description});
203 }
204 } elsif ($object->{status} eq 'ok') {
205 $self->setResponseObject( $object );
206 }
207 } else {
208 print "I am here";
209 $self->log("Response is empty...",'Exiting process.');
210 $self->error_message('Response is empty...');
211 }
212 }
213
214
215 # Response status
216 # @return string, OK on success, error on failure
217 sub getResponseStatus
218 {
219 my ($self) = @_;
220 return $self->{_response_status};
221 }
222
223
224 # Setting response status
225 # @return void
226 sub setResponseStatus
227 {
228 my ( $self) = @_;
229 $self->{_response_status} = $self->getResponseObject()->{status};
230 return $self->{_response_status};
231 }
232
233
234 # Method: getResponseData
235 # If data is not empty it is returning response data from oxd in array format.
236 # If data empty or error , you have problem with parameter or protocol.
237 #
238 # Parameters:
239 #
240 # object ResponseObject
241 #
242 # Returns:
243 # object ResponseData
244 sub getResponseData{
245 my ($self) = @_;
246 if (!$self->getResponseObject()) {
247 $self->{_response_data} = 'Data is empty';
248 $self->error_message($self->{_response_data});
249 } else {
250 $self->{_response_data} = $self->getResponseObject()->{data};
251 }
252 return $self->{_response_data};
253 }
254
255 # Method: getData
256 # Data which need to send to oxd server.
257 #
258 # Parameters:
259 #
260 # string $command - oxd-server command name
261 #
262 # dict $params - oxd-server request parameters
263 #
264 # Returns:
265 # object RequestObject
266 sub getData{
267 my ($self) = @_;
268
269 my $data = {
270 "command" => $self->getCommand(),
271 "params" => $self->getParams(),
272 };
273
274 #my @data = ('command' => $self->getCommand(), 'params' => $self->getParams());
275 return $data;
276 }
277
278
279 # Protocol name for request.
280 # @return string
281
282 sub getCommand{
283 my ($self) = @_;
284 return $self->{_command};
285 #return 'register_site';
286 }
287
288 # Protocol name for request.
289 # @return string
290
291 sub gethttpCommand{
292 my ($self) = @_;
293 return $self->{_httpcommand};
294 }
295
296 # Setting protocol name for request.
297 # @return void
298 #sub setCommand : Abstract;
299
300 # If response data is not empty it is returning response data from oxd-server in format object.
301 # If response data empty or error , you have problem with parameter or protocol.
302 #
303 # @return object
304
305 sub setResponseObject{
306 my ( $self, $response_object ) = @_;
307 $self->{_response_object} = $response_object if defined($response_object);
308 return $self->{_response_object};
309 }
310
311
312 # Method: getResponseObject
313 # It returns response object from oxd-server in json format.
314 #
315 # Returns:
316 # object ResponseObject
317 sub getResponseObject{
318 my ($self) = @_;
319 return $self->{_response_object};
320 }
321
322
323
324 # @return string
325
326 sub getResponseJSON{
327 my ($self) = @_;
328 return $self->{_response_json};
329 }
330
331
332 # Setting parameters for request.
333 # @return void
334 #sub setParams : Abstract;
335
336
337 # Parameters for request.
338 # @return array
339 sub getParams{
340 my ($self) = @_;
341 return $self->{_params};
342 }
343
344
345 # Checking format string.
346 # @param string $string
347 # @return bool
348 sub is_JSON{
349 # Get passed arguments
350 my($self,$jsondata) = @_;
351 my $json_out = eval { decode_json($jsondata) };
352 if ($@){
353 #print "Error: $@";
354 return 0;
355 }else{
356 return 1;
357 #print "OK!\n";
358 }
359 }
360
361 1; # this 1; is neccessary for our class to work
« OxdPackages/OxdRemove.pm ↑ Summary OxdPackages/OxdRegister.pm »