Skip to content

Commit a3275cf

Browse files
author
tchapi
committed
More fixes
1 parent 2c35626 commit a3275cf

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ Or use it directly in the Apache configuration
239239
240240
### Specific environment variables for IMAP and LDAP authentication methods
241241
242-
In case you use the `IMAP` auth type, you must specify the auth url (_the "mailbox" url_) in `IMAP_AUTH_URL`.
242+
In case you use the `IMAP` auth type, you must specify the auth url (_the "mailbox" url_) in `IMAP_AUTH_URL` as `host:port`, the encryption method (SSL, TLS or None) and whether the certificate should be validated.
243243
244-
You should also explicitely define whether you want new authenticated to be created upon login:
244+
You should also explicitely define whether you want new authenticated users to be created upon login:
245245
246246
```shell
247-
IMAP_AUTH_URL="{imap.gmail.com:993}"
247+
IMAP_AUTH_URL=imap.mydomain.com:993
248+
IMAP_ENCRYPTION_METHOD=ssl # ssl, tls or false
249+
IMAP_CERTIFICATE_VALIDATION=true
248250
IMAP_AUTH_USER_AUTOCREATE=true # false by default
249251
```
250252
251-
Same goes for LDAP, where you must specify the LDAP server url, the DN pattern, the Mail attribute, as well as whether you want new authenticated to be created upon login (_like for IMAP_):
253+
Same goes for LDAP, where you must specify the LDAP server url, the DN pattern, the Mail attribute, as well as whether you want new authenticated users to be created upon login (_like for IMAP_):
252254
253255
```shell
254256
LDAP_AUTH_URL=ldap://127.0.0.1:3890 # default LDAP port

config/services.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ services:
3333
App\Services\IMAPAuth:
3434
arguments:
3535
$IMAPAuthUrl: "%env(IMAP_AUTH_URL)%"
36-
$autoCreate: "%env(bool:IMAP_AUTH_USER_AUTOCREATE)%"
3736
$IMAPEncryptionMethod: "%env(IMAP_ENCRYPTION_METHOD)%"
3837
$IMAPCertificateValidation: "%env(bool:IMAP_CERTIFICATE_VALIDATION)%"
38+
$autoCreate: "%env(bool:IMAP_AUTH_USER_AUTOCREATE)%"
3939

4040
App\Services\LDAPAuth:
4141
arguments:
4242
$LDAPAuthUrl: "%env(LDAP_AUTH_URL)%"
4343
$LDAPDnPattern: "%env(LDAP_DN_PATTERN)%"
4444
$LDAPMailAttribute: "%env(LDAP_MAIL_ATTRIBUTE)%"
45-
$autoCreate: "%env(bool:LDAP_AUTH_USER_AUTOCREATE)%"
4645
$LDAPCertificateCheckingStrategy: "%env(LDAP_CERTIFICATE_CHECKING_STRATEGY)%"
46+
$autoCreate: "%env(bool:LDAP_AUTH_USER_AUTOCREATE)%"
4747

4848
# controllers are imported separately to make sure services can be injected
4949
# as action arguments even if you don't extend any base controller class

docker/.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ AUTH_METHOD=Basic # Basic or IMAP or LDAP
3232
AUTH_REALM=SabreDAV
3333

3434
# IMAP auth settings
35-
IMAP_AUTH_URL={imap.gmail.com:993/imap/ssl/novalidate-cert}
35+
IMAP_AUTH_URL=imap.mydomain.com:993
36+
IMAP_ENCRYPTION_METHOD=ssl
37+
IMAP_CERTIFICATE_VALIDATION=true
3638
IMAP_AUTH_USER_AUTOCREATE=false
3739

3840
# LDAP auth settings

src/Services/IMAPAuth.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,26 @@ final class IMAPAuth extends AbstractBasic
5555

5656
public function __construct(ManagerRegistry $doctrine, Utils $utils, string $IMAPAuthUrl, bool $autoCreate, string $IMAPEncryptionMethod, bool $IMAPCertificateValidation)
5757
{
58-
$this->IMAPAuthUrl = $IMAPAuthUrl;
58+
$components = parse_url($IMAPAuthUrl);
5959

60-
// We're making sure that only ssl, tls or 'false' are passed down to the IMAP client
60+
if (!$components) {
61+
throw new Exception('IMAP Error (parsing IMAP url "'.$IMAPAuthUrl.'"): '.$e->getMessage());
62+
}
63+
64+
$this->IMAPHost = $components['host'] ?? null;
65+
66+
// Trying to choose the best port if it was not provided,
67+
// defaulting to 993 (secure)
68+
if (isset($components['port'])) {
69+
$this->IMAPPort = $components['port'];
70+
} elseif (false === $this->IMAPEncryptionMethod) {
71+
$this->IMAPPort = 143;
72+
} else {
73+
$this->IMAPPort = 993;
74+
}
75+
76+
// We're making sure that only ssl, tls or 'false' are passed down to the IMAP client,
77+
// defaulting to SSL
6178
$IMAPEncryptionMethodCleaned = strtolower($IMAPEncryptionMethod);
6279
if ('false' === $IMAPEncryptionMethodCleaned) {
6380
$this->IMAPEncryptionMethod = false;
@@ -76,33 +93,16 @@ public function __construct(ManagerRegistry $doctrine, Utils $utils, string $IMA
7693

7794
/**
7895
* Connects to an IMAP server and tries to authenticate.
79-
* If the user does not exist, create it.
96+
* If the user does not exist, create it (depending on the autoCreate flag).
8097
*/
8198
protected function imapOpen(string $username, string $password): bool
8299
{
83100
$cm = new ClientManager($options = []);
84101

85-
$components = parse_url($this->IMAPAuthUrl);
86-
87-
if (!$components) {
88-
error_log('IMAP Error (parsing IMAP url "'.$this->IMAPAuthUrl.'" ): '.$e->getMessage());
89-
90-
return false;
91-
}
92-
93-
// Trying to choose the best port if it was not provided
94-
if ($components['port']) {
95-
$port = $components['port'];
96-
} elseif (false === $this->IMAPEncryptionMethod) {
97-
$port = 143;
98-
} else {
99-
$port = 993;
100-
}
101-
102102
// Create a new instance of the IMAP client manually
103103
$client = $cm->make([
104-
'host' => $components['host'],
105-
'port' => $port,
104+
'host' => $this->IMAPHost,
105+
'port' => $this->IMAPPort,
106106
'encryption' => $this->IMAPEncryptionMethod,
107107
'validate_cert' => $this->IMAPCertificateValidation,
108108
'username' => $username,

0 commit comments

Comments
 (0)