-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmake-it-so.py
331 lines (258 loc) · 9.81 KB
/
make-it-so.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
from logging import DEBUG, INFO, getLogger, FileHandler, StreamHandler, Formatter
from os.path import join, isdir, isfile
from traceback import format_exc
from urllib import urlencode
from os import environ
from time import time
from flask import Flask, redirect, request, make_response, render_template, session
from requests import post
from requests_oauthlib import OAuth2Session
from git import prepare_git_checkout, PrivateRepoException
from git import MissingRepoException, MissingRefException
from href import needs_redirect, get_redirect
from util import get_directory_response
from util import get_file_response
from util import errors_logged
from jekyll import jekyll_build
from git import github_client_id, github_client_secret
flask_secret_key = 'poop'
app = Flask(__name__)
app.secret_key = flask_secret_key
@app.before_first_request
def adjust_log_level():
getLogger('jekit').setLevel(DEBUG if app.debug else INFO)
def should_redirect():
''' Return True if the current flask.request should redirect.
'''
if request.args.get('go') == u'\U0001f44c':
return False
referer_url = request.headers.get('Referer')
if not referer_url:
return False
return needs_redirect(request.host, request.path, referer_url)
def make_redirect():
''' Return a flask.redirect for the current flask.request.
'''
referer_url = request.headers.get('Referer')
other = redirect(get_redirect(request.path, referer_url), 302)
other.headers['Cache-Control'] = 'no-store private'
other.headers['Vary'] = 'Referer'
return other
def get_auth():
''' Get (username, password) tuple from flask.request, or None.
'''
auth = request.authorization
return (auth.username, auth.password) if auth else None
def get_token():
''' Get OAuth token from flask.session, or a fake one guaranteed to fail.
'''
token = dict(token_type='bearer', access_token='<fake token, will fail>')
token.update(session.get('token', {}))
return token
def make_401_response():
''' Create an HTTP 401 Not Authorized response to trigger Github OAuth.
Start by redirecting the user to Github OAuth authorization page:
http://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
'''
state_id = 'foobar' # fake.
states = session.get('states', {})
states[state_id] = dict(redirect=request.url, created=time())
session['states'] = states
data = dict(scope='user,repo', client_id=github_client_id, state=state_id)
auth = redirect('https://github.com/login/oauth/authorize?' + urlencode(data), 302)
auth.headers['Cache-Control'] = 'no-store private'
auth.headers['Vary'] = 'Referer'
return auth
def make_404_response(template, vars):
'''
'''
return make_response(render_template(template, **vars), 404)
def make_500_response(error, traceback):
'''
'''
try:
message = unicode(error)
except UnicodeDecodeError:
message = str(error).decode('latin-1')
vars = dict(error=message, traceback=traceback)
return make_response(render_template('error-runtime.html', **vars), 500)
@app.route('/')
@errors_logged
def hello_world():
if should_redirect():
return make_redirect()
id = session.get('id', None)
script = '''
javascript:(
function ()
{
document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src='http://host:port/bookmarklet.js';
}()
);
'''
script = script.replace('http', request.scheme)
script = script.replace('host:port', request.host)
script = script.replace(' ', '').replace('\n', '')
return render_template('index.html', id=id, script=script, request=request)
@app.route('/.well-known/status')
@errors_logged
def wellknown_status():
if should_redirect():
return make_redirect()
status = '''
{
"status": "ok",
"updated": %d,
"dependencies": [ ],
"resources": { }
}
''' % time()
resp = make_response(status, 200)
resp.headers['Content-Type'] = 'application/json'
return resp
@app.route('/bookmarklet.js')
@errors_logged
def bookmarklet_script():
if should_redirect():
return make_redirect()
js = open('scripts/bookmarklet.js').read()
script = make_response(js.replace('host:port', request.host), 200)
script.headers['Content-Type'] = 'text/javascript'
script.headers['Cache-Control'] = 'no-store private'
return script
@app.route('/oauth/callback')
@errors_logged
def get_oauth_callback():
''' Handle Github's OAuth callback after a user authorizes.
http://developer.github.com/v3/oauth/#github-redirects-back-to-your-site
'''
if 'error' in request.args:
return render_template('error-oauth.html', reason="you didn't authorize access to your account.")
try:
code, state_id = request.args['code'], request.args['state']
except:
return render_template('error-oauth.html', reason='missing code or state in callback.')
try:
state = session['states'].pop(state_id)
except:
return render_template('error-oauth.html', reason='state "%s" not found?' % state_id)
#
# Exchange the temporary code for an access token:
# http://developer.github.com/v3/oauth/#parameters-1
#
data = dict(client_id=github_client_id, code=code, client_secret=github_client_secret)
resp = post('https://github.com/login/oauth/access_token', urlencode(data),
headers={'Accept': 'application/json'})
auth = resp.json()
if 'error' in auth:
return render_template('error-oauth.html', reason='Github said "%(error)s".' % auth)
elif 'access_token' not in auth:
return render_template('error-oauth.html', reason="missing `access_token`.")
session['token'] = auth
#
# Figure out who's here.
#
url = 'https://api.github.com/user'
id = OAuth2Session(github_client_id, token=session['token']).get(url).json()
id = dict(login=id['login'], avatar_url=id['avatar_url'], html_url=id['html_url'])
session['id'] = id
other = redirect(state['redirect'], 302)
other.headers['Cache-Control'] = 'no-store private'
other.headers['Vary'] = 'Referer'
return other
@app.route('/logout', methods=['POST'])
@errors_logged
def logout():
'''
'''
if 'id' in session:
session.pop('id')
if 'token' in session:
session.pop('token')
return redirect('/', 302)
@app.route('/<account>/<repo>')
@errors_logged
def repo_only(account, repo):
''' Redirect to "master" on a hunch.
'''
if should_redirect():
return make_redirect()
return redirect('/%s/%s/master/' % (account, repo), 302)
@app.route('/<account>/<repo>/')
@errors_logged
def repo_only_slash(account, repo):
''' Redirect to "master" on a hunch.
'''
if should_redirect():
return make_redirect()
return redirect('/%s/%s/master/' % (account, repo), 302)
@app.route('/<account>/<repo>/<ref>')
@errors_logged
def repo_ref(account, repo, ref):
''' Redirect to add trailing slash.
'''
if should_redirect():
return make_redirect()
return redirect('/%s/%s/%s/' % (account, repo, ref), 302)
@app.route('/<account>/<repo>/<ref>/')
@errors_logged
def repo_ref_slash(account, repo, ref):
''' Show repository root directory listing.
'''
if should_redirect():
return make_redirect()
try:
site_path = jekyll_build(prepare_git_checkout(account, repo, ref, token=get_token()))
except MissingRepoException:
return make_404_response('no-such-repo.html', dict(account=account, repo=repo))
except MissingRefException:
return make_404_response('no-such-ref.html', dict(account=account, repo=repo, ref=ref))
except PrivateRepoException:
return make_401_response()
except RuntimeError, e:
return make_500_response(e, format_exc())
return get_directory_response(site_path)
@app.route('/<account>/<repo>/<ref>/<path:path>')
@errors_logged
def repo_ref_path(account, repo, ref, path):
''' Show response for a path, whether a file or directory.
'''
if should_redirect():
return make_redirect()
try:
site_path = jekyll_build(prepare_git_checkout(account, repo, ref, token=get_token()))
except MissingRepoException:
return make_404_response('no-such-repo.html', dict(account=account, repo=repo))
except MissingRefException:
return make_404_response('no-such-ref.html', dict(account=account, repo=repo, ref=ref))
except PrivateRepoException:
return make_401_response()
except RuntimeError, e:
return make_500_response(e, format_exc())
local_path = join(site_path, path)
if isfile(local_path) and not isdir(local_path):
return get_file_response(local_path)
if isdir(local_path) and not path.endswith('/'):
other = redirect('/%s/%s/%s/%s/' % (account, repo, ref, path), 302)
other.headers['Cache-Control'] = 'no-store private'
return other
if isdir(local_path):
return get_directory_response(local_path)
kwargs = dict(account=account, repo=repo, ref=ref, path=path)
return make_404_response('error-404.html', kwargs)
@app.route('/<path:path>')
@errors_logged
def all_other_paths(path):
'''
'''
if should_redirect():
return make_redirect()
if environ.get('app-logfile', None):
handler = FileHandler(environ['app-logfile'])
handler.setFormatter(Formatter('%(process)05s %(asctime)s %(levelname)06s: %(message)s'))
else:
handler = StreamHandler()
handler.setFormatter(Formatter('%(process)05s %(levelname)06s: %(message)s'))
getLogger('jekit').addHandler(handler)
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)