Skip to content

Commit bc761ba

Browse files
committed
Add methods to check group memberships
1 parent d4eb665 commit bc761ba

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

lib/Authentication/AuthTokens/OIDCAuthToken.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ abstract class OIDCAuthToken implements IAuthentication
1111
private $principal;
1212
protected $acceptedIssuers;
1313
protected $authRealm;
14+
protected $groupHeader;
15+
protected $groupSplitChar;
16+
protected $bannedGroups;
17+
protected $requiredGroups;
18+
protected $helpString;
1419

1520
/**
1621
* {@see IAuthentication::eraseCredentials()}
@@ -101,7 +106,7 @@ public static function isStateless()
101106
}
102107

103108
/**
104-
* Set principal/User details from the session.
109+
* Set principal/User details from the session and check group membership.
105110
*/
106111
protected function setTokenFromSession()
107112
{
@@ -110,6 +115,82 @@ protected function setTokenFromSession()
110115
$this->userDetails = array(
111116
'AuthenticationRealm' => array($this->authRealm)
112117
);
118+
119+
// Check group membership is acceptable.
120+
$this->checkBannedGroups();
121+
$this->checkRequiredGroups();
122+
}
123+
}
124+
125+
/**
126+
* Check the token lists all the required groups.
127+
*/
128+
protected function checkRequiredGroups()
129+
{
130+
$groupArray = explode(
131+
$this->groupSplitChar,
132+
$_SERVER[$this->groupHeader]
133+
);
134+
135+
// Build up a list of missing groups.
136+
$missingGoodGroups = [];
137+
foreach ($this->requiredGroups as $group) {
138+
if (!in_array($group, $groupArray)) {
139+
$missingGoodGroups[] = $group;
140+
}
141+
}
142+
143+
// If the list of missing groups is not empty, reject the user.
144+
if (!empty($missingGoodGroups)) {
145+
$this->rejectUser(
146+
'You are missing the following group(s):',
147+
$missingGoodGroups
148+
);
149+
}
150+
}
151+
152+
/**
153+
* Check the token lists non of the banned groups.
154+
*/
155+
protected function checkBannedGroups()
156+
{
157+
$groupArray = explode($this->groupSplitChar, $_SERVER[$this->groupHeader]);
158+
159+
$presentBadGroups = [];
160+
foreach ($this->bannedGroups as $group) {
161+
if (in_array($group, $groupArray)) {
162+
$presentBadGroups[] = $group;
163+
}
164+
}
165+
166+
// If the list of present bad groups is not empty, reject the user.
167+
if (!empty($presentBadGroups)) {
168+
$this->rejectUser(
169+
'We do not grant access to GOCDB to members of the following group(s):',
170+
$presentBadGroups
171+
);
113172
}
114173
}
174+
175+
/**
176+
* Craft a BadCredentialsException exception.
177+
*
178+
* Uses the given error message to provide the end user more context.
179+
*
180+
* @param string $errorContext Context for the error.
181+
* @param string[] $groupArray An array of group memberships
182+
*/
183+
protected function rejectUser($errorContext, $groupArray)
184+
{
185+
// For readability, when listing groups to the user,
186+
// start each one on a new line with a '-' character.
187+
$prependString = '<br />- ';
188+
$groupString = implode($prependString, $groupArray);
189+
throw new BadCredentialsException(
190+
null,
191+
'You do not belong to the correct group(s) ' .
192+
'to gain access to this site.<br /><br />' . $errorContext .
193+
$prependString . $groupString . '<br /><br />' . $this->helpString
194+
);
195+
}
115196
}

0 commit comments

Comments
 (0)