title: django request transport to custom database backend
body: <p>My team is developing secure django project and we stacked with transport Django request into custom database backend. </p>
  
  <p>We made such an algorithm for GSSAPI(SPNEGO) authentication in django and delegating user credentials for server querying on behalf of user:</p>
  
  <pre><code>    if 'HTTP_AUTHORIZATION' in request.META:
          kind, initial_client_token = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
          if kind == 'Negotiate':
              server = 'HTTP@server.domain.ru'
              _ignore_result, krb_context = kerberos.authGSSServerInit(server)
              kerberos.authGSSServerStep(krb_context, initial_client_token)
              principal = kerberos.authGSSServerUserName(krb_context)
              _ignore_result = kerberos.authGSSServerStoreDelegate(krb_context)
              conn = psycopg2.connect(
                  host='krb5-dbhost',
                  user=principal,
                  dbname='db',
              )
              cursor = conn.cursor()
              cursor.execute("SELECT version()")
              records = cursor.fetchall()
  </code></pre>
  
  <p>this works well in django-view. Kerberos server can authorize user and cache krb5-ticket for credentials delegation to query in psycopg. Now we need to inject it into django.</p>
  
  <p>We want is inherit postgresql database backend like this:</p>
  
  <pre><code>from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper
  
  
  class CustomDatabaseWrapper(DatabaseWrapper):
      def __init__(self, *args, **kwargs):
          super(CustomDatabaseWrapper, self).__init__(*args, **kwargs)
  
      def get_connection_params(self):
          '''We need to customize this function,
          We need get request here when query processed by web interface,'''
          #.... the source code could be here, but it is not necessary
          return conn_params
  </code></pre>
  
  <p>So the question is: "How can we get request.META (for get user`s Negotiate token) in get_connection_params() function, and how can we divide users request from web interface from management commands."</p>
  
  <p>Sorry for my english skills.Thank you!</p>