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

OIDC Plugin : Using CUID as Username Instead of Email. #122

Open
wants to merge 3 commits into
base: 5.x-dev
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
27 changes: 17 additions & 10 deletions Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,38 +362,45 @@ private function isPluginSetup($settings) : bool
*
* @param SystemSettings $settings
* @param string $providerUserId Remote user id
* @param string $matomoUserLogin Users email address, will be used as username as well
* @param string $providerEmail Users email address
* @return void
*/
private function signupUser($settings, string $providerUserId, string $matomoUserLogin = null)
private function signupUser($settings, string $providerUserId, string $providerEmail = null)
{
// only sign up user if setting is enabled
if ($settings->allowSignup->getValue()) {
// verify response contains email address
if (empty($matomoUserLogin)) {
if (empty($providerEmail)) {
throw new Exception(Piwik::translate("LoginOIDC_ExceptionUserNotFoundAndNoEmail"));
}

if (empty($providerUserId)) {
throw new Exception(Piwik::translate("LoginOIDC_ExceptionUserNotFoundAndNoUserId"));
}
if ($settings->useEmailAsUsername->getValue()) {
$userId = $providerEmail;
} else {
$userId = $providerUserId;
}
// verify email address domain is allowed to sign up
if (!empty($settings->allowedSignupDomains->getValue())) {
$signupDomain = substr($matomoUserLogin, strpos($matomoUserLogin, "@") + 1);
$signupDomain = substr($providerEmail, strpos($matomoUserLogin, "@") + 1);
$allowedDomains = explode("\n", $settings->allowedSignupDomains->getValue());
if (!in_array($signupDomain, $allowedDomains)) {
throw new Exception(Piwik::translate("LoginOIDC_ExceptionAllowedSignupDomainsDenied"));
}
}

// set an invalid pre-hashed password, to block the user from logging in by password
Access::getInstance()->doAsSuperUser(function () use ($matomoUserLogin, $result) {
UsersManagerApi::getInstance()->addUser($matomoUserLogin,
Access::getInstance()->doAsSuperUser(function () use ($userId, $providerEmail, $result) {
UsersManagerApi::getInstance()->addUser($userId,
"(disallow password login)",
$matomoUserLogin,
$providerEmail,
/* $_isPasswordHashed = */ true,
/* $initialIdSite = */ null);
});
$userModel = new Model();
$user = $userModel->getUser($matomoUserLogin);
$this->linkAccount($providerUserId, $matomoUserLogin);
$user = $userModel->getUser($userId);
$this->linkAccount($providerUserId, $userId);
$this->signinAndRedirect($user, $settings);
} else {
throw new Exception(Piwik::translate("LoginOIDC_ExceptionUserNotFoundAndSignupDisabled"));
Expand Down
22 changes: 22 additions & 0 deletions SystemSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
*/
public $userinfoId;

/**
* Use the e-mail address as username.
*
* @var bool
*/
public $useEmailAsUsername;

/**
* The client id given by the provider.
*
Expand Down Expand Up @@ -158,6 +165,7 @@ protected function init()
$this->userinfoUrl = $this->createUserinfoUrlSetting();
$this->endSessionUrl = $this->createEndSessionUrlSetting();
$this->userinfoId = $this->createUserinfoIdSetting();
$this->useEmailAsUsername = $this->createUseEmailAsUsernameSetting();
$this->clientId = $this->createClientIdSetting();
$this->clientSecret = $this->createClientSecretSetting();
$this->scope = $this->createScopeSetting();
Expand Down Expand Up @@ -337,6 +345,20 @@ private function createUserinfoIdSetting() : SystemSetting
});
}

/**
* Add useEmailAsUsername setting.
*
* @return SystemSetting
*/
private function createUseEmailAsUsernameSetting() : SystemSetting
{
return $this->makeSetting("useEmailAsUsername", $default = true, FieldConfig::TYPE_BOOL, function(FieldConfig $field) {
$field->title = Piwik::translate("LoginOIDC_SettingUseEmailAsUsername");
$field->description = Piwik::translate("LoginOIDC_SettingUseEmailAsUsernameHelp");
$field->uiControl = FieldConfig::UI_CONTROL_CHECKBOX;
});
}

/**
* Add client id setting.
*
Expand Down
3 changes: 3 additions & 0 deletions lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"SettingEndSessionUrlHelp": "Nach dem Logout wird der Benutzer zu dieser URL weitergeleitet, damit die Session beim Provider beendet wird. Bei Unklarheit sollte dieses Feld freigelassen werden.",
"SettingUserinfoId": "Userinfo ID",
"SettingUserinfoIdHelp": "Name des Feldes, in dem die Benutzer-ID enthalten ist. Normalerweise, für OpenID Connect Dienste wie Auth0, ist das 'sub'. Github gibt die eindeutige Benutzer-ID in dem Feld 'id' an.",
"SettingUseEmailAsUsername": "E-Mail Adresse als Benutzer-ID verwenden",
"SettingUseEmailAsUsernameHelp": "Verwendet die E-Mail-Adresse des OpenID Connect Providers als Matomo Benutzer-ID.",
"SettingClientId": "Client ID",
"SettingClientIdHelp": "",
"SettingClientSecret": "Client Secret",
Expand All @@ -45,6 +47,7 @@
"ExceptionUserNotFoundAndSignupDisabled": "Benutzer nicht gefunden. Neue Registrierungen über OAuth werden nicht unterstützt.",
"ExceptionUserNotFoundAndNoEmail": "Benutzer nicht gefunden. Benutzer konnte nicht erstellt werden, weil der OAuth Service keine E-Mail Adresse zurückgab.",
"ExceptionSuperUserOauthDisabled": "OAuth Login für Superuser ist deaktiviert.",
"ExceptionUserNotFoundAndNoUserId": "Benutzer nicht gefunden. Benutzer konnte nicht erstellt werden, weil der OAuth Service keine Benutzer-ID zurückgab.",
"ExceptionAllowedSignupDomainsValidationFailed": "Die Liste der zugelassenen Domains hat nicht das richtige Format.",
"ExceptionAllowedSignupDomainsDenied": "Die verwendete Domain ist nicht für Registrierungen freigeschaltet.",
"ExceptionAlreadyLinkedToDifferentAccount": "Der Benutzer beim OAuth-Service ist bereits mit einem anderem Matomo-Nutzer verlinkt."
Expand Down
3 changes: 3 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"SettingEndSessionUrlHelp": "After logging out, the user is redirected to this URL to end the session at the provider. If you are unsure, just leave this field empty.",
"SettingUserinfoId": "Userinfo ID",
"SettingUserinfoIdHelp": "Name of the unique user id field in the userinfo response. Usually for OpenID Connect services like Auth0 this is 'sub'. Github provides the user id in 'id'.",
"SettingUseEmailAsUsername": "Use e-mail address as username",
"SettingUseEmailAsUsernameHelp": "Uses the e-mail address from the OpenID Connect provider as Matomo username.",
"SettingClientId": "Client ID",
"SettingClientIdHelp": "",
"SettingClientSecret": "Client Secret",
Expand All @@ -47,6 +49,7 @@
"ExceptionUserNotFoundAndSignupDisabled": "User not found. OAuth registrations are disabled.",
"ExceptionUserNotFoundAndNoEmail": "User not found. User could not be created because the OAuth service did not return an email address.",
"ExceptionSuperUserOauthDisabled": "OAuth login disabled for superusers.",
"ExceptionUserNotFoundAndNoUserId": "User not found. User could not be created because the OAuth service did not return a user ID.",
"ExceptionAllowedSignupDomainsValidationFailed": "Validation failed for the list of domains allowed for user creation.",
"ExceptionAllowedSignupDomainsDenied": "The domain is currently not activated for account creation.",
"ExceptionAlreadyLinkedToDifferentAccount": "The remote OAuth user is already linked to another account."
Expand Down
3 changes: 3 additions & 0 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"SettingEndSessionUrlHelp": "",
"SettingUserinfoId": "ID Userinfo",
"SettingUserinfoIdHelp": "Nom du champ de l'identifiant unique utilisateur dans la réponse 'userinfo'. Habituellement, pour les services de connexion OpenID Connect comme Auth0, il s'agit de 'sub'. Github fourni l'identifiant utilisateur avec 'id'.",
"SettingUseEmailAsUsername": "Utiliser l'adresse e-mail comme nom d'utilisateur",
"SettingUseEmailAsUsernameHelp": "Utilise l'adresse e-mail du fournisseur OpenID Connect comme nom d'utilisateur Matomo.",
"SettingClientId": "Client ID",
"SettingClientIdHelp": "",
"SettingClientSecret": "Client Secret",
Expand All @@ -42,6 +44,7 @@
"ExceptionInvalidResponse": "Réponse inattendue du service OAuth.",
"ExceptionUserNotFoundAndSignupDisabled": "Utilisateur non trouvé. Les nouvelles inscriptions via OAuth sont désactivées.",
"ExceptionUserNotFoundAndNoEmail": "Utilisateur non trouvé. L'utilisateur n'a pas pu être créé car le service OAuth n'a pas renvoyé d'adresse e-mail.",
"ExceptionUserNotFoundAndNoUserId": "Utilisateur non trouvé. L'utilisateur n'a pas pu être créé car le service OAuth n'a pas renvoyé d'ID utilisateur.",
"ExceptionSuperUserOauthDisabled": "La connexion OAuth pour les Supers Utilisateurs est désactivée.",
"ExceptionAllowedSignupDomainsValidationFailed": "La validation a échoué pour la liste des domaines autorisés pour la création d'utilisateurs.",
"ExceptionAllowedSignupDomainsDenied": "Le domaine n'est pas activé pour la création d'un compte.",
Expand Down