OS Dependencies
Debian
apt install python3-dev libldap2-dev libsasl2-dev
Centos
yum install python3-devel openldap-devel.x86_64 libgsasl-devel.x86_64
Alpine
apk add musl-dev openldap-dev gcc libgsasl-dev
Install
pip install Flask-LDAPAuth
Settings
App configs
| NAME | Default |
|---|---|
| LDAP_URL | None |
| LDAP_ROOTDN | None |
| LDAP_USERDN | None |
| LDAP_GROUP | False |
| LDAP_START_TLS | True |
| LDAP_USER_FILTER | 'cn' |
| LDAP_TIMEOUT | 10 |
| SECRET_KEY | None |
| LDAP_TOKEN_EXPIRE | 8 |
Examples
Simple User Authentication
from flask import Flask, jsonify, make_response
from flask_ldapauth import LDAPAuth
app = Flask(__name__)
app.config['LDAP_URL'] = "ldap://localhost:389"
app.config['LDAP_ROOTDN'] = "dc=localhost"
app.config['LDAP_USERDN'] = "ou=People,dc=localhost"
auth = LDAPAuth(app)
@app.route('/login')
def index():
user = auth.connect(username='nmacias',
password='password')
if user is False:
return jsonify({'mesg': 'invalid username or password'}), 401
return jsonify({'mesg': 'login'})
def run():
app.config['DEBUG'] = True
app.config['ENV'] = 'development'
app.run()
def main():
run()
if __name__ == '__main__':
main()
Advance User Authenciation
- Only allow user if part of a group
- Create auth token cookies
- Protect routes
- Return JSON data on protected routes
from flask import Flask, jsonify, make_response
from flask_ldapauth import LDAPAuth
app = Flask(__name__)
app.config['LDAP_URL'] = "ldap://localhost:389"
app.config['LDAP_ROOTDN'] = "dc=localhost"
app.config['LDAP_USERDN'] = "ou=People,dc=localhost"
app.config['LDAP_GROUP'] = "admins"
app.config['SECRET_KEY'] = 'supersecretkey'
auth = LDAPAuth(app)
@app.route('/protected', methods=['GET', 'POST'])
@auth.protected(data=True)
def propected(data):
return jsonify({'mesg': 'Top secret', 'name': data['name']})
@app.route('/login')
def login():
user = auth.connect(username='nmacias',
password='password', return_user=True)
if user is False:
return jsonify({'mesg': 'invalid username or password'}), 401
token = auth.token.create(payload=user)
response = make_response(jsonify({'token': token}))
response.set_cookie('token', value=token, httponly=True)
return response
@app.route('/logout')
def logout():
response = make_response(jsonify({}))
response.set_cookie('token', expires=0)
return response
@app.route('/validate/<token>')
def validate(token):
token_validate = auth.token.validate(token=token)
if token_validate is False:
return jsonify({'mesg': 'Invalid token'}), 401
return jsonify({})
def run():
app.config['DEBUG'] = True
app.config['ENV'] = 'development'
app.run()
def main():
run()
if __name__ == '__main__':
main()
Release files for flask-ldapauth 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-LDAPAuth-0.0.3.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_LDAPAuth-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / Flask-LDAPAuth-0.0.3.tar.gz
| Download URL | Flask-LDAPAuth-0.0.3.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7988fbda81772f41aad90ff81ab3e1259831562a2a2a37884d0a5666164407bd
|
|
BLAKE2b-256 checksum How to use checksums |
ccad3d1f3d3bb639e06647d98c1d609930e642f5cb2751f9fddd5564506984f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
|
Release files / Flask_LDAPAuth-0.0.3-py3-none-any.whl
| Download URL | Flask_LDAPAuth-0.0.3-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
783814d3366661f85c616d33ff2b178af3447b8c242a44c29c12cd1755fce3f0
|
|
BLAKE2b-256 checksum How to use checksums |
6bf61f4a916a0b77c61dadc9ba1d22e8af814848d2ebad6d572b5c985135abc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.0 importlib_metadata/3.7.3 packaging/20.9 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5
|