Skip to content

Commit eccc305

Browse files
Nickolay PonomarevNickolay Ponomarev
authored andcommitted
Sync up examples/basic_auth with the README
As noted in: #5 #43 - @login_required can't be used without like that, so it makes sense to replace it with the more basic `@basic_auth_required`. - It's easy to forget to configure `LDAP_HOST`, and many people probably are trying to connect to a pre-existing directory, instead of bringing up their own.
1 parent a3aa363 commit eccc305

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,29 @@ Flask-SimpleLDAP depends, and will install for you, recent versions of Flask
1818
(0.12.4 or later) and [python-ldap](https://python-ldap.org/).
1919
Please consult the [python-ldap installation instructions](https://www.python-ldap.org/en/latest/installing.html) if you get an error during installation.
2020

21-
Next, add a ``LDAP`` instance to your code and at least the three
22-
required configuration options:
21+
Next, add an ``LDAP`` instance to your code and at least the three
22+
required configuration options. The complete sample from
23+
[examples/basic_auth/app.py](examples/basic_auth/app.py) looks like this:
2324

2425
```python
25-
from flask import Flask
26+
from flask import Flask, g
2627
from flask_simpleldap import LDAP
2728

2829
app = Flask(__name__)
30+
#app.config['LDAP_HOST'] = 'ldap.example.org' # defaults to localhost
2931
app.config['LDAP_BASE_DN'] = 'OU=users,dc=example,dc=org'
3032
app.config['LDAP_USERNAME'] = 'CN=user,OU=Users,DC=example,DC=org'
3133
app.config['LDAP_PASSWORD'] = 'password'
3234

3335
ldap = LDAP(app)
3436

35-
36-
@app.route('/ldap')
37-
@ldap.login_required
38-
def ldap_protected():
39-
return 'Success!'
40-
37+
@app.route('/')
38+
@ldap.basic_auth_required
39+
def index():
40+
return 'Welcome, {0}!'.format(g.ldap_username)
4141

4242
if __name__ == '__main__':
4343
app.run()
44-
4544
```
4645

4746
You can take a look at [examples/groups](examples/groups) for a more complete
@@ -61,7 +60,7 @@ configuration, add the following at least LDAP_USER_OBJECT_FILTER and
6160
LDAP_USER_OBJECT_FILTER.
6261

6362
```python
64-
from flask import Flask
63+
from flask import Flask, g
6564
from flask_simpleldap import LDAP
6665

6766
app = Flask(__name__)
@@ -86,16 +85,13 @@ app.config['LDAP_GROUP_MEMBER_FILTER_FIELD'] = "cn"
8685

8786
ldap = LDAP(app)
8887

89-
90-
@app.route('/ldap')
91-
@ldap.login_required
92-
def ldap_protected():
93-
return 'Success!'
94-
88+
@app.route('/')
89+
@ldap.basic_auth_required
90+
def index():
91+
return 'Welcome, {0}!'.format(g.ldap_username)
9592

9693
if __name__ == '__main__':
9794
app.run()
98-
9995
```
10096

10197
Resources

examples/basic_auth/app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from flask import Flask, g, request, session, redirect, url_for
1+
from flask import Flask, g
22
from flask_simpleldap import LDAP
33

44
app = Flask(__name__)
5-
app.secret_key = 'dev key'
6-
app.debug = True
7-
8-
app.config['LDAP_HOST'] = 'ldap.example.org'
5+
#app.config['LDAP_HOST'] = 'ldap.example.org' # defaults to localhost
96
app.config['LDAP_BASE_DN'] = 'OU=users,dc=example,dc=org'
107
app.config['LDAP_USERNAME'] = 'CN=user,OU=Users,DC=example,DC=org'
118
app.config['LDAP_PASSWORD'] = 'password'

examples/basic_auth/app_oldap.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
from flask import Flask, g, request, session, redirect, url_for
1+
from flask import Flask, g
22
from flask_simpleldap import LDAP
33

44
app = Flask(__name__)
5-
app.secret_key = 'dev key'
6-
app.debug = True
75

8-
app.config['LDAP_OPENLDAP'] = True
9-
app.config['LDAP_OBJECTS_DN'] = 'dn'
6+
# Base
107
app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication'
118
app.config['LDAP_HOST'] = 'openldap.example.org'
129
app.config['LDAP_BASE_DN'] = 'dc=users,dc=openldap,dc=org'
1310
app.config['LDAP_USERNAME'] = 'cn=user,ou=servauth-users,dc=users,dc=openldap,dc=org'
1411
app.config['LDAP_PASSWORD'] = 'password'
12+
13+
# OpenLDAP
14+
app.config['LDAP_OPENLDAP'] = True
15+
app.config['LDAP_OBJECTS_DN'] = 'dn'
1516
app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=inetOrgPerson)(uid=%s))'
1617

18+
# Groups
19+
app.config['LDAP_GROUP_MEMBERS_FIELD'] = "uniquemember"
20+
app.config['LDAP_GROUP_OBJECT_FILTER'] = "(&(objectclass=groupOfUniqueNames)(cn=%s))"
21+
app.config['LDAP_GROUP_MEMBER_FILTER'] = "(&(cn=*)(objectclass=groupOfUniqueNames)(uniquemember=%s))"
22+
app.config['LDAP_GROUP_MEMBER_FILTER_FIELD'] = "cn"
23+
1724
ldap = LDAP(app)
1825

1926
@app.route('/')

0 commit comments

Comments
 (0)