I'm not experienced with PHP and have to redact the password in the following code:
$body=array(
'userInfo' => array(
'userName' => $username,
'password' => $password,
'applicationKey' => $appKey,
),
);
The variable $body
is used both in application logic, and in logging the array:
$this->logger->debug("REQUEST: URL[{$this->config->endpoint}] BODY: " . json_encode($body));
But I should not be logging the password. I'd like to redact it.
I know I could just copy and paste the array to a new variable $body_with_password_hidden
but I'd like to learn some PHP idioms, not to mention keep the code base as compact as realistically possible.
What would be an elegant way to do this? In a language I am more comfortable, I would clone the dictionary, and overwrite the sensitive value. How would I do this in PHP?
More info
Current log statement:
[2018-04-10T18:23:11+00:00] [DEBUG] REQUEST: URL[http://myservice.com/myendpoint/login] BODY: {"userInfo":{"userName":"Administrator","password":"Administrator","applicationKey":"abc123"}} -
Desired log statement:
[2018-04-10T18:23:11+00:00] [DEBUG] REQUEST: URL[http://myservice.com/myendpoint/login] BODY: {"userInfo":{"userName":"Administrator","password":"********","applicationKey":"abc123"}} -