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

Add ability to use LDAP as password reset provider #106

Merged
merged 1 commit into from
Feb 15, 2022
Merged
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
1 change: 1 addition & 0 deletions LDAP-Auth/Api/LdapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public IActionResult TestServerBind([FromBody] ServerConnectionInfo body)
configuration.LdapBindUser = body.LdapBindUser;
configuration.LdapBindPassword = body.LdapBindPassword;
configuration.LdapBaseDn = body.LdapBaseDn;
configuration.PasswordResetUrl = body.PasswordResetUrl;
LdapPlugin.Instance.UpdateConfiguration(configuration);

return Ok(_ldapAuthenticationProvider.TestServerBind());
Expand Down
5 changes: 5 additions & 0 deletions LDAP-Auth/Api/Models/ServerConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ public class ServerConnectionInfo
/// </summary>
[Required]
public string LdapBaseDn { get; set; }

/// <summary>
/// Gets or sets the password reset url.
/// </summary>
public string PasswordResetUrl { get; set; }
}
}
5 changes: 5 additions & 0 deletions LDAP-Auth/Config/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,10 @@ public PluginConfiguration()
/// Gets or sets a list of folder Ids which are enabled for access by default.
/// </summary>
public string[] EnabledFolders { get; set; }

/// <summary>
/// Gets or sets the password reset url.
/// </summary>
public string PasswordResetUrl { get; set; }
}
}
15 changes: 14 additions & 1 deletion LDAP-Auth/Config/configPage.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<title>LDAP</title>
</head>
Expand Down Expand Up @@ -45,6 +45,15 @@ <h2 class="sectionTitle">LDAP Settings:</h2>
</label>
<div class="fieldDescription checkboxFieldDescription">Skip verification of connection certificate when using SSL/STARTTLS.</div>
</div>
<div class="inputContainer fldExternalAddressFilter">
<input is="emby-input" type="text" id="txtLdapPasswordResetUrl" label="Password Reset Url:" />
<div class="fieldDescription">
The url for password reset.
</br>Placeholders:
</br>- $userId
</br>- $userName
</div>
</div>
<div class="inputContainer fldExternalAddressFilter">
<input is="emby-input" type="text" id="txtLdapBindUser" label="LDAP Bind User:" />
<div class="fieldDescription">A bind user for LDAP search queries, required if your LDAP doesn't support anonymous bind.</div>
Expand Down Expand Up @@ -168,6 +177,7 @@ <h2>${HeaderLibraryAccess}</h2>
txtLdapUsernameAttribute: document.querySelector("#txtLdapUsernameAttribute"),
chkEnableAllFolders: document.querySelector('#chkEnableAllFolders'),
folderAccessList: document.querySelector('.folderAccess'),
txtPasswordResetUrl: document.querySelector("#txtLdapPasswordResetUrl")
};

document.querySelector('.esqConfigurationPage').addEventListener("pageshow", function () {
Expand Down Expand Up @@ -196,6 +206,7 @@ <h2>${HeaderLibraryAccess}</h2>
LdapConfigurationPage.chkEnableAllFolders.checked = config.EnableAllFolders;
/* Default to empty array if Enabled Folders is not set */
config.EnabledFolders = config.EnabledFolders || [];
LdapConfigurationPage.txtPasswordResetUrl.value = config.PasswordResetUrl || '';
loadMediaFolders(config).then(() => {
Dashboard.hideLoadingMsg();
});
Expand Down Expand Up @@ -277,6 +288,7 @@ <h2>${HeaderLibraryAccess}</h2>
folders = Array.prototype.filter.call(folders, folder => folder.checked)
.map(folder => folder.getAttribute("data-id"));
config.EnabledFolders = folders;
config.PasswordResetUrl = LdapConfigurationPage.txtPasswordResetUrl;
window.ApiClient.updatePluginConfiguration(LdapConfigurationPage.pluginUniqueId, config).then(Dashboard.processPluginConfigurationUpdateResult);
});

Expand All @@ -301,6 +313,7 @@ <h2>${HeaderLibraryAccess}</h2>
LdapBindUser: LdapConfigurationPage.txtLdapBindUser.value,
LdapBindPassword: LdapConfigurationPage.txtLdapBindPassword.value,
LdapBaseDn: LdapConfigurationPage.txtLdapBaseDn.value,
PasswordResetUrl: LdapConfigurationPage.txtPasswordResetUrl.value,
}

const handler = response => response.json().then(res => {
Expand Down
35 changes: 33 additions & 2 deletions LDAP-Auth/LDAPAuthenticationProviderPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using MediaBrowser.Common;
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Users;
using Microsoft.Extensions.Logging;
using Novell.Directory.Ldap;

Expand All @@ -16,7 +17,7 @@ namespace Jellyfin.Plugin.LDAP_Auth
/// <summary>
/// Ldap Authentication Provider Plugin.
/// </summary>
public class LdapAuthenticationProviderPlugin : IAuthenticationProvider
public class LdapAuthenticationProviderPlugin : IAuthenticationProvider, IPasswordResetProvider
{
private readonly ILogger<LdapAuthenticationProviderPlugin> _logger;
private readonly IApplicationHost _applicationHost;
Expand Down Expand Up @@ -128,7 +129,9 @@ public async Task<ProviderAuthenticationResult> Authenticate(string username, st
if (LdapPlugin.Instance.Configuration.CreateUsersFromLdap)
{
user = await userManager.CreateUserAsync(ldapUsername).ConfigureAwait(false);
user.AuthenticationProviderId = GetType().FullName;
var providerName = GetType().FullName!;
user.AuthenticationProviderId = providerName;
user.PasswordResetProviderId = providerName;
user.SetPermission(PermissionKind.IsAdministrator, ldapIsAdmin);
user.SetPermission(PermissionKind.EnableAllFolders, LdapPlugin.Instance.Configuration.EnableAllFolders);
if (!LdapPlugin.Instance.Configuration.EnableAllFolders)
Expand Down Expand Up @@ -285,6 +288,34 @@ public LdapEntry LocateLdapUser(string username)
return ldapUser;
}

/// <inheritdoc />
public Task<ForgotPasswordResult> StartForgotPasswordProcess(User user, bool isInNetwork)
{
var resetUrl = LdapPlugin.Instance.Configuration.PasswordResetUrl;
if (string.IsNullOrEmpty(resetUrl))
{
throw new NotImplementedException();
}

resetUrl = resetUrl
.Replace("$userId", user.Id.ToString(), StringComparison.OrdinalIgnoreCase)
.Replace("$userName", user.Username, StringComparison.OrdinalIgnoreCase);

var result = new ForgotPasswordResult
{
Action = ForgotPasswordAction.PinCode,
PinFile = resetUrl
};

return Task.FromResult(result);
}

/// <inheritdoc />
public Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
{
throw new NotImplementedException();
}

private LdapAttribute GetAttribute(LdapEntry userEntry, string attr)
{
var attributeSet = userEntry.GetAttributeSet();
Expand Down