Skip to content

Commit d7d2264

Browse files
committed
Added AuthorizedNetSensitiveTagsConfig.json to sdk-php instead of only client.
1 parent 4a4a3c4 commit d7d2264

4 files changed

Lines changed: 53 additions & 171 deletions

File tree

lib/net/authorize/api/controller/base/ApiOperationBase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ public function executeWithApiResponse($endPoint = \net\authorize\api\constants\
111111
public function execute($endPoint = \net\authorize\api\constants\ANetEnvironment::CUSTOM)
112112
{
113113
$this->beforeExecute();
114-
// $this->logger->debugFormat("Request object :%s", $this->apiRequest);
115-
$this->logger->debug($this->apiRequest);
114+
116115
$this->logger->info("Request Serialization Begin");
117116
$xmlRequest = $this->serializer->serialize($this->apiRequest, 'xml');
118117
$this->logger->info("Request Serialization End");

lib/net/authorize/util/ANetSensitiveFields.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ private static function getDefaulSensitiveXmlTags(){
1919
}
2020
public static function getSensitiveXmlTags(){
2121
$sensitiveTags = array();
22-
if (file_exists(ANET_SENSITIVE_XMLTAGS_JSON_FILE)) {
22+
$configFilePath = dirname(__FILE__) . "/" . ANET_SENSITIVE_XMLTAGS_JSON_FILE;
23+
if (file_exists($configFilePath)) {
2324
//read list of tags(and associate regex-patterns and replacements) from .json file
24-
$sensitiveTags = json_decode(file_get_contents(ANET_SENSITIVE_XMLTAGS_JSON_FILE));
25+
$sensitiveTags = json_decode(file_get_contents($configFilePath));
2526
if (json_last_error() === JSON_ERROR_NONE) {
2627
// JSON is valid
2728
}
2829
else{
29-
echo "ERROR: Invalid json in: " . ANET_SENSITIVE_XMLTAGS_JSON_FILE . " json_last_error_msg : " . json_last_error_msg();
30+
echo "ERROR: Invalid json in: " . $configFilePath . " json_last_error_msg : " . json_last_error_msg();
3031
return self::getDefaulSensitiveXmlTags();
3132
}
3233
}
3334
else {
3435
// if not present, create a local config file
3536
$sensitiveTags = self::getDefaulSensitiveXmlTags();
36-
file_put_contents(ANET_SENSITIVE_XMLTAGS_JSON_FILE, json_encode($sensitiveTags, JSON_PRETTY_PRINT));
37+
file_put_contents($configFilePath, json_encode($sensitiveTags, JSON_PRETTY_PRINT));
3738
}
3839
//Check for disableMask flag in case of client json.
3940
$applySensitiveTags = array();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"tagName": "cardCode",
4+
"pattern": "",
5+
"replacement": "",
6+
"disableMask": false
7+
},
8+
{
9+
"tagName": "cardNumber",
10+
"pattern": "^(\\d+)(\\d{4})$",
11+
"replacement": "$xxxx-$3$4",
12+
"disableMask": false
13+
},
14+
{
15+
"tagName": "expirationDate",
16+
"pattern": "",
17+
"replacement": "",
18+
"disableMask": false
19+
}
20+
]

lib/net/authorize/util/Log.php

Lines changed: 27 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,27 @@
1616
define("ANET_LOG_INFO",2);
1717
define("ANET_LOG_WARN",3);
1818
define("ANET_LOG_ERROR",4);
19-
2019
//set level
2120
define("ANET_LOG_LEVEL",ANET_LOG_DEBUG);
2221

23-
/**
24-
* A class to implement logging.
25-
*
26-
* @package AuthorizeNet
27-
* @subpackage net\authorize\util
28-
*/
29-
3022
class Log
3123
{
3224
private $sensitiveXmlTags = NULL;
33-
34-
/**
35-
* Takes an xml as string and masks the sensitive fields.
36-
*
37-
* @param string $rawString The xml as a string.
38-
*
39-
* @return string The xml as a string after masking sensitive fields
40-
*/
25+
4126
private function maskSensitiveXmlString($rawString){
27+
//Tag name is compulsory, can leave patterns and repalcements blank
28+
// $tags= array("cardCode","cardNumber","expirationDate");
29+
// $patterns=array("","([^0-9]*)(\d+)(\d{4})(.*)","");
4230
$patterns=array();
31+
// $replacements=array("","$1xxxx-$3$4","");
4332
$replacements=array();
44-
4533
foreach ($this->sensitiveXmlTags as $i => $sensitiveTag){
4634
$tag = $sensitiveTag->tagName;
47-
$inputPattern = "(.+)"; //no need to mask null data
48-
$inputReplacement = "xxxx";
35+
$inputPattern = $sensitiveTag->pattern;
36+
$inputReplacement = "XXXX";
4937

50-
if(trim($sensitiveTag->pattern)) {
51-
$inputPattern = $sensitiveTag->pattern;
38+
if(!trim($inputPattern)) {
39+
$inputPattern = "(.+)"; //no need to mask null data
5240
}
5341
$pattern = "/<" . $tag . ">". $inputPattern ."<\/" . $tag . ">/";
5442

@@ -64,138 +52,32 @@ private function maskSensitiveXmlString($rawString){
6452
return $maskedString;
6553
}
6654

67-
/**
68-
* Object data masking related functions START
69-
*/
70-
71-
/**
72-
* private function getPropertiesInclBase($reflClass).
73-
*
74-
* Receives a ReflectionObject, ...
75-
* iteratively fetches the properties of the object (including from the base classes up the hierarchy), ...
76-
* collects them in an array of ReflectionProperty and returns the array.
77-
*
78-
* @param ReflectionObject $reflClass
79-
*
80-
* @return \ReflectionProperty[]
81-
*/
82-
private function getPropertiesInclBase($reflClass)
83-
{
84-
$properties = array();
85-
try {
86-
do {
87-
$curClassPropList = $reflClass->getProperties();
88-
foreach ($curClassPropList as $p) {
89-
$p->setAccessible(true);
90-
}
91-
$properties = array_merge($curClassPropList, $properties);
92-
} while ($reflClass = $reflClass->getParentClass());
93-
} catch (\ReflectionException $e) { }
94-
return $properties;
95-
}
96-
97-
/**
98-
* private function checkAndMask($prop, $obj).
99-
*
100-
* Receives a ReflectionProperty and an object, and returns a masked object if the ReflectionProperty corresponds to a sensitive field, else returns false.
101-
*
102-
* @param ReflectionProperty $prop
103-
* @param object $obj
104-
*
105-
* @return string|bool
106-
*/
107-
private function checkAndMask($prop, $obj){
108-
foreach($this->sensitiveXmlTags as $i => $sensitiveTag)
109-
{
110-
$inputPattern = "(.+)";
111-
$inputReplacement = "xxxx";
112-
113-
if(trim($sensitiveTag->pattern)) {
114-
$inputPattern = $sensitiveTag->pattern;
115-
}
116-
$inputPattern='/'.$inputPattern.'/';
117-
118-
if(trim($sensitiveTag->replacement)) {
119-
$inputReplacement = $sensitiveTag->replacement;
120-
}
121-
122-
if(strcmp($prop->getName(),$sensitiveTag->tagName)==0)
123-
{
124-
$prop->setValue($obj,preg_replace($inputPattern,$inputReplacement,$prop->getValue($obj)));
125-
return $prop->getValue($obj);
126-
}
127-
}
128-
return false;
129-
}
130-
131-
/**
132-
* called by getMasked() to mask sensitive fields of an object.
133-
*
134-
* @param object $obj
135-
*
136-
* @return object
137-
*/
138-
private function maskSensitiveProperties ($obj)
139-
{
140-
// first retrieve all properties of the passed object
141-
$reflectObj = new \ReflectionObject($obj);
142-
$props = $this->getPropertiesInclBase($reflectObj);
143-
144-
// for composite property recursively execute; for scalars, do a check and mask
145-
foreach($props as $i => $prop){
146-
$propValue=$prop->getValue($obj);
147-
148-
// for object and arrays, recursively call for inner elements
149-
if(is_object($propValue)){
150-
$prop->setValue($obj, $this->maskSensitiveProperties($propValue));
151-
}
152-
else if(is_array($propValue)){
153-
$newVals=array();
154-
foreach($propValue as $i=>$arrEle)
155-
{
156-
$newVals[]=$this->maskSensitiveProperties($arrEle);
157-
}
158-
$prop->setValue($obj, $newVals);
159-
}
160-
// else check if the property represents a sensitive field. If so, mask.
161-
else{
162-
$res=$this->checkAndMask($prop, $obj);
163-
if($res)
164-
$prop->setValue($obj, $res);
165-
}
166-
}
167-
168-
return $obj;
169-
}
170-
171-
/**
172-
* Object data masking related functions END
173-
*/
174-
175-
/**
176-
* private function getMasked($raw).
177-
*
178-
* called by log()
179-
*
180-
* @param mixed $raw
181-
*
182-
* @return string
183-
*/
18455
private function getMasked($raw)
185-
{ //always returns string
56+
{
18657
$messageType = gettype($raw);
18758
$message="";
18859
if ($messageType == "string") {
18960
$message = $this->maskSensitiveXmlString($raw);
19061
}
191-
else if($messageType == "object"){
192-
$obj = unserialize(serialize($raw)); // deep copying the object
193-
$message = print_r($this->maskSensitiveProperties($obj), true); //object to string
194-
}
19562
return $message;
19663
}
197-
198-
private function log($logLevelPrefix, $logMessage, $flags){
64+
public function debug($logMessage, $flags=FILE_APPEND)
65+
{
66+
if(ANET_LOG_DEBUG >= ANET_LOG_LEVEL){
67+
$this->log(ANET_LOG_DEBUG_PREFIX, $logMessage,$flags);
68+
}
69+
}
70+
public function info($logMessage, $flags=FILE_APPEND){
71+
if(ANET_LOG_INFO >= ANET_LOG_LEVEL) {
72+
$this->log(ANET_LOG_INFO_PREFIX, $logMessage,$flags);
73+
}
74+
}
75+
public function error($logMessage, $flags=FILE_APPEND){
76+
if(ANET_LOG_ERROR >= ANET_LOG_LEVEL) {
77+
$this->log(ANET_LOG_ERROR_PREFIX, $logMessage,$flags);
78+
}
79+
}
80+
private function log($logLevelPrefix, $logMessage, $flags){
19981
//masking
20082
$logMessage = $this->getMasked($logMessage);
20183

@@ -215,26 +97,6 @@ private function log($logLevelPrefix, $logMessage, $flags){
21597
$methodName, $fileName, $lineNumber, $logMessage);
21698
file_put_contents(ANET_LOG_FILE, $logString, $flags);
21799
}
218-
219-
public function debug($logMessage, $flags=FILE_APPEND)
220-
{
221-
if(ANET_LOG_DEBUG >= ANET_LOG_LEVEL){
222-
$this->log(ANET_LOG_DEBUG_PREFIX, $logMessage,$flags);
223-
}
224-
}
225-
226-
public function info($logMessage, $flags=FILE_APPEND){
227-
if(ANET_LOG_INFO >= ANET_LOG_LEVEL) {
228-
$this->log(ANET_LOG_INFO_PREFIX, $logMessage,$flags);
229-
}
230-
}
231-
232-
public function error($logMessage, $flags=FILE_APPEND){
233-
if(ANET_LOG_ERROR >= ANET_LOG_LEVEL) {
234-
$this->log(ANET_LOG_ERROR_PREFIX, $logMessage,$flags);
235-
}
236-
}
237-
238100
public function __construct(){
239101
$this->sensitiveXmlTags = ANetSensitiveFields::getSensitiveXmlTags();
240102
}

0 commit comments

Comments
 (0)