-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwebapi.py
377 lines (286 loc) · 12.1 KB
/
webapi.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import vanilla
import base64
import json
import itertools
import datetime
import multipart
import sys
import torrents
import logging
import urlparse
import string
from restInterface import *
import fairywren
import math
import users
import struct
import socket
def decodePassword(password):
#Password comes across as 64 bytes of base64 encoded data
#with trailing ='s lopped off.
password += '=='
if len(password) != 88: #64 bytes in base64 is length 88
return None
try:
return base64.urlsafe_b64decode(password)
except TypeError:
return None
def validateUsername(username):
allowedChars = string.digits + string.ascii_lowercase
for c in username:
if c not in allowedChars:
return None
return username
def toJsonDict(someString):
try:
r = json.loads(someString)
except ValueError:
return None
if not isinstance(r,dict):
return None
return r
def extractUserId(*pathComponents):
return int(pathComponents[1],16)
class Webapi(restInterface):
MAX_TORRENTS_PER_RESULT = 50
def __init__(self,swarm,peers,users,authmgr,torrents,httpPathDepth,secure):
self.peers = peers
self.swarm = swarm
def authenticateUser(username,password):
#Password comes across as 64 bytes of base64 encoded data
#with trailing ='s lopped off.
password += '=='
if len(password) != 88: #64 bytes in base64 is length 88
return None
return vanilla.http_error(400,env,start_response,msg='password too short')
try:
password = base64.urlsafe_b64decode(password)
except TypeError:
return None
return vanilla.http_error(400,env,start_response,msg='password poorly formed')
return authmgr.authenticateUser(username,password)
def authorizeUser(session,roles):
return authmgr.isUserMemberOfRole(session.getId(),roles)
super(Webapi,self).__init__(httpPathDepth,authenticateUser,authorizeUser,secure)
self.authmgr = authmgr
self.torrents = torrents
self.users = users
self.log = logging.getLogger('fairywren.webapi')
self.log.info('Created')
def getResponseForSession(self,session):
return {'my' : {'href':fairywren.USER_FMT % session.getId()} }
def getRoles(self):
return [ res.getName() for res in self.getResources() if res.requiresAuthorization()]
@requireAuthorization()
@resource(True,'GET','swarm')
def getSwarm(self,env,start_response,session):
response = self.swarm.getPeers()
for peer in response.itervalues():
for d in peer:
d.pop('peerId')
return vanilla.sendJsonWsgiResponse(env,start_response,response)
@resource(True,'GET','roles')
def listRoles(self,env,start_response,session):
return vanilla.sendJsonWsgiResponse(env,start_response,{'roles':self.getRoles()})
@requireAuthorization()
@parameter('roles',array=True)
@resource(True,'POST','users',fairywren.UID_RE,'roles')
def changeRolesOfUser(self,env,start_response,session,uid,roles):
uid = int(uid,16)
try:
self.users.setUserRoles(roles,uid)
except ValueError as e:
return vanilla.http_error(400,env,start_response,msg=e.message)
return vanilla.sendJsonWsgiResponse(env,start_response,{'roles':self.users.getUserRoles(uid)})
@resource(True,'GET','users',fairywren.UID_RE,'roles')
def listRolesOfUser(self,env,start_response,session,uid):
uid = int(uid,16)
#Potential pitfall in this implementation:
#If the user for the uid does not exist, this stil returns an empty list
return vanilla.sendJsonWsgiResponse(env,start_response,{'roles':self.users.getUserRoles(uid)})
@authorizeSelf(extractUserId)
@requireAuthorization()
@resource(True,'GET','users',fairywren.UID_RE,'invites')
def listInvites(self,env,start_response,session,uid):
uid = int(uid,16)
#Potential pitfall in this implementation:
#If a user is authorized to perform a GET on this resource but
#user for the uid does not exist, this stil returns an empty list
response = { 'invites' : list(self.users.listInvitesByUser(uid)) }
return vanilla.sendJsonWsgiResponse(env,start_response,response)
@resource(False,'GET','invites',fairywren.SECRET_RE)
def inviteStatus(self,env,start_response,secret):
secret = base64.urlsafe_b64decode(secret + '=')
try:
claimed =self.users.getInviteState(secret)
except ValueError as e:
return vanilla.http_error(404,env,start_response,msg=e.message)
return vanilla.sendJsonWsgiResponse(env,start_response,{'claimed':claimed})
@parameter('password',decodePassword)
@parameter('username')
@resource(False,'POST','invites',fairywren.SECRET_RE)
def claimInvite(self,env,start_response,secret,username,password):
secret = base64.urlsafe_b64decode(secret + '=')
try:
newuser = self.users.claimInvite(secret,username,password)
except users.UserAlreadyExists:
return vanilla.http_error(409,env,start_response,msg='User with that name already exists')
except ValueError as e:
return vanilla.http_error(404,env,start_response,msg=e.message)
return vanilla.sendJsonWsgiResponse(env,start_response,{'href' : newuser})
@requireAuthorization()
@resource(True,'POST','invites')
def createInvite(self,env,start_response,session):
pathOfNewInvite = self.users.createInvite(session.getId())
return vanilla.sendJsonWsgiResponse(env,start_response,{'href':pathOfNewInvite})
@authorizeSelf(extractUserId)
@requireAuthorization('Administrator')
@parameter('password',decodePassword)
@resource(True,'POST','users',fairywren.UID_RE,'password')
def changePassword(self,env,start_response,session,uid,password):
uid = int(uid,16)
if None == self.authmgr.changePassword(uid,password):
return vanilla.http_error(400,env,start_response)
return vanilla.sendJsonWsgiResponse(env,start_response,{})
@resource(True,'GET','users',fairywren.UID_RE )
def userInfo(self,env,start_response,session,uid):
uid = int(uid,16)
response = self.users.getInfo(uid)
if response == None:
return vanilla.http_error(404,env,start_response)
if session.getId() == uid:
response['announce'] = { 'href': self.torrents.getAnnounceUrlForUser(uid) }
return vanilla.sendJsonWsgiResponse(env,start_response,response)
@requireAuthorization()
@parameter('password',decodePassword)
@parameter('username',validateUsername)
@resource(True,'POST','users')
def addUser(self,env,start_response,session,password,username):
try:
resourceForNewUser,_ = self.users.addUser(username,password)
except users.UserAlreadyExists:
return vanilla.http_error(409,env,start_response,'user already exists')
response = { 'href' : resourceForNewUser }
return vanilla.sendJsonWsgiResponse(env,start_response,response)
def searchTorrents(self,env,start_response,session,query):
tokens = query.get('token')
if tokens == None:
return vanilla.http_error(400,env,start_response,'search must have at least one instance of token parameter')
if len(tokens) > 5:
return vanilla.http_error(400,env,start_response,'search may not have more than 5 tokens')
listOfTorrents = []
for torrent in self.torrents.searchTorrents(tokens):
torrentInfoHash = torrent.pop('infoHash')
torrent.pop('id')
seeds, leeches = self.peers.getNumberOfPeers(torrentInfoHash)
torrent['seeds'] = seeds
torrent['leeches'] = leeches
listOfTorrents.append(torrent)
return vanilla.sendJsonWsgiResponse(env,start_response,
{'torrents': listOfTorrents})
@resource(True,'GET','torrents')
def listTorrents(self,env,start_response,session):
if 'QUERY_STRING' not in env:
query = {}
else:
query = urlparse.parse_qs(env['QUERY_STRING'])
if 'search' in query:
return self.searchTorrents(env,start_response,session,query)
#Use the first occurence of the supplied parameter
#With a default
resultSize = query.get('resultSize',[self.MAX_TORRENTS_PER_RESULT])[0]
try:
resultSize = int(resultSize)
except ValueError:
return vanilla.http_error(400,env,start_response,'resultSize must be integer')
#Use the first occurence of the supplied parameter
#With a default of zero
subset = query.get('subset',[0])[0]
try:
subset = int(subset)
except ValueError:
return vanilla.http_error(400,env,start_response,'subset must be integer')
listOfTorrents = []
for torrent in self.torrents.getTorrents(resultSize,subset):
torrent.pop('id')
torrentInfoHash = torrent.pop('infoHash')
seeds, leeches = self.peers.getNumberOfPeers(torrentInfoHash)
torrent['seeds'] = seeds
torrent['leeches'] = leeches
listOfTorrents.append(torrent)
return vanilla.sendJsonWsgiResponse(env,start_response,
{'torrents' : listOfTorrents ,'numSubsets' : int(math.ceil(self.torrents.getNumTorrents() / float(resultSize)))} )
@parameter('title')
@parameter('extended',toJsonDict)
@resource(True,'POST','torrents',fairywren.UID_RE + '.json')
def updateTorrent(self,env,start_response,session,uid,extended,title):
uid = int(uid,16)
try:
self.torrents.updateTorrent(uid,title,extended)
except ValueError as e:
return vanilla.http_error(404,env,start_response,msg=e.message)
return vanilla.sendJsonWsgiResponse(env,start_response,{})
@resource(True,'POST','torrents')
def createTorrent(self,env,start_response,session):
if not 'CONTENT_TYPE' in env:
return vanilla.http_error(411,env,start_response,'missing Content-Type header')
contentType = env['CONTENT_TYPE']
if 'multipart/form-data' not in contentType:
return vanilla.http_error(415,env,start_response,'must be form upload')
forms,files = multipart.parse_form_data(env)
if 'torrent' not in files or 'title' not in forms:
return vanilla.http_error(400,env,start_response,'missing torrent or title')
try:
extended = json.loads(forms.get('extended','{}'))
except ValueError:
return vanilla.http_error(400,env,start_response,'bad extended info')
if not isinstance(extended,dict):
return vanilla.http_error(400,env,start_response,'extended info must be dict')
data = files['torrent'].raw
try:
newTorrent = torrents.Torrent.fromBencodedData(data)
except ValueError as e:
return vanilla.http_error(400,env,start_response,str(e))
response = {}
response['redownload'] = newTorrent.scrub()
response['redownload'] |= self.torrents.getAnnounceUrlForUser(session.getId())!=newTorrent.getAnnounceUrl()
try:
url,infoUrl = self.torrents.addTorrent(newTorrent,forms['title'],session.getId(),extended)
except ValueError as e: #Thrown when a torrent already exists with this info hash
return vanilla.http_error(400,env,start_response,e.message)
response['metainfo'] = { 'href' : url }
response['info'] = { 'href' : infoUrl }
return vanilla.sendJsonWsgiResponse(env,start_response,response)
@requireAuthorization()
@resource(True,'DELETE','torrents',fairywren.UID_RE + '.torrent')
def deleteTorrent(self,env,start_response,session,uid):
uid = int(uid,16)
try:
torrent = self.torrents.deleteTorrent(uid)
except ValueError as e:
return vanilla.http_error(404,env,start_response,msg=e.message)
return vanilla.sendJsonWsgiResponse(env,start_response,{})
@resource(True,'GET','torrents',fairywren.UID_RE + '.torrent')
def downloadTorrent(self,env,start_response,session,uid):
uid = int(uid,16)
try:
torrent = self.torrents.getTorrentForDownload(uid,session.getId())
except ValueError as e:
return vanilla.http_error(404,env,start_response,msg=e.message)
headers = [('Content-Type','application/x-bittorrent')]
headers.append(('Content-Disposition','attachment; filename="%s.torrent"' % vanilla.sanitizeForContentDispositionHeaderFilename(torrent.getTitle()) ))
headers.append(('Cache-Control','no-cache'))
start_response('200 OK',headers)
return [torrent.raw()]
@resource(True,'GET','torrents',fairywren.UID_RE + '.json')
def torrentInfo(self,env,start_response,session,uid):
uid = int(uid,16)
response = self.torrents.getInfo(uid)
if response == None:
return vanilla.http_error(404,env,start_response,msg='no such torrent')
torrentInfoHash = response.pop('infoHash')
numSeeds,numLeeches = self.peers.getNumberOfPeers(torrentInfoHash)
response['extended'] = self.torrents.getExtendedInfo(uid)
response['seeds'] = numSeeds
response['leeches'] = numLeeches
return vanilla.sendJsonWsgiResponse(env,start_response,response)