-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathverify_password2.py
executable file
·71 lines (54 loc) · 2.12 KB
/
verify_password2.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
#!/usr/bin/python
#ref: https://macadmins.slack.com/files/elios/F0JA6JD6V/Untitled.py
from OpenDirectory import *
import getpass
username = getpass.getuser().decode(u"utf-8")
password = getpass.getpass((u"password for %s: " % username).encode(u"utf-8"))
class ODException(Exception):
'''Base exception for OpenDirectory'''
class ODSessionException(Exception):
'''ODSessionException'''
class ODNodeException(ODException):
'''ODNode exception'''
class ODQueryException(ODException):
'''ODQueryException'''
class ODRecordException(ODException):
'''ODRecordException'''
class ODPasswordException(ODException):
'''ODPasswordException'''
def ODverifyPassword(username, password, dsnode='/Search'):
'''Uses the OpenDirectory framework to verify username and password.
Input: username, password, and optional DS node name.
Output: True if username and password are verified
Exceptions: all sorts!
'''
session = ODSession.defaultSession()
if not session:
raise ODSessionException('Could not get default Open Directory session')
node, error = ODNode.nodeWithSession_name_error_(session, dsnode, None)
if error:
raise ODNodeException(error)
query, error = ODQuery.queryWithNode_forRecordTypes_attribute_matchType_queryValues_returnAttributes_maximumResults_error_(
node,
kODRecordTypeUsers,
kODAttributeTypeRecordName,
kODMatchEqualTo,
username,
kODAttributeTypeStandardOnly,
1,
None )
if error:
raise ODQueryException(error)
results, error = query.resultsAllowingPartial_error_(False, None)
if error:
raise ODQueryException(error)
if results:
record = results[0]
passwordVerified, error = record.verifyPassword_error_(password, None)
if error and error.code() != 5000: # 5000 means invalid user or password
raise ODPasswordException(error)
return passwordVerified
else:
# no matching username in DS, so return False
return False
print ODverifyPassword(username, password)