Python Forum
Uninitialized ASN.1 value in Flask LDAP3 Auth blueprint
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uninitialized ASN.1 value in Flask LDAP3 Auth blueprint
#1
I'm trying to put together a flask blueprint for LDAP3 auth. I started out with a standard flask app and that works fine but as soon as I turn it into a blueprint, it fails to work as expected.

Here's the debug output when I run the flask app

Output:
DEBUG:root:Validating LDAPLoginForm against LDAP DEBUG:flask_ldap3_login:Opening connection with bind user 'XXXX@XXXX.COM' DEBUG:flask_ldap3_login:Successfully bound to LDAP as 'XXXX@XXXX.COM' for search_bind method DEBUG:flask_ldap3_login:Performing an LDAP Search using filter '(&(objectclass=person)(sAMAccountName=YYYY))', base 'ou=Users,ou=XXXX,dc=XXXX,dc=COM', and scope 'SUBTREE' DEBUG:flask_ldap3_login:Opening connection with bind user 'CN=YYYY,OU=Admin Users,OU=Users,OU=XXXX,DC=XXXX,DC=COM' DEBUG:flask_ldap3_login:Directly binding a connection to a server with user:'CN=YYYY,OU=Admin Users,OU=Users,OU=XXXX,DC=XXXX,DC=COM' DEBUG:flask_ldap3_login:Authentication was successful for user 'YYYY'
And here's the debug output when run as a blueprint

Output:
DEBUG:root:Validating LDAPLoginForm against LDAP DEBUG:flask_ldap3_login:Opening connection with bind user 'XXXX@XXXX.COM' DEBUG:flask_ldap3_login:Destroying connection at <0x7f181f9ee2b0> ERROR:flask_ldap3_login:Uninitialized ASN.1 value ("__len__" attribute looked up)
My __init__.py looks like this:

1
2
3
4
5
6
7
from flask import Flask
 
app = Flask(__name__)
app.config.from_object('config')
 
from app.ldauth.views import auth_blueprint
app.register_blueprint(auth_blueprint)
And app/ldauth/views.py looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, Blueprint, url_for
from flask_ldap3_login import LDAP3LoginManager
from flask_login import LoginManager, login_user, UserMixin, current_user
from flask import render_template_string, render_template, redirect
from flask_ldap3_login.forms import LDAPLoginForm
from app import app
 
auth_blueprint = Blueprint('ldauth',__name__,template_folder='templates')
 
login_manager = LoginManager(app)             
ldap_manager = LDAP3LoginManager(app)         
users = {}
 
class User(UserMixin):
    def __init__(self, dn, username, data):
        self.dn = dn
        self.username = username
        self.data = data
 
    def __repr__(self):
        return self.dn
 
    def get_id(self):
        return self.dn
 
@login_manager.user_loader
def load_user(id):
    if id in users:
        return users[id]
    return None
 
 
@ldap_manager.save_user
def save_user(dn, username, data, memberships):
    user = User(dn, username, data)
    users[dn] = user
    return user
 
@auth_blueprint.route('/login', methods=['GET', 'POST'])
def login():
    template = """
    {{ get_flashed_messages() }}
    {{ form.errors }}
    <form method="POST">
        <label>Username{{ form.username() }}</label>
        <label>Password{{ form.password() }}</label>
        {{ form.submit() }}
        {{ form.hidden_tag() }}
    </form>
    """
 
    # Instantiate a LDAPLoginForm which has a validator to check if the user
    # exists in LDAP.
 
    form = LDAPLoginForm()
 
    if form.validate_on_submit():
        # Successfully logged in, We can now access the saved user object
        # via form.user.
        login_user(form.user)  # Tell flask-login to log them in.
 
        # TODO: Validate next to ensure it is safe!
        return redirect(next# Send them home
 
    return render_template_string(template,form=form)
Fairly inexperienced with python so maybe I am just doing something fundamentally wrong here. Any suggestions?
Reply
#2
Turns out this was environment related, created a new virtualenv this morning, deployed the code into that and everything is working as expected.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Google calendar pthon flask auth Kireta 0 2,820 Sep-16-2019, 04:50 PM
Last Post: Kireta
  Flask tutorial errors in jinja, auth undefined Ecniv 2 3,251 May-03-2019, 02:04 PM
Last Post: Ecniv
  Problem enabling http auth in a route nikos 2 3,596 Mar-02-2019, 01:13 PM
Last Post: nikos
  Refactored web app with flasks blueprint - 404 error jolsendev 1 4,248 Oct-26-2018, 03:23 AM
Last Post: jolsendev
  Python /Flask Login with LDAP Auth pythonnubie 4 10,185 Apr-16-2018, 03:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020