# Path: txcas/settings.py
# def get_bool(value, default=False):
#     if value is None:
#         return False
#     value = str(value).strip().lower()
#     if value in ('y', '1', 't', 'yes', 'true'):
#         return True
#     elif value in ('n', '0', 'f', 'no', 'false'):
#         return False
#     else:
#         return default
# 
# def export_settings_to_dict(scp):
#     settings = {}
#     for section in scp.sections():
#         for option in scp.options(section):
#             settings.setdefault(section, {})[option] = scp.get(section, option)
#     return settings
# 
# def load_settings(config_basename, defaults=None, syspath=None, appdir=None):
#     """
#     Load settings.
#     """
#     if defaults is None:
#         defaults = {}
#     scp = load_defaults(defaults)
#     if appdir is None:
#         appdir = os.path.dirname(os.path.dirname(__file__))
#     paths = []
#     if syspath is not None:
#         paths.append(os.path.join(syspath, "%s.cfg" % config_basename))
#     paths.append(os.path.expanduser("~/%src" % config_basename))
#     paths.append(os.path.join(appdir, "%s.cfg" % config_basename))
#     scp.read(paths)
#     return scp


from textwrap import dedent
from txcas.settings import get_bool, export_settings_to_dict, load_settings
from ldaptor.protocols.ldap.ldapclient import LDAPClient
from ldaptor.protocols.ldap import ldapsyntax
from ldaptor.protocols.ldap.ldaperrors import LDAPInvalidCredentials
from twisted.cred import credentials
from twisted.cred.checkers import ICredentialsChecker
from twisted.cred.error import UnauthorizedLogin
from twisted.cred.strcred import ICheckerFactory
from twisted.internet import defer, reactor
from twisted.internet.endpoints import clientFromString, connectProtocol
from twisted.internet.ssl import Certificate, optionsForClientTLS, platformTrust
from twisted.plugin import IPlugin
from twisted.python import log
from zope.interface import implements
import sys
import txcas.utils
import pem
    # for the plugin system to find our factory.
    implements(ICheckerFactory, IPlugin)

    # This tells AuthOptionsMixin how to find this factory.
    authType = "ldap_simple_bind"

    # This is a one-line explanation of what arguments, if any,
    # your particular cred plugin requires at the command-line.
    argStringFormat = "A colon-separated key=value list."

    # This help text can be multiple lines. It will be displayed
    # when someone uses the "--help-auth-type special" command.
    authHelp = dedent('''\
            Uses a 2-stage BIND to determine if the credentials 
            presented are valid.  Valid options include:
            
            - endpointstr
            - basedn
            - binddn
            - bindpw 
            - query_template: default -> (uid=%(username)s)
              The %(username)s part will be interpolated with the (escaped)
              avatar ID.
            ''')

    # The types of credentials this factory supports.
    credentialInterfaces = (credentials.IUsernamePassword,)

    # This will be called once per command-line.
    def generateChecker(self, argstring=""):

# Path: txcas/settings.py
# def get_bool(value, default=False):
#     if value is None:
#         return False
#     value = str(value).strip().lower()
#     if value in ('y', '1', 't', 'yes', 'true'):
#         return True
#     elif value in ('n', '0', 'f', 'no', 'false'):
#         return False
#     else:
#         return default
# 
# def export_settings_to_dict(scp):
#     settings = {}
#     for section in scp.sections():
#         for option in scp.options(section):
#             settings.setdefault(section, {})[option] = scp.get(section, option)
#     return settings
# 
# def load_settings(config_basename, defaults=None, syspath=None, appdir=None):
#     """
#     Load settings.
#     """
#     if defaults is None:
#         defaults = {}
#     scp = load_defaults(defaults)
#     if appdir is None:
#         appdir = os.path.dirname(os.path.dirname(__file__))
#     paths = []
#     if syspath is not None:
#         paths.append(os.path.join(syspath, "%s.cfg" % config_basename))
#     paths.append(os.path.expanduser("~/%src" % config_basename))
#     paths.append(os.path.join(appdir, "%s.cfg" % config_basename))
#     scp.read(paths)
#     return scp


# Path: txcas/ldap_cred_checker.py
from textwrap import dedent
from txcas.settings import get_bool, export_settings_to_dict, load_settings
from ldaptor.protocols.ldap.ldapclient import LDAPClient
from ldaptor.protocols.ldap import ldapsyntax
from ldaptor.protocols.ldap.ldaperrors import LDAPInvalidCredentials
from twisted.cred import credentials
from twisted.cred.checkers import ICredentialsChecker
from twisted.cred.error import UnauthorizedLogin
from twisted.cred.strcred import ICheckerFactory
from twisted.internet import defer, reactor
from twisted.internet.endpoints import clientFromString, connectProtocol
from twisted.internet.ssl import Certificate, optionsForClientTLS, platformTrust
from twisted.plugin import IPlugin
from twisted.python import log
from zope.interface import implements
import sys
import txcas.utils
import pem

    # for the plugin system to find our factory.
    implements(ICheckerFactory, IPlugin)

    # This tells AuthOptionsMixin how to find this factory.
    authType = "ldap_simple_bind"

    # This is a one-line explanation of what arguments, if any,
    # your particular cred plugin requires at the command-line.
    argStringFormat = "A colon-separated key=value list."

    # This help text can be multiple lines. It will be displayed
    # when someone uses the "--help-auth-type special" command.
    authHelp = dedent('''\
            Uses a 2-stage BIND to determine if the credentials 
            presented are valid.  Valid options include:
            
            - endpointstr
            - basedn
            - binddn
            - bindpw 
            - query_template: default -> (uid=%(username)s)
              The %(username)s part will be interpolated with the (escaped)
              avatar ID.
            ''')

    # The types of credentials this factory supports.
    credentialInterfaces = (credentials.IUsernamePassword,)

    # This will be called once per command-line.
    def generateChecker(self, argstring=""):

        scp = load_settings('cas', syspath='/etc/cas')