| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | ## | ||
| 4 | # Gluu-oxd-library | ||
| 5 | # | ||
| 6 | # An open source application library for Perl | ||
| 7 | # | ||
| 8 | # This content is released under the MIT License (MIT) | ||
| 9 | # | ||
| 10 | # Copyright (c) 2018, Gluu inc, USA, Austin | ||
| 11 | # | ||
| 12 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 13 | # of this software and associated documentation files (the "Software"), to deal | ||
| 14 | # in the Software without restriction, including without limitation the rights | ||
| 15 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 16 | # copies of the Software, and to permit persons to whom the Software is | ||
| 17 | # furnished to do so, subject to the following conditions: | ||
| 18 | # | ||
| 19 | # The above copyright notice and this permission notice shall be included in | ||
| 20 | # all copies or substantial portions of the Software. | ||
| 21 | # | ||
| 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 23 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 24 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 25 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 26 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 27 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 28 | # THE SOFTWARE. | ||
| 29 | # | ||
| 30 | # @package Gluu-oxd-library | ||
| 31 | # @version 3.1.2 | ||
| 32 | # @author Inderpal Singh, Sobhan Panda | ||
| 33 | # @author_email inderpal@ourdesignz.com, sobhan@centroxy.com | ||
| 34 | # @copyright Copyright (c) 2018, Gluu inc federation (https://gluu.org/) | ||
| 35 | # @license http://opensource.org/licenses/MIT MIT License | ||
| 36 | # @link https://gluu.org/ | ||
| 37 | # @since Version 3.1.2 | ||
| 38 | # @filesource | ||
| 39 | #/ | ||
| 40 | |||
| 41 | ## | ||
| 42 | # UMA RS Protect resources package | ||
| 43 | # | ||
| 44 | # Package is connecting to oxd-server via socket, and adding resources in gluu-server. | ||
| 45 | # | ||
| 46 | # @package Gluu-oxd-library | ||
| 47 | # @subpackage Libraries | ||
| 48 | # @category Relying Party (RP) and User Managed Access (UMA) | ||
| 49 | # @author Inderpal Singh, Sobhan Panda | ||
| 50 | # @author inderpal@ourdesignz.com, sobhan@centroxy.com | ||
| 51 | # @see OxdClientSocket | ||
| 52 | # @see OxdClient | ||
| 53 | # @see OxdConfig | ||
| 54 | ## | ||
| 55 | |||
| 56 | package UmaRsProtect; | ||
| 57 | use vars qw($VERSION); | ||
| 58 | $VERSION = '0.01'; | ||
| 59 | use OxdPackages::OxdClient; | ||
| 60 | use base qw(OxdClient Class::Accessor); | ||
| 61 | use strict; | ||
| 62 | our @ISA = qw(OxdClient); # inherits from OxdClient | ||
| 63 | use Data::Dumper; | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | my $class = shift; | ||
| 67 | my $self = { | ||
| 68 | # @var string $request_oxd_id This parameter you must get after registration site in gluu-server | ||
| 69 | _request_oxd_id => shift, | ||
| 70 | |||
| 71 | _request_protection_access_token => shift, | ||
| 72 | |||
| 73 | # @var array $request_resources This parameter your resources parameter | ||
| 74 | _request_resources => [], | ||
| 75 | _request_resource => [], | ||
| 76 | _request_condition => [], | ||
| 77 | }; | ||
| 78 | # Print all the values just for clarification. | ||
| 79 | #print "First Name is $self->{_request_oxd_id}\n"; | ||
| 80 | |||
| 81 | #print "<br>"; | ||
| 82 | bless $self, $class; | ||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | # @return string | ||
| 87 | sub getRequestOxdId{ | ||
| 88 | my( $self ) = @_; | ||
| 89 | return $self->{_request_oxd_id}; | ||
| 90 | } | ||
| 91 | |||
| 92 | # @param string $request_oxd_id | ||
| 93 | # @return void | ||
| 94 | sub setRequestOxdId{ | ||
| 95 | my ( $self, $request_oxd_id ) = @_; | ||
| 96 | $self->{_request_oxd_id} = $request_oxd_id if defined($request_oxd_id); | ||
| 97 | return $self->{_request_oxd_id}; | ||
| 98 | } | ||
| 99 | |||
| 100 | # @return array | ||
| 101 | sub getRequestProtectionAccessToken | ||
| 102 | { | ||
| 103 | my( $self ) = @_; | ||
| 104 | return $self->{_request_protection_access_token}; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | # @param array $request_request_protection_access_token | ||
| 109 | # @return void | ||
| 110 | sub setRequestProtectionAccessToken | ||
| 111 | { | ||
| 112 | my ( $self, $request_protection_access_token ) = @_; | ||
| 113 | $self->{_request_protection_access_token} = $request_protection_access_token if defined($request_protection_access_token); | ||
| 114 | return $self->{_request_protection_access_token}; | ||
| 115 | } | ||
| 116 | |||
| 117 | # @return array | ||
| 118 | sub getRequestResources{ | ||
| 119 | my( $self ) = @_; | ||
| 120 | return $self->{_request_resources}; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub addResource{ | ||
| 124 | my ( $self, $path ) = @_; | ||
| 125 | |||
| 126 | my @request_resources = { | ||
| 127 | 'path'=>$path, | ||
| 128 | 'conditions' => $self->{_request_condition} | ||
| 129 | }; | ||
| 130 | |||
| 131 | push $self->{_request_resources}, @request_resources; | ||
| 132 | $self->{_request_condition} = ""; | ||
| 133 | } | ||
| 134 | |||
| 135 | # Method: addConditionForPath | ||
| 136 | # | ||
| 137 | # Parameters: | ||
| 138 | # | ||
| 139 | # array $httpMethods - List of HTTP Methods supported in a condition | ||
| 140 | # | ||
| 141 | # array $scopes - List of Scopes in a condition | ||
| 142 | # | ||
| 143 | # array $ticketScopes - List of Scopes protected with ticket in a condition | ||
| 144 | # | ||
| 145 | # dict $scope_expression - Scope expression for logical operations | ||
| 146 | # | ||
| 147 | # Returns: | ||
| 148 | # dict Condition | ||
| 149 | sub addConditionForPath{ | ||
| 150 | my ( $self, $httpMethods, $scopes, $ticketScopes, $scope_expression ) = @_; | ||
| 151 | |||
| 152 | my @request_condition = { | ||
| 153 | "httpMethods" => $httpMethods, | ||
| 154 | "scopes" => $scopes, | ||
| 155 | "ticketScopes" => $ticketScopes, | ||
| 156 | "scope_expression" => $scope_expression | ||
| 157 | }; | ||
| 158 | |||
| 159 | push $self->{_request_condition}, @request_condition; | ||
| 160 | return $self->{_request_condition}; | ||
| 161 | } | ||
| 162 | sub getCondition{ | ||
| 163 | my( $self ) = @_; | ||
| 164 | return $self->{_request_condition}; | ||
| 165 | } | ||
| 166 | |||
| 167 | # Method: getScopeExpression | ||
| 168 | # | ||
| 169 | # Parameters: | ||
| 170 | # | ||
| 171 | # dict $rule - Rule | ||
| 172 | # | ||
| 173 | # array $data - Data | ||
| 174 | # | ||
| 175 | # Returns: | ||
| 176 | # dict ScopeExpression | ||
| 177 | sub getScopeExpression { | ||
| 178 | my ( $self, $request_rule, $request_data ) = @_; | ||
| 179 | |||
| 180 | my $request_scope_expression = { | ||
| 181 | "rule" => $request_rule, | ||
| 182 | "data" => $request_data, | ||
| 183 | }; | ||
| 184 | |||
| 185 | #push $self->{_request_scope_expression}, @request_scope_expression; | ||
| 186 | return $request_scope_expression; | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | # Protocol command to oxd server | ||
| 191 | # @return void | ||
| 192 | sub setCommand{ | ||
| 193 | my ( $self ) = @_; | ||
| 194 | $self->{_command} = 'uma_rs_protect'; | ||
| 195 | } | ||
| 196 | |||
| 197 | # Protocol command to oxd to http server | ||
| 198 | # @return void | ||
| 199 | sub sethttpCommand { | ||
| 200 | my ( $self ) = @_; | ||
| 201 | $self->{_httpcommand} = 'uma-rs-protect'; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Method: setParams | ||
| 205 | # This method sets the parameters for uma_rs_protect command. | ||
| 206 | # This module uses `request` method of OxdClient module for sending request to oxd-server | ||
| 207 | # | ||
| 208 | # Parameters: | ||
| 209 | # | ||
| 210 | # string $oxd_id - (Required) oxd Id from Client registration | ||
| 211 | # | ||
| 212 | # dict $resources - (Required) Resources to be protected | ||
| 213 | # | ||
| 214 | # string $protection_access_token - Protection Acccess Token. OPTIONAL for `oxd-server` but REQUIRED for `oxd-https-extension` | ||
| 215 | # | ||
| 216 | # Returns: | ||
| 217 | # void | ||
| 218 | # | ||
| 219 | # This module uses `getResponseObject` method of OxdClient module for getting response from oxd. | ||
| 220 | # | ||
| 221 | # *Example response from getResponseObject:* | ||
| 222 | # --- Code | ||
| 223 | # { "status": "ok" } | ||
| 224 | # --- | ||
| 225 | # | ||
| 226 | sub setParams{ | ||
| 227 | my ( $self, $params ) = @_; | ||
| 228 | my $paramsArray = { | ||
| 229 | "oxd_id" => $self->getRequestOxdId(), | ||
| 230 | "resources" => $self->getRequestResources(), | ||
| 231 | "protection_access_token" => $self->getRequestProtectionAccessToken() | ||
| 232 | |||
| 233 | }; | ||
| 234 | # print Dumper $paramsArray; | ||
| 235 | $self->{_params} = $paramsArray; | ||
| 236 | |||
| 237 | #exit 1; | ||
| 238 | return $self->{_params}; | ||
| 239 | } | ||
| 240 | |||
| 241 | 1; # this 1; is neccessary for our class to work |