Skip to content

Commit fe49fd8

Browse files
committed
option to enforce oauth for some users
1 parent 90b4dfc commit fe49fd8

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
lines changed

OAuthManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected function validateUserData($userdata, $servicename)
195195
/** @var \helper_plugin_oauth $hlp */
196196
$hlp = plugin_load('helper', 'oauth');
197197

198-
if (!$hlp->checkMail($userdata['mail'])) {
198+
if (!$hlp->checkMail($userdata['mail'], $hlp->getValidDomains())) {
199199
throw new Exception('rejectedEMail', [implode(', ', $hlp->getValidDomains())]);
200200
}
201201

_test/CheckMailTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function testCheckMail($restriction, $input, $expected)
4646

4747
/** @var \helper_plugin_oauth $hlp */
4848
$hlp = plugin_load('helper', 'oauth');
49-
$this->assertSame($expected, $hlp->checkMail($input));
49+
$this->assertSame($expected, $hlp->checkMail($input, $hlp->getValidDomains()));
5050
}
5151
}

auth.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ public function trustExternal($user, $pass, $sticky = false)
5454
}
5555
}
5656

57+
/**
58+
* Enforce oauth login for certain email domains
59+
*
60+
* @inheritdoc
61+
*/
62+
public function checkPass($user, $pass)
63+
{
64+
$ok = parent::checkPass($user, $pass);
65+
if(!$ok) return $ok;
66+
$domains = $this->hlp->getEnforcedDomains();
67+
if($domains === []) return $ok;
68+
69+
if($this->hlp->checkMail($this->getUserData($user)['mail'], $domains)) {
70+
global $lang;
71+
// we overwrite the standard bad password message with our own
72+
$lang['badlogin'] = $this->getLang('eMailEnforced');
73+
return false;
74+
}
75+
return $ok;
76+
}
77+
78+
5779
/**
5880
* Enhance function to check against duplicate emails
5981
*

conf/default.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
$conf['info'] = '';
1010
$conf['custom-redirectURI'] = '';
1111
$conf['mailRestriction'] = '';
12+
$conf['mailEnforcement'] = '';
1213
$conf['singleService'] = '';
1314
$conf['register-on-auth'] = 0;
1415
$conf['overwrite-groups'] = 0;

conf/metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
$meta['custom-redirectURI'] = array('string','_caution' => 'warning');
1111
// https://regex101.com/r/mG4aL5/3
1212
$meta['mailRestriction'] = array('string','_pattern' => '!^(@[^,@]+(\.[^,@]+)+(,|$))*$!');
13+
$meta['mailEnforcement'] = array('string','_pattern' => '!^(@[^,@]+(\.[^,@]+)+(,|$))*$!');
1314
$meta['singleService'] = array('onoff');
1415
$meta['register-on-auth'] = array('onoff','_caution' => 'security');
1516
$meta['overwrite-groups'] = array('onoff','_caution' => 'danger');

helper.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,33 @@ public function getValidDomains()
8181
if ($this->getConf('mailRestriction') === '') {
8282
return [];
8383
}
84-
$validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
85-
return array_map('trim', $validDomains);
84+
$domains = explode(',', trim($this->getConf('mailRestriction'), ','));
85+
return array_map('trim', $domains);
86+
}
87+
88+
/**
89+
* @return array
90+
*/
91+
public function getEnforcedDomains()
92+
{
93+
if ($this->getConf('mailEnforcement') === '') {
94+
return [];
95+
}
96+
$domains = explode(',', trim($this->getConf('mailEnforcement'), ','));
97+
return array_map('trim', $domains);
8698
}
8799

88100
/**
89101
* @param string $mail
102+
* @param array $domains List of domains to check against (from getValidDomains or getEnforcedDomains)
90103
*
91104
* @return bool
92105
*/
93-
public function checkMail($mail)
106+
public function checkMail($mail, array $domains)
94107
{
95-
$validDomains = $this->getValidDomains();
96-
if (empty($validDomains)) return true;
108+
if (empty($domains)) return true;
97109

98-
foreach ($validDomains as $validDomain) {
110+
foreach ($domains as $validDomain) {
99111
if (str_ends_with($mail, $validDomain)) {
100112
return true;
101113
}

lang/en/lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$lang['loginButton'] = 'Log in with ';//... i.e. Google (on SingleAuth)
1313
$lang['rejectedEMail'] = 'Invalid eMail-Account used. Only email accounts from the following domain(s) are allowed: %s!';
1414
$lang['eMailRestricted'] = 'Only email accounts from the following domain(s) are allowed: %s';
15+
$lang['eMailEnforced'] = 'Sorry, accounts from your domain have to login via oAuth.';
1516
$lang['noEmail'] = '%s service did not provide the an email address. Can\'t log you in.';
1617
$lang['addUser not possible'] = 'Self-Registration is currently disabled or conf/users.auth.php is not writable. Please ask your DokuWiki administrator to create your account manually.';
1718
$lang['oauth login failed'] = 'Your (re)login has failed.';

lang/en/settings.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
*/
77

88

9-
$lang['info'] = 'Redirect URI to use when configuring the applications';
9+
$lang['info'] = 'Redirect URI to use when configuring the applications';
1010
$lang['custom-redirectURI'] = 'Use the following custom redirect URI';
11-
$lang['mailRestriction'] = "Limit authentification to users from this domain (optional, must start with an <code>@</code>)";
12-
$lang['singleService'] = 'Login with single oAuth service only (disables local logins!)';
11+
$lang['mailRestriction'] = "Limit oAuth authentification to users from this domain (optional, must start with an <code>@</code>). Multiple domains can be separated by commas.";
12+
$lang['mailEnforcement'] = "Enforce oAuth authentification for users from this domain (optional, must start with an <code>@</code>). Multiple domains can be separated by commas.";
13+
$lang['singleService'] = 'Login with single oAuth service only (disables local logins!)';
1314
$lang['singleService_o_'] = 'Allow all services';
1415
$lang['register-on-auth'] = 'Register authenticated users even if self-registration is disabled in main configuration';
1516
$lang['overwrite-groups'] = 'Overwrite all DokuWiki user groups by those supplied by provider';

0 commit comments

Comments
 (0)