def done(request): """List the gists""" user = request.user social_auth = UserSocialAuth.objects.get(user=user) tokens = social_auth.tokens github = login(token=tokens['access_token']) gists = [g for g in github.iter_gists()] return render_to_response('done.html', RequestContext(request))
def app_from_config_file(filename): """Loads the app from the YAML-encoded config file, and updates the config file if needed. :param filename: the filename of the config to load :type filename: :class:`basestring` :returns: the loaded app :rtype: :class:`~asuka.app.App` """ dirname = os.path.dirname(filename) with open(filename) as fp: loaded_config = load(fp) config = dict(loaded_config) config['ec2_connection'] = connect_to_region(**config['ec2_connection']) try: private_key = config['private_key'] except KeyError: pass else: private_key = RSAKey.from_private_key_file( os.path.join(dirname, private_key)) config['private_key'] = private_key gh_auth = None try: gh_token = config['repository']['token'] gh_repository = config['repository']['repository'] except KeyError: try: gh_login = config['repository']['login'] gh_password = config['repository']['password'] gh_repository = config['repository']['repository'] except KeyError: gh_token = None else: gh_auth = authorize(gh_login, gh_password, ['repo'], 'Asuka Deployment System') gh_token = str(gh_auth.token) if gh_token: gh = login(token=gh_token) config['repository'] = gh.repository(*gh_repository.split('/', 1)) app, delta = app_from_config(config) if gh_auth: delta['repository'] = {'token': gh_token, 'repository': gh_repository} if delta: try: private_key = delta['private_key'] except KeyError: pass else: key_filename = app.name + '_id_rsa' private_key.write_private_key_file( os.path.join(dirname, key_filename)) delta['private_key'] = key_filename loaded_config.update(delta) with open(filename, 'w') as fp: dump(loaded_config, fp, default_flow_style=False) return app
def authorize(request): """Authorizes the user using GitHub OAuth 2.""" back = request.args.get('back', request.build_url('home', _external=True)) if request.session.get('github_login'): return redirect(back) app = request.app.app code = request.args.get('code') if not code: # Step 1 state = '{0:040x}'.format(random.randrange(256 ** 20)) request.session.update(state=state, back=back) params = url_encode({ 'client_id': app.github_client_id, 'redirect_uri': request.build_url('authorize', _external=True), 'scope': 'user,repo,repo:status', 'state': state }) return redirect('https://github.com/login/oauth/authorize?' + params) # Step 2 if request.args['state'] != request.session['state']: raise BadRequest() with session() as client: response = client.get( 'https://github.com/login/oauth/access_token', params={ 'client_id': app.github_client_id, 'client_secret': app.github_client_secret, 'redirect_uri': request.build_url('authorize', _external=True), 'code': code, 'state': request.session['state'] }, headers={'Accept': 'application/json'} ) token = response.json()['access_token'] gh = login(token=token) if not app.repository.is_collaborator(gh.user().login): raise Forbidden() request.session['github_login'] = gh.user().login del request.session['state'] back = request.session.pop('back') return redirect(back)
def __setstate__(self, state): token, owner, repo = state.pop('repository') github = login(token=token) state['repository'] = github.repository(owner, repo) self.__init__(**state)
def app_from_config_file(filename): """Loads the app from the YAML-encoded config file, and updates the config file if needed. :param filename: the filename of the config to load :type filename: :class:`basestring` :returns: the loaded app :rtype: :class:`~asuka.app.App` """ dirname = os.path.dirname(filename) with open(filename) as fp: loaded_config = load(fp) config = dict(loaded_config) config['ec2_connection'] = connect_to_region(**config['ec2_connection']) try: private_key = config['private_key'] except KeyError: pass else: private_key = RSAKey.from_private_key_file( os.path.join(dirname, private_key) ) config['private_key'] = private_key gh_auth = None try: gh_token = config['repository']['token'] gh_repository = config['repository']['repository'] except KeyError: try: gh_login = config['repository']['login'] gh_password = config['repository']['password'] gh_repository = config['repository']['repository'] except KeyError: gh_token = None else: gh_auth = authorize(gh_login, gh_password, ['repo'], 'Asuka Deployment System') gh_token = str(gh_auth.token) if gh_token: gh = login(token=gh_token) config['repository'] = gh.repository(*gh_repository.split('/', 1)) app, delta = app_from_config(config) if gh_auth: delta['repository'] = { 'token': gh_token, 'repository': gh_repository } if delta: try: private_key = delta['private_key'] except KeyError: pass else: key_filename = app.name + '_id_rsa' private_key.write_private_key_file( os.path.join(dirname, key_filename) ) delta['private_key'] = key_filename loaded_config.update(delta) with open(filename, 'w') as fp: dump(loaded_config, fp, default_flow_style=False) return app
def __setstate__(self, state): token, owner, repo = state.pop("repository") github = login(token=token) state["repository"] = github.repository(owner, repo) self.__init__(**state)