Skip to content

Commit 5efcc2d

Browse files
committed
More logging - now configurable.
1 parent 58521af commit 5efcc2d

File tree

6 files changed

+81
-34
lines changed

6 files changed

+81
-34
lines changed

config/packages/dev/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
# Put parameters here that don't need to change on each machine where the app is deployed
55
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
66
parameters:
7+
env(LOG_PASSWORDS): true
8+
env(HASH_PASSWORDS): false
79
log_requests: true
810
log_responses: true
11+
log_passwords: '%env(bool:LOG_PASSWORDS)%'
12+
hash_passwords: '%env(bool:HASH_PASSWORDS)%'

config/packages/prod/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
parameters:
77
log_requests: false
88
log_responses: false
9+
log_passwords: false
10+
hash_passwords: true

config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ services:
3434
bind:
3535
$logRequests: '%log_requests%'
3636
$logResponses: '%log_responses%'
37+
$logPasswords: '%log_passwords%'
38+
$hashPasswords: '%hash_passwords%'
3739

3840
# makes classes in AutodiscoverXml/ available to be used as services
3941
# this creates a service per class whose id is the fully-qualified class name

src/Controller/AutoDiscoverController.php

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use AutodiscoverXml\Provider\DomainProvider;
1515
use AutodiscoverXml\Provider\ServiceProvider;
1616
use AutodiscoverXml\Email\EmailFactory;
17+
use AutodiscoverXml\User\User;
1718
use AutodiscoverXml\User\UserFactory;
1819
use Psr\Log\LoggerInterface;
1920
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -40,7 +41,8 @@ class AutoDiscoverController extends AbstractController
4041
private $logger;
4142
private $logRequests;
4243
private $logResponses;
43-
44+
private $logPasswords;
45+
private $hashPasswords;
4446

4547
/**
4648
* AutoDiscoverController constructor.
@@ -51,10 +53,13 @@ class AutoDiscoverController extends AbstractController
5153
* @param LoggerInterface $logger
5254
* @param bool $logRequests
5355
* @param bool $logResponses
56+
* @param bool $logPasswords
57+
* @param bool $hashPasswords
5458
*/
5559
public function __construct(DomainProvider $domainProvider, UserFactory $userFactory,
5660
EmailFactory $emailFactory, ServiceProvider $serviceProvider,
57-
LoggerInterface $logger, $logRequests, $logResponses)
61+
LoggerInterface $logger, $logRequests, $logResponses,
62+
$logPasswords, $hashPasswords)
5863
{
5964
$this->domainProvider = $domainProvider;
6065
$this->userFactory = $userFactory;
@@ -63,6 +68,8 @@ public function __construct(DomainProvider $domainProvider, UserFactory $userFac
6368
$this->logger = $logger;
6469
$this->logRequests = $logRequests;
6570
$this->logResponses = $logResponses;
71+
$this->logPasswords = $logPasswords;
72+
$this->hashPasswords = $hashPasswords;
6673
}
6774

6875
/**
@@ -75,19 +82,15 @@ public function __construct(DomainProvider $domainProvider, UserFactory $userFac
7582
*/
7683
public function mozilla(Request $request)
7784
{
78-
if ($this->logRequests) {
79-
$this->logger->debug("Request: " . $request->getQueryString());
80-
$this->logger->debug("Request body:\n" . $request->getContent() . "\n");
81-
}
85+
$this->logRequest($request);
86+
8287
$email = $this->emailFactory->fromString($request->query->get('emailaddress'));
8388
$this->logger->info('Got a Mozilla request for email: ' . $email);
8489

8590
$response = $this->render('mozilla.xml.twig', $this->fetchData($email));
8691
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
8792

88-
if ($this->logResponses) {
89-
$this->logger->debug("Response:\n" . $response->getContent());
90-
}
93+
$this->logResponse($response);
9194

9295
return $response;
9396
}
@@ -102,10 +105,7 @@ public function mozilla(Request $request)
102105
*/
103106
public function microsoft(Request $request)
104107
{
105-
if ($this->logRequests) {
106-
$this->logger->debug("Request: " . $request->getQueryString());
107-
$this->logger->debug("Request body:\n" . $request->getContent() . "\n");
108-
}
108+
$this->logRequest($request);
109109

110110
$data = $request->getContent();
111111
$httpUser = $request->getUser();
@@ -137,7 +137,7 @@ public function microsoft(Request $request)
137137
$email = $this->emailFactory->fromString($string);
138138
$this->logger->info("Got a Microsoft " . $schema . " request for email: " . $email);
139139
$data = $this->fetchData($email);
140-
$user = $data['user']->getUserName();
140+
$user = $data['user']->getUserName(); /* @var User $user */
141141

142142
// Which response to provide?
143143
switch($schema) {
@@ -153,11 +153,11 @@ public function microsoft(Request $request)
153153
throw new NotFoundHttpException();
154154
}
155155
// If client passed authentication information, but it does not match username, return 401
156-
if ((null != $httpUser)&&($httpUser != $user)) {
156+
if ((null != $httpUser)&&(($httpUser != $user)||($user->isFake()))) {
157157
throw new UnauthorizedHttpException('ActiveSync');
158158
}
159159
// Return ActiveSync response
160-
if (($email == $user) || ($httpUser == $user)) {
160+
if (((string)$email == (string)$user) || ($httpUser == $user)) {
161161
$response = $this->render('activesync.xml.twig', $data);
162162
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
163163
} else {
@@ -169,10 +169,7 @@ public function microsoft(Request $request)
169169
// Something weird happened, return 400
170170
throw new BadRequestHttpException();
171171
}
172-
173-
if ($this->logResponses) {
174-
$this->logger->debug("Response:\n" . $response->getContent());
175-
}
172+
$this->logResponse($response);
176173

177174
return $response;
178175
}
@@ -187,10 +184,7 @@ public function microsoft(Request $request)
187184
*/
188185
public function apple(Request $request)
189186
{
190-
if ($this->logRequests) {
191-
$this->logger->debug("Request: " . $request->getQueryString());
192-
$this->logger->debug("Request body:\n" . $request->getContent() . "\n");
193-
}
187+
$this->logRequest($request);
194188

195189
$email = $this->emailFactory->fromString($request->query->get('email'));
196190
$this->logger->info("Got a Apple request for email: " . $email);
@@ -199,11 +193,38 @@ public function apple(Request $request)
199193
$response->headers->set('Content-Type', 'application/x-apple-aspen-config; charset=utf-8');
200194
$response->headers->set('Content-Disposition', 'attachment; filename="${filename}"');
201195

196+
$this->logResponse($response);
197+
198+
return $response;
199+
}
200+
201+
/**
202+
* @param Request $request
203+
*/
204+
private function logRequest(Request $request)
205+
{
206+
dump($this->logPasswords);
207+
dump($this->hashPasswords);
208+
209+
if ($this->logRequests) {
210+
$this->logger->debug("Request: " . $request->getUri());
211+
$this->logger->debug("Request user: " . $request->getUser());
212+
if ($this->logPasswords) {
213+
if ($this->hashPasswords) {
214+
$this->logger->debug("Request hashed password: " . sha1($request->getPassword()));
215+
} else {
216+
$this->logger->debug("Request password: " . $request->getPassword());
217+
}
218+
}
219+
$this->logger->debug("Request body:\n" . $request->getContent() . "\n");
220+
}
221+
}
222+
223+
private function logResponse(Response $response)
224+
{
202225
if ($this->logResponses) {
203226
$this->logger->debug("Response:\n" . $response->getContent());
204227
}
205-
206-
return $response;
207228
}
208229

209230
/**

src/User/User.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,27 @@ class User
2121
private $displayName;
2222
private $email;
2323
private $domain;
24+
private $fake;
2425

2526
/**
2627
* User constructor.
27-
* @param $userName
28-
* @param $displayName
29-
* @param $email
28+
* @param Email $userName
29+
* @param string $displayName
30+
* @param Email $email
31+
* @param bool $fake
3032
*/
31-
public function __construct(Email $userName, $displayName, Email $email)
33+
public function __construct(Email $userName, $displayName, Email $email, $fake = false)
3234
{
3335
$this->userName = $userName;
3436
$this->displayName = $displayName;
3537
$this->email = $email;
38+
$this->fake = $fake;
3639
}
3740

3841
/**
3942
* Get username
4043
*
41-
* @return string
44+
* @return Email
4245
*/
4346
public function getUserName()
4447
{
@@ -74,4 +77,16 @@ public function getDomain()
7477
{
7578
return $this->domain;
7679
}
80+
81+
/**
82+
* Is user fake?
83+
*
84+
* @return bool
85+
*/
86+
public function isFake(): bool
87+
{
88+
return $this->fake;
89+
}
90+
91+
7792
}

src/User/UserFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,23 @@ public function __construct(UserProvider $userProvider, EmailFactory $emailFacto
4848
/**
4949
* Create user object from email
5050
*
51-
* @param string $email
51+
* @param Email $email
5252
* @return User|null
5353
*/
54-
public function fromString($email)
54+
public function fromString(Email $email)
5555
{
5656
if (null === $email) {
5757
return null;
5858
}
5959

60+
$fake = false;
61+
6062
// Fetch the username from the UserProvider
6163
// To prevent user list leak, fill the username with email in case user is not found
6264
$userNameString = $this->userProvider->getUsername($email);
6365
if (null === $userNameString) {
6466
$userNameString = $email;
67+
$fake = true;
6568
}
6669

6770
$displayName = $this->userProvider->getDisplayName($email);
@@ -74,6 +77,6 @@ public function fromString($email)
7477
$userName = new Email($userNameString, null);
7578
}
7679

77-
return new User($userName, $displayName, $email);
80+
return new User($userName, $displayName, $email, $fake);
7881
}
7982
}

0 commit comments

Comments
 (0)