forked from philchristensen/txOpenID
/
db.py
315 lines (256 loc) · 8.51 KB
/
db.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# txopenid
# Copyright (c) 2007 Phil Christensen
#
# See LICENSE for details
"""
DB persistence layer.
At this time, the persistence features of txOpenID are fairly dependant
on MySQL. However, every attempt has been made to keep MySQL-specific
code in this module, in hopes of making it easy to support new DB types.
"""
import time
from MySQLdb import cursors
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.enterprise import adbapi
class Connection(object):
"""
A representation of a database connection.
This is pretty much just a wrapper around
L{twisted.enterprise.adbapi.ConnectionPool}. This class
just adds a bunch of convenience methods for the limited
amount of DB access needed by txOpenID.
"""
def __init__(self, **kwargs):
"""
Create a new connection to the DB.
"""
self.conn = adbapi.ConnectionPool('MySQLdb',
host=kwargs.get('host', 'localhost'),
db=kwargs.get('db', 'txopenid'),
user=kwargs.get('user', 'txopenid'),
passwd=kwargs.get('passwd', 'txopenid'),
cursorclass=cursors.SSDictCursor,
cp_reconnect=True,
cp_noisy=False,
cp_min=3,
cp_max=10,
)
@inlineCallbacks
def checkUserIdentity(self, user, identity):
"""
Does the provided user have the provided identity?
@param user: the user in question
@type user: L{txopenid.user.User}
@param identity: the identity to check
@type user: str
@return: True if there were no errors.
"""
identity_query = "SELECT 1 FROM identity WHERE user_id = %s and url = %s"
result = yield self.conn.runQuery(identity_query, [user['id'], identity])
if(result):
returnValue(True)
returnValue(False)
@inlineCallbacks
def getUserIdentities(self, user):
"""
Return a list of identities for this user.
@param user: the user in question
@type user: L{txopenid.user.User}
@return: a list of identity records
@rtype: list(dict(column => value))
"""
identity_query = "SELECT * FROM identity WHERE user_id = %s"
result = yield self.conn.runQuery(identity_query, [user['id']])
returnValue(result)
@inlineCallbacks
def getUserIdForIdentity(self, identity):
"""
Return the user who posesses the provided identity.
@param identity: the identity to check for
@type user: str
@return: the registered user's ID
"""
identity_query = "SELECT user_id FROM identity WHERE url = %s"
result = yield self.conn.runQuery(identity_query, [identity])
if(result):
returnValue(result[0]['user_id'])
returnValue(None)
@inlineCallbacks
def saveUserIdentity(self, user, identity):
"""
Add the provided identity to the user's list.
@param user: the user in question
@type user: L{txopenid.user.User}
@param identity: the identity to save
@type user: str
@return: True if there were no errors.
"""
identity_operation = "INSERT INTO identity (user_id, url) VALUES (%s, %s)"
yield self.conn.runOperation(identity_operation, [user['id'], identity])
returnValue(True)
@inlineCallbacks
def removeUserIdentities(self, user, identity_ids):
"""
Remove these identities for this user.
@param user: the current user
@type user: L{txopenid.user.User}
@param identity_ids: a list of IDs to remove
@type identity_ids: int or list(int)
@return: True if there were no errors.
"""
if not(isinstance(identity_ids, (list, tuple))):
identity_ids = [identity_ids]
identity_operation = "DELETE FROM identity WHERE user_id = %%s AND id IN (%s)" % ','.join(['%s']*len(identity_ids))
yield self.conn.runOperation(identity_operation, [user['id']] + identity_ids)
returnValue(True)
@inlineCallbacks
def checkUserTrust(self, user, root):
"""
Does the provided user trust provided root URL?
@param user: the user in question
@type user: L{txopenid.user.User}
@param identity: the root URL to check
@type user: str
@return: True if there were no errors.
"""
trust_query = "SELECT 1 FROM trusted_root WHERE user_id = %s and url = %s"
result = yield self.conn.runQuery(trust_query, [user['id'], root])
if(result):
returnValue(True)
returnValue(False)
@inlineCallbacks
def getUserTrustedRoots(self, user):
"""
Return a list of trusted roots for this user.
@param user: the user in question
@type user: L{txopenid.user.User}
@return: a list of trusted_root records
@rtype: list(dict(column => value))
"""
trust_query = "SELECT * FROM trusted_root WHERE user_id = %s"
result = yield self.conn.runQuery(trust_query, [user['id']])
returnValue(result)
@inlineCallbacks
def saveUserRoot(self, user, root):
"""
Add the provided root to the user's list.
@param user: the user in question
@type user: L{txopenid.user.User}
@param identity: the root to trust
@type user: str
@return: True if there were no errors.
"""
root_operation = "INSERT INTO trusted_root (user_id, url) VALUES (%s, %s)"
yield self.conn.runOperation(root_operation, [user['id'], root])
returnValue(True)
@inlineCallbacks
def removeUserRoots(self, user, root_ids):
"""
Remove these roots for this user.
@param user: the current user
@type user: L{txopenid.user.User}
@param identity_ids: a list of IDs to remove
@type identity_ids: int or list(int)
@return: True if there were no errors.
"""
if not(isinstance(root_ids, (list, tuple))):
root_ids = [root_ids]
root_operation = "DELETE FROM trusted_root WHERE user_id = %%s AND id IN (%s)" % ','.join(['%s']*len(root_ids))
yield self.conn.runOperation(root_operation, [user['id']] + root_ids)
returnValue(True)
@inlineCallbacks
def loadUser(self, user_id):
"""
Return the user object for this user_id.
@param user_id: the user ID to load.
@type user_id: int
"""
user_query = "SELECT * FROM user u WHERE u.id = %s"
result = yield self.conn.runQuery(user_query, [user_id])
if(result):
from txopenid import user
if(result[0]):
u = user.User(result[0])
u.pool = self
returnValue(u)
returnValue(None)
@inlineCallbacks
def loadSession(self, sid):
"""
Return the session record for this user_id.
@param user_id: the user ID to load.
@type user_id: int
@return: the session record
@rtype: L{dict}
"""
session_query = "SELECT * FROM session s WHERE s.id = %s"
result = yield self.conn.runQuery(session_query, [sid])
if(result):
returnValue(result[0])
returnValue(None)
@inlineCallbacks
def saveSession(self, session):
"""
Save the provided session record.
@param session: the session record to save
@type session: dict
@return: True if there were no errors.
"""
if(session.get('_new') is None):
fields = []
values = []
for column, value in session.items():
if(column != 'id'):
fields.append('%s = %%s' % column)
values.append(value)
values.append(session['id'])
save_query = "UPDATE session SET %s WHERE id = %%s" % ', '.join(fields)
else:
del session['_new']
keys = session.keys()
values = session.values()
save_query = "INSERT INTO session (%s) VALUES (%s)" % (', '.join(keys), ', '.join(['%s'] * len(values)))
yield self.conn.runOperation(save_query, values)
returnValue(True)
@inlineCallbacks
def verifyLogin(self, username, password):
"""
Get the user_id for the provided username and password.
@param username: the login username
@type username: str
@param password: the login password
@type password: str
@return: the user_id, or None
@rtype: int
"""
session_query = "SELECT id FROM user u WHERE u.username = %s AND u.crypt = ENCRYPT(%s, SUBSTRING(u.crypt, 1, 2))"
result = yield self.conn.runQuery(session_query, [username, password])
if(result):
returnValue(result[0]['id'])
returnValue(None)
@inlineCallbacks
def verifySession(self, sid):
"""
Get the user_id for the provided session ID.
@param sid: the login session ID
@type sid: str
@return: the user_id, or None
@rtype: int
"""
session_query = "SELECT * FROM session s WHERE s.id = %s AND accessed - created < timeout"
result = yield self.conn.runQuery(session_query, [sid])
if(result):
returnValue(result[0]['user_id'])
returnValue(None)
def destroySession(self, sid):
"""
Remove expired sessions from the database.
"""
destroy_query = "DELETE FROM session WHERE id = %s"
return self.conn.runOperation(destroy_query, [sid])
def cleanupSessions(self):
"""
Remove expired sessions from the database.
"""
cleanup_query = "DELETE FROM session WHERE timeout < (%s - accessed)"
return self.conn.runOperation(cleanup_query, [int(time.time())])