From 9f57379f16733c2241d83ecb9081ed2e8e857719 Mon Sep 17 00:00:00 2001 From: Guillaume GILL Date: Tue, 7 Nov 2017 00:58:01 +0100 Subject: [PATCH] Initial commit --- octoprint_auth_ldap/__init__.py | 52 +++++++++++++++++++++++++++++++++ octoprint_skeleton/__init__.py | 18 ------------ setup.py | 10 +++---- 3 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 octoprint_auth_ldap/__init__.py delete mode 100644 octoprint_skeleton/__init__.py diff --git a/octoprint_auth_ldap/__init__.py b/octoprint_auth_ldap/__init__.py new file mode 100644 index 0000000..efc21c8 --- /dev/null +++ b/octoprint_auth_ldap/__init__.py @@ -0,0 +1,52 @@ +# coding=utf-8 +from __future__ import absolute_import + +from octoprint.users import FilebasedUserManager +from octoprint.settings import settings +import ldap +import uuid + + +class LDAPUserManager(FilebasedUserManager): + def checkPassword(self, username, password): + ldap_server = settings().get(["accessControl", "ldap_uri"]) + ldap_search_base = settings().get(["accessControl", "ldap_search_base"]) + if ldap_server is None or ldap_search_base is None: + self._logger.debug("LDAP conf error") + return False + dn = "uid=" + username + ",ou=users," + ldap_search_base + try: + connection = ldap.initialize(ldap_server) + connection.start_tls_s() + connection.bind_s(dn, password) + + user = self.findUser(username) + if not user: + self.addUser(username, uuid.uuid4(), True) + return True + except ldap.INVALID_CREDENTIALS: + self._logger.debug("LDAP : Your username or password is incorrect.") + return FilebasedUserManager.checkPassword(self, username, password) + except ldap.LDAPError, e: + if type(e.message) == dict: + for (k, v) in e.message.iteritems(): + self._logger.debug("%s: %sn" % (k, v)) + else: + self._logger.debug(e.message) + return False + +def changeUserPassword(self, username, password): + pass + +def ldap_user_factory(components, settings, *args, **kwargs): + return LDAPUserManager(); + +__plugin_name__ = "Auth LDAP" +__plugin_version__ = "1.0.0" +__plugin_description__ = "LDAP authentication" + +def __plugin_load__(): + global __plugin_hooks__ + __plugin_hooks__ = { + "octoprint.users.factory": ldap_user_factory + } diff --git a/octoprint_skeleton/__init__.py b/octoprint_skeleton/__init__.py deleted file mode 100644 index 9e0252b..0000000 --- a/octoprint_skeleton/__init__.py +++ /dev/null @@ -1,18 +0,0 @@ -# coding=utf-8 -from __future__ import absolute_import - -### (Don't forget to remove me) -# This is a basic skeleton for your plugin's __init__.py. You probably want to adjust the class name of your plugin -# as well as the plugin mixins it's subclassing from. This is really just a basic skeleton to get you started. - -import octoprint.plugin - -class SkeletonPlugin(octoprint.plugin.TemplatePlugin): - # TODO Implement me! - pass - -# If you want your plugin to be registered within OctoPrint under a different name than what you defined in setup.py -# ("OctoPrint-PluginSkeleton"), you may define that here. Same goes for the other metadata derived from setup.py that -# can be overwritten via __plugin_xyz__ control properties. See the documentation for that. -__plugin_name__ = "Plugin Skeleton" -__plugin_implementation__ = SkeletonPlugin() diff --git a/setup.py b/setup.py index 67b0b6e..2dfc210 100644 --- a/setup.py +++ b/setup.py @@ -4,14 +4,14 @@ ### Do not forget to adjust the following variables to your own plugin. # The plugin's identifier, has to be unique -plugin_identifier = "skeleton" +plugin_identifier = "auth_ldap" # The plugin's python package, should be "octoprint_", has to be unique plugin_package = "octoprint_%s" % plugin_identifier # The plugin's human readable name. Can be overwritten within OctoPrint's internal data via __plugin_name__ in the # plugin module -plugin_name = "OctoPrint-PluginSkeleton" +plugin_name = "OctoPrint-LDAP" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module plugin_version = "0.1" @@ -21,10 +21,10 @@ plugin_version = "0.1" plugin_description = "TODO" # The plugin's author. Can be overwritten within OctoPrint's internal data via __plugin_author__ in the plugin module -plugin_author = "TODO" +plugin_author = "Guillaume GILL" # The plugin's author's mail address. -plugin_author_email = "todo@example.com" +plugin_author_email = "guillaume.gill@petitchinois.net" # The plugin's homepage URL. Can be overwritten within OctoPrint's internal data via __plugin_url__ in the plugin module plugin_url = "TODO" @@ -33,7 +33,7 @@ plugin_url = "TODO" plugin_license = "AGPLv3" # Any additional requirements besides OctoPrint should be listed here -plugin_requires = [] +plugin_requires = ["ldap","uuid"] # Additional package data to install for this plugin. The subfolders "templates", "static" and "translations" will # already be installed automatically if they exist.