Created
October 28, 2015 13:44
-
-
Save sinkovsky/c36b6c0295b6ebb5585a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint, session, url_for, jsonify, request, g | |
from app import app | |
from app import oauth, oauth_client | |
from app.database import connection | |
from app.blueprints.auth import require_login | |
sprava = Blueprint('sprava', __name__) | |
sprava_app = oauth_client.remote_app( | |
'sprava', | |
consumer_key='9qFbZD4udTzFVYo0u5UzkZX9iuzbdcJDRAquTfRk', | |
consumer_secret='foobar', | |
request_token_params={'scopes': 'all', 'state': 'auth'}, | |
base_url='http://sprava.local:4020/1/', | |
request_token_url=None, #We are using OAuth2, not 1.0a | |
access_token_method='POST', | |
access_token_url='http://sprava.local:4020/oauth/token', | |
authorize_url='http://sprava.local:4020/oauth/authorize' | |
) | |
@sprava.route('/login', methods=['GET','POST']) | |
@oauth.require_oauth() | |
# @require_login | |
def login(): | |
#session['user_id'] = request.oauth.user.user_id | |
#g.user = user | |
return sprava_app.authorize(callback=url_for('sprava.authorized', _external=True)) | |
@sprava.route('/login/authorized') | |
@require_login | |
def authorized(): | |
resp = sprava_app.authorized_response() | |
app.logger.debug(resp) | |
if resp is None: | |
return 'Access denied: reason=%s error=%s' % ( | |
request.args['error'], | |
request.args['error_description'] | |
) | |
session['sprava_token'] = (resp['access_token'], '') | |
app.logger.info('Access token: %s' % resp['access_token']) | |
us = connection.UserServices() | |
us['user_id'] = g.user.id | |
us['service'] = 'sprava' | |
us['access_token'] = resp['access_token'] | |
us['refresh_token'] = resp['refresh_token'] | |
us.save() | |
# destroying session | |
session['user_id'] = None | |
return jsonify(resp) | |
@sprava_app.tokengetter | |
def get_sprava_oauth_token(): | |
app.logger.debug(request.oauth.user.id) | |
us = connection.UserServices.find_one({'user_id': request.oauth.user.id, 'service': 'sprava'}) | |
app.logger.debug(us['access_token']) | |
return (us['access_token'], '') | |
@sprava.route('/profile') | |
@oauth.require_oauth() | |
def profile(): | |
app.logger.debug(get_sprava_oauth_token()) | |
return sprava_app.get('/profile').data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment