-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
36 lines (31 loc) · 1.41 KB
/
utils.py
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
from ldap3 import Server, Connection, ALL_ATTRIBUTES
import config
from models import User
def is_admin(r: str) -> bool:
for g in [g.decode() for g in r['memberOf']]:
if 'CN=Domain Admins,' in g:
return True
return False
def ldap_auth(username: str, password: str) -> User:
s = Server(host=config.LDAP_HOST, port=config.LDAP_PORT, use_ssl=config.LDAP_SSL)
with Connection(s, user=(username + '@iseage.org'), password=password) as c:
u = None
if c.bind():
print("Successful bind for user " + username)
c.search(search_base=config.LDAP_BASE_DN,
search_filter='({})'.format(config.LDAP_FILTER.format(username)),
attributes=ALL_ATTRIBUTES)
r = c.response[0]['raw_attributes']
u, created = User.get_or_create(username=username,
defaults={'ldap': True,
'password': '',
'admin': is_admin(r)
})
if created:
print("Created new user from LDAP: " + username)
else:
u.admin = is_admin(r)
u.save()
else:
print("Failed to bind with user " + config.LDAP_FILTER.format(username) + config.LDAP_BASE_DN)
return u