Skip to content

Commit

Permalink
Changes added to be compatible with the new octoprint version.
Browse files Browse the repository at this point in the history
Tested with version 1.8.6
Check for added settings version with pop to remove from dict.
passwordHash for users added to fullfill signature_key_for_user check.
passwordHash is generated after each successful ldap check with used password.
Added fresh parameter for find_user function to ensure compatibility.
  • Loading branch information
BlackJaecky committed Oct 25, 2022
1 parent a93da0d commit 0c4381b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions octoprint_auth_ldap/group_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _load(self):
try:
with io.open(self._groupfile, 'rt', encoding='utf-8') as f:
data = yaml.safe_load(f)
version = data.pop("_version", 1)

if "groups" not in data:
groups = data
Expand Down
3 changes: 2 additions & 1 deletion octoprint_auth_ldap/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LDAPUser(User):
def __init__(
self,
username,
passwordHash=None,
active=True,
permissions=None,
groups=None,
Expand All @@ -21,7 +22,7 @@ def __init__(
User.__init__(
self,
username=username,
passwordHash=None,
passwordHash=passwordHash,
active=active,
permissions=permissions,
groups=groups,
Expand Down
10 changes: 8 additions & 2 deletions octoprint_auth_ldap/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os

import yaml
from passlib import pwd
from ldap.filter import filter_format
from octoprint.access.users import FilebasedUserManager, User, UserAlreadyExists
from octoprint.util import atomic_write
Expand All @@ -27,7 +28,7 @@ def __init__(self, plugin, ldap, **kwargs):
def group_manager(self):
return self._group_manager

def find_user(self, userid=None, apikey=None, session=None):
def find_user(self, userid=None, apikey=None, session=None, fresh=False):
self.logger.debug("Search for userid=%s, apiKey=%s, session=%s" % (userid, apikey, session))
user = FilebasedUserManager.find_user(self, userid=userid, apikey=apikey, session=session)
user, userid = self._find_user_with_transformation(apikey, session, user, userid)
Expand Down Expand Up @@ -80,7 +81,7 @@ def _find_user_with_transformation(self, apikey, session, user, userid):

def add_user(self,
username,
password=None,
password=pwd.genword(entropy=52, length=20),
active=False,
permissions=None,
groups=None,
Expand Down Expand Up @@ -112,6 +113,7 @@ def add_user(self,

self._users[username] = LDAPUser(
username=username,
passwordHash=LDAPUserManager.create_password_hash(password, settings=self._settings),
active=active,
permissions=permissions,
groups=groups,
Expand All @@ -133,6 +135,8 @@ def check_password(self, username, password):
client = self.ldap.get_client(user.distinguished_name, password)
authenticated = client is not None
self.logger.debug("%s was %sauthenticated" % (user.get_name(), "" if authenticated else "not "))
if authenticated:
user._passwordHash = LDAPUserManager.create_password_hash(password, settings=self._settings)
return authenticated
else:
self.logger.debug("%s is inactive or no longer a member of required groups" % user.get_id())
Expand All @@ -158,6 +162,8 @@ def _load(self):
self._customized = True
with io.open(self._userfile, 'rt', encoding='utf-8') as f:
data = yaml.safe_load(f)
version = data.pop("_version", 1)

for name, attributes in data.items():
permissions = self._to_permissions(*attributes.get("permissions", []))
groups = attributes.get("groups", {
Expand Down

0 comments on commit 0c4381b

Please sign in to comment.