-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth42.py
executable file
·89 lines (72 loc) · 2.46 KB
/
auth42.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import base64
import ldap
class Auth42:
"""
This class provides methods to interact with 42's ldap
"""
_server_url = 'ldaps://ldap.42.fr'
_base_dn = 'ou=paris,ou=people,dc=42,dc=fr'
def __init__(self):
self.server = self._new_connection()
self.search = None
@staticmethod
def _new_connection():
"""
Tries to connect with ldap server -> LDAPObject
"""
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
ldap.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
try:
return ldap.initialize(Auth42._server_url)
except ldap.SERVER_DOWN:
return None
def close_connection(self):
"""
Close connection with ldap server
"""
self.server.unbind()
def ldap_search(self, user):
"""
Tries to search for given user -> Dict
"""
if self.search is not None and self.search.get('login') == user: return self.search.get('data')
data = self.server.search_s(self._base_dn, ldap.SCOPE_SUBTREE, '(uid=' + user + ')')
if len(data): self.search = {'login': user, 'data': data[0][1]}
else: self.search = None
return self.search
def ldap_get_email(self, user):
"""
Tries to get email for given user -> String
"""
result = self.ldap_search(user)
if result is not None:
alias = result.get('data').get('alias')
return [m for m in alias if m == user + '@student.42.fr'][0]
return None
def ldap_get_fullname(self, user):
"""
Tries to get fullname of given user -> String
"""
result = self.ldap_search(user)
if result is not None:
return result.get('data').get('cn')[0]
return None
def ldap_get_number(self, user):
"""
Tries to get the number of given user -> String
"""
result = self.ldap_search(user)
if result is not None:
return result.get('data').get('mobile')[0]
return None
def ldap_authenticate(self, user, password, pool_month, pool_year):
"""
Tries to authenticate user -> Boolean
"""
student_info = ',ou=' + pool_month + ',ou=' + pool_year + ','
dn = 'uid=' + user + student_info + self._base_dn
try:
self.server.simple_bind_s(dn, password)
return True
except ldap.LDAPError:
return False