Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.39 KB | None | 0 0
  1. ## -*- coding: utf-8 -*-
  2. ############################################
  3. ## File : auth.py
  4. ## Author : Sylvain Viollon
  5. ## Email : sylvain@infrae.com
  6. ## Creation Date : Fri May 16 10:28:49 2008 CEST
  7. ## Last modification : Mon Jul 14 13:30:39 2008 CEST
  8. ############################################
  9.  
  10. __author__ ="sylvain@infrae.com"
  11. __format__ ="plaintext"
  12. __version__ ="$Id$"
  13.  
  14. from authkit.authenticate import middleware as AuthenticateMiddleware
  15. from authkit.authorize import middleware as AuthorizeMiddleware
  16. from authkit.permissions import RemoteUser
  17.  
  18. from watlabws.odbc import  ODBCConnectionPool
  19. import csv
  20.  
  21. class FileAuthChecker(object):
  22.     """Check the authentication using a CSV file.
  23.    """
  24.  
  25.     def __init__(self, filename):
  26.         self.filename = filename
  27.  
  28.     def __call__(self, environ, user, password):
  29.         csv_file = csv.reader(open(self.filename))
  30.         for csv_user, csv_password in csv_file:
  31.             if (csv_user == user) and (csv_password == password):
  32.                 return True
  33.         return False
  34.  
  35.  
  36. SQL_REQUEST = """select %(login_column)s as username, %(password_column)s as password
  37. from %(user_table)s
  38. where %(login_column)s = '%%s'"""
  39.  
  40.  
  41. class ODBCAuthChecker(object):
  42.     """Check the authentication using a ODBC connection.
  43.    """
  44.  
  45.     def __init__(self, dsn, user_table, login_column, password_column,
  46.                  number_connection=1, max_request=1000):
  47.         sql_args = dict(login_column=login_column,
  48.                         password_column=password_column,
  49.                         user_table=user_table)
  50.         self.sql_request = SQL_REQUEST %  sql_args
  51.         self.connection = ODBCConnectionPool(dsn, number_connection, max_request)
  52.  
  53.     def __call__(self, environ, user, password):
  54.         entries = list(self.connection.execute(self.sql_request % user))
  55.         if len(entries) != 1:
  56.             return False
  57.         if (entries[0]['username'] == user and
  58.         entries[0]['password'] == password):
  59.             return True
  60.         return False
  61.  
  62.  
  63. def authorize_filter(global_conf):
  64.     """Create a middleware to authorize.
  65.    """
  66.  
  67.     def filter(app):
  68.         return AuthorizeMiddleware(app, RemoteUser())
  69.     return filter
  70.  
  71.  
  72. def buildAuthenticationMiddleware(checkAuth, conf):
  73.     """Build a middleware checking the authentication using the given
  74.    auth checker.
  75.    """
  76.  
  77.     def filter(app):
  78.         return AuthenticateMiddleware(app,
  79.                                       setup_method=conf.get('method', 'basic'),
  80.                                       basic_realm=conf.get('realm', 'Auth Realm'),
  81.                                       basic_authenticate_function=checkAuth)
  82.     return filter
  83.  
  84.  
  85. def authenticate_text_filter(global_conf, passfile, **conf):
  86.     """Create a middleware for authentication using a CSV file.
  87.    """
  88.  
  89.     checkAuth = FileAuthChecker(passfile)
  90.     return buildAuthenticationMiddleware(checkAuth, conf)
  91.  
  92.  
  93. def authenticate_odbc_filter(global_conf, dsn, user_table, login_column,
  94.                              password_column, number_connection=1, max_request=1000,
  95.                              **conf):
  96.     """Create a middleware for authentication using an mxODBC connection.
  97.    """
  98.  
  99.     checkAuth = ODBCAuthChecker(dsn, user_table, login_column, password_column,
  100.                                 number_connection, max_request)
  101.     return buildAuthenticationMiddleware(checkAuth, conf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement