-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (138 loc) · 5.92 KB
/
main.py
File metadata and controls
183 lines (138 loc) · 5.92 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
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
# Copyright 2014 Tom SF Haines
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from bin.rfam import RFAM
from bin.response import Response
# Check if there is a secret file with a random string to be used for access...
try:
with open('secret.txt', 'r') as fin:
secret1, secret2 = fin.read().split('\n')
secret1 = secret1.strip() # Access token
secret2 = secret2.strip() # For encoding jwt
except FileNotFoundError:
secret1 = None
secret2 = None
if secret1 is not None:
import jwt
print('secure mode enabled')
# Create the RFAM object...
rfam = RFAM()
# Initialise the list of modules for handling requests...
from bin import javascript # Provides javascript files
from bin import stylesheets # Provides stylesheet files
from bin import logo # Provides the logo image!
from bin import icon # Provides the icon image!
from bin import head # Provides the image of a user.
from bin import heads # Provides the image of a team.
from bin import images # Provides access to the various images - the update status icons for ajax.
from bin import login # The login screen.
from bin import home # The home screen, which shows a summary of current users tasks.
from bin import assets # Asset list screen.
from bin import asset # Screen for editing a single asset.
from bin import new # Screen for creating a new asset.
from bin import team # Screen that shows the team and what they should be working on.
from bin import project # Screen that shows the project overview.
from bin import shots # Overview of shots and their state.
from bin import rendering # Web interface for rendering.
from bin import store # Handles all the ajax updates for all the interface elements.
from bin import add # Ajax code for adding assets, render jobs, rolls in the credits and external assets,
from bin import remove # Opposite to above.
from bin import action # Bunch of random ajax things, such as making a file checkpoint.
from bin import selector # Provides the file selector used on the rendering screen.
from bin import info # gets information about a file as part of above.
from bin import farm # The interface to the render farm used by render nodes.
from bin import potential # The interface used by the pre-node, for rendering on a cluster.
from bin import credits # Provides the automatically generated credit roll.
modules = dict()
modules['javascript'] = javascript.app
modules['stylesheets'] = stylesheets.app
modules['logo'] = logo.app
modules['icon'] = icon.app
modules['head'] = head.app
modules['heads'] = heads.app
modules['images'] = images.app
modules['login'] = login.app
modules['home'] = home.app
modules['assets'] = assets.app
modules['asset'] = asset.app
modules['new'] = new.app
modules['team'] = team.app
modules['project'] = project.app
modules['shots'] = shots.app
modules['rendering'] = rendering.app
modules['store'] = store.app
modules['add'] = add.app
modules['remove'] = remove.app
modules['action'] = action.app
modules['selector'] = selector.app
modules['info'] = info.app
modules['farm'] = farm.app
modules['potential'] = potential.app
modules['credits'] = credits.app
modules[''] = home.app
# The actual application that responds to everything...
def application(environ, start_response):
response = Response(environ)
cookie = response.getCookie()
# Handle security...
if secret1 is not None:
print('security check')
query = response.getQuery()
# If secret key provided set a jwt authorising access...
if 'access' in query:
code = query['access'].strip()
if code==secret1:
# Prepare to redirect...
response.refresh()
# Set access cookie...
payload = jwt.encode({'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24)}, secret2, algorithm='HS256')
response.addCookie('access', payload.decode('ascii'), 24)
# Lose the access token from the url with a redirect...
return response.response(start_response)
else:
response.make403()
return response.response(start_response)
elif 'access' in cookie:
# If secret enabled check for a jwt before providing access...
try:
payload = jwt.decode(cookie['access'], secret2, algorithms=['HS256'])
except (jwt.ExpiredSignatureError, jwt.exceptions.InvalidSignatureError):
response.make403()
return response.response(start_response)
else:
response.make403()
return response.response(start_response)
# Check if the user is logged in, and record this fact in the response...
response.project = None
response.project_name = '<<Unknown>>'
if 'project' in cookie:
project = rfam.getProject(cookie['project'])
if project!=None:
response.project = cookie['project']
response.project_name = project['name']
response.user = None
response.user_name = '<<Unknown>>'
if 'user' in cookie:
user = rfam.getUser(cookie['user'])
if user!=None:
response.user = cookie['user']
response.user_name = user['name']
# Process the url properly - start by extracting the path...
path = response.getPath()
base = path[0] if len(path)!=0 else ''
# Find the relevant module to provide the return data, or 404 if none found...
if base in modules:
modules[base](rfam, response)
else:
response.make404()
# Return the actual data...
return response.response(start_response)