-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmppauth.py
73 lines (56 loc) · 1.96 KB
/
xmppauth.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
from UserManager import UserManager
import Session
from Log import Log
import sasl
class XMPPAuth(sasl.auth.Authenticator):
"""To authenticate XMPP users.
Plan to support 2 methods:
PLAIN: just username & password
X-BBS-OAUTH: use OAuth token
"""
def __init__(self, service_type, host, service_name):
self._service_type = service_type
self._host = host
self._service_name = service_name
self._username = None
def service_type(self):
return self._service_type
def host(self):
return self._host
def service_name(self):
return self._service_name
def username(self):
return self._username
def password(self):
raise NotImplementedError
def get_password(self):
raise NotImplementedError
def verify_token(self, token):
"""Verify token"""
try:
result = Session.SessionManager.CheckSession(token)
if result is not None:
self._username = result
else:
Log.warn("XMPPAuth: fail to verify session")
return result is not None
except Exception as e:
Log.warn("XMPPAuth: exception in CheckSession: %r" % e)
return False
def verify_password(self, authorize, username, passwd):
"""Verify password"""
if (authorize and username != authorize):
Log.warn("XMPPAuth: user %s does not match authorize %s" % (username, authorize))
return False
username = username.encode("gbk")
# print "trying to auth %s pass %s" % (user, passwd)
user = UserManager.LoadUser(username)
if (user == None):
Log.warn("XMPPAuth: user not exist: %s" % username)
return False
if (user.Authorize(passwd)):
# print "OK"
return True
Log.warn("XMPPAuth: user %s auth failed!" % username)
# print "Wrong PW"
return False