def maybe_add_slashes(request_path, GET, *args, **kwargs): ''' Redirect with trailing slashes if necessary. ''' # Look for a missing trailing slash at the repository root. split_req = request_path.lstrip('/').split('/', 2) if len(split_req) == 2 and split_req[-1] != '': # There are two full components in the path: owner and repo, req_owner, req_repo = split_req if repo_exists(req_owner, req_repo, GET): # Missing a trailing slash for the branch listing. return redirect( absolute_url(request, '{}/'.format(request_path)), 302) if len(split_req) == 3 and split_req[-1] != '': # There are three full components in the path: owner, repo, and ref. req_owner, req_repo, req_ref_path = split_req req_ref, req_path = split_branch_path(req_owner, req_repo, req_ref_path, GET) if req_path == '' and not req_ref_path.endswith('/'): # Missing a trailing slash at the root of the repository. return redirect( absolute_url(request, '{}/'.format(request_path)), 302) return untouched_route(*args, **kwargs)
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(absolute_url(request, state['redirect']), 302) other.headers['Cache-Control'] = 'no-store private' other.headers['Vary'] = 'Referer' return other
def logout(): ''' ''' if 'id' in session: session.pop('id') if 'token' in session: session.pop('token') return redirect(absolute_url(request, '/'), 302)
def make_redirect(slash_count): ''' Return a flask.redirect for the current flask.request. ''' referer_url = request.headers.get('Referer') request_part = request.path + '?{}'.format(request.query_string).rstrip('?') location = get_redirect(request_part, referer_url, slash_count) other = redirect(absolute_url(request, location), 302) other.headers['Cache-Control'] = 'no-store private' other.headers['Vary'] = 'Referer' return other
def maybe_add_slashes(request_path, GET, *args, **kwargs): ''' Redirect with trailing slashes if necessary. ''' # Look for a missing trailing slash at the repository root. split_req = request_path.lstrip('/').split('/', 2) if len(split_req) == 2 and split_req[-1] != '': # There are two full components in the path: owner and repo, req_owner, req_repo = split_req if repo_exists(req_owner, req_repo, GET): # Missing a trailing slash for the branch listing. return redirect(absolute_url(request, '{}/'.format(request_path)), 302) if len(split_req) == 3 and split_req[-1] != '': # There are three full components in the path: owner, repo, and ref. req_owner, req_repo, req_ref_path = split_req req_ref, req_path = split_branch_path(req_owner, req_repo, req_ref_path, GET) if req_path == '' and not req_ref_path.endswith('/'): # Missing a trailing slash at the root of the repository. return redirect(absolute_url(request, '{}/'.format(request_path)), 302) return untouched_route(*args, **kwargs)
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(absolute_url(request, state['redirect']), 302) other.headers['Cache-Control'] = 'no-store private' other.headers['Vary'] = 'Referer' return other