Repository navigation
Expand file tree
/
Copy pathapi_app.py
More file actions
112 lines (80 loc) · 3.49 KB
/
Copy pathapi_app.py
File metadata and controls
112 lines (80 loc) · 3.49 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib import unquote
from itertools import chain
from traceback import format_exc, print_exc
from bottle import route, request, response, HTTPError
from utils import toDict, set_session
from webinterface import PYLOAD
from module.common.json_layer import json
from module.lib.SafeEval import const_eval as literal_eval
from module.Api import BaseObject
# json encoder that accepts TBase objects
class TBaseEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, BaseObject):
return toDict(o)
return json.JSONEncoder.default(self, o)
# accepting positional arguments, as well as kwargs via post and get
@route("/api/:func:args#[a-zA-Z0-9\-_/\"'\[\]%{}]*#")
@route("/api/:func:args#[a-zA-Z0-9\-_/\"'\[\]%{}]*#", method="POST")
def call_api(func, args=""):
response.headers.replace("Content-type", "application/json")
response.headers.append("Cache-Control", "no-cache, must-revalidate")
if 'u' in request.POST and 'p' in request.POST:
info = PYLOAD.checkAuth(request.POST['u'], request.POST['p'])
if info:
if not PYLOAD.isAuthorized(func, {"role": info["role"], "permission": info["permission"]}):
return HTTPError(401, json.dumps("Unauthorized"))
else:
return HTTPError(403, json.dumps("Forbidden"))
else:
s = request.environ.get('beaker.session')
if 'session' in request.POST:
s = s.get_by_id(request.POST['session'])
if not s or not s.get("authenticated", False):
return HTTPError(403, json.dumps("Forbidden"))
if not PYLOAD.isAuthorized(func, {"role": s["role"], "permission": s["perms"]}):
return HTTPError(401, json.dumps("Unauthorized"))
args = args.split("/")[1:]
kwargs = {}
for x, y in chain(request.GET.iteritems(), request.POST.iteritems()):
if x in ("u", "p", "session"): continue
kwargs[x] = unquote(y)
try:
return callApi(func, *args, **kwargs)
except Exception, e:
print_exc()
return HTTPError(500, json.dumps({"error": e.message, "traceback": format_exc()}))
def callApi(func, *args, **kwargs):
if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"):
print "Invalid API call", func
return HTTPError(404, json.dumps("Not Found"))
result = getattr(PYLOAD, func)(*[literal_eval(x) for x in args],
**dict([(x, literal_eval(y)) for x, y in kwargs.iteritems()]))
# null is invalid json response
if result is None: result = True
return json.dumps(result, cls=TBaseEncoder)
#post -> username, password
@route("/api/login", method="POST")
def login():
response.headers.replace("Content-type", "application/json")
response.headers.append("Cache-Control", "no-cache, must-revalidate")
user = request.forms.get("username")
password = request.forms.get("password")
info = PYLOAD.checkAuth(user, password)
if not info:
return json.dumps(False)
s = set_session(request, info)
# get the session id by dirty way, documentations seems wrong
try:
sid = s._headers["cookie_out"].split("=")[1].split(";")[0]
return json.dumps(sid)
except:
return json.dumps(True)
@route("/api/logout")
def logout():
response.headers.replace("Content-type", "application/json")
response.headers.append("Cache-Control", "no-cache, must-revalidate")
s = request.environ.get('beaker.session')
s.delete()