Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/group based filter #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
'password_flow' => isset($_POST['password_flow']),
'password_reset_url' => $_POST['password_reset_url'],
'registration_url' => $_POST['registration_url'],
'allowed_groups' => $_POST['allowed_groups'],
'groups_claim' => $_POST['groups_claim'],
];

conf_update_param('OIDC', $conf['OIDC']);
Expand Down
2 changes: 1 addition & 1 deletion auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@
}

redirect(get_gallery_home_url());
?>
?>
2 changes: 2 additions & 0 deletions maintain.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class OpenIdConnect_maintain extends PluginMaintain
'password_flow' => false,
'password_reset_url' => '',
'registration_url' => '',
'allowed_groups' => '',
'groups_claim' => '',
];

function install($plugin_version, &$errors=array())
Expand Down
35 changes: 34 additions & 1 deletion oidc.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ function oidc_retrieve(OpenIDConnectClient $oidc, $force_registration = false) {
// If the user is not found, try to register
if (empty($row['id'])) {
if ($config['register_new_users'] || $force_registration) {
if(!user_in_allowed_groups($oidc, $conf)) {
return null;
}
// Registration is allowed, overwrite $row
$errors = [];
$row['id'] = register_user($name, random_pass(), $email, $config['notify_admins_on_register'], $errors, $config['notify_user_on_register']);
Expand Down Expand Up @@ -171,6 +174,11 @@ function oidc_login(OpenIDConnectClient $oidc, $token, $remember_me)
}
$_SESSION[OIDC_SESSION] = json_encode($encoded);

// check if user is member of allowed groups, if any is set
if (!user_in_allowed_groups($oidc, $conf)) {
return false;
}

// Update user data from ID token data
$fields = array($conf['user_fields']['email'], $conf['user_fields']['username']);

Expand Down Expand Up @@ -201,4 +209,29 @@ function oidc_logout()
logout_user();
redirect_auth() or redirect('identification.php');
}
?>

function user_in_allowed_groups(OpenIDConnectClient $oidc, $conf) {
// check if user is member of allowed groups, if any is set
$oidc_config = $conf['OIDC'];
$groups_claim = $oidc_config['groups_claim'] ?: 'groups';
$user_groups = $oidc->requestUserInfo($groups_claim);

$allowed = true;
$allowed_groups = $oidc_config['allowed_groups'];

if (!empty($allowed_groups)) {
if (empty($user_groups)) {
return false;
}
$allowed = false;
$allowed_groups_array = preg_split("/\s+/", $allowed_groups);
foreach ($allowed_groups_array as $allowed_group) {
if (in_array($allowed_group, $user_groups)) {
$allowed = true;
break;
}
}
}
return $allowed;
}
?>
14 changes: 14 additions & 0 deletions template/config.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@
<br />
<i>{'This claim is used for local identification and must return a unique value for each user. Falls back to \'sub\'.'|translate}</i>
</li>
<li>
<label for="allowed_groups">{'Allowed groups'|translate}</label>
<br />
<input type="text" size=50 name="allowed_groups" id="allowed_groups" value="{$allowed_groups}">
<br />
<i>{'Space separated list of groups from which users can log into piwigo.'|translate}</i>
</li>
<li>
<label for="groups_claim">{'Groups claim'|translate}</label>
<br />
<input type="text" size=50 name="groups_claim" id="groups_claim" value="{$groups_claim}">
<br />
<i>{'Name of the claim containing the groups list. Falls back to \'groups\'.'|translate}</i>
</li>
<li>
<label for="proxy">{'HTTP Proxy'|translate}</label>
<br />
Expand Down