Update libraries

Update for Python 3.8
Migrate from Flask-OAuthlib to Authlib
This commit is contained in:
RunasSudo 2020-07-11 17:59:05 +10:00
parent da24cc4f63
commit 2c15f443f8
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 20 additions and 23 deletions

View File

@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from flask_oauthlib.client import OAuth from authlib.integrations.flask_client import OAuth
import flask import flask
@ -27,51 +27,48 @@ blueprint = flask.Blueprint('eosweb.redditauth', __name__)
app = None app = None
oauth = None oauth = None
reddit = None
@blueprint.record @blueprint.record
def reddit_register(setup_state): def reddit_register(setup_state):
global app, oauth, reddit global app, oauth
app = setup_state.app app = setup_state.app
oauth = OAuth() oauth = OAuth(app)
reddit = oauth.remote_app('Reddit', oauth.register('reddit',
request_token_url=None, #request_token_url=None,
authorize_url='https://www.reddit.com/api/v1/authorize.compact', authorize_url='https://www.reddit.com/api/v1/authorize.compact',
request_token_params={'duration': 'temporary', 'scope': 'identity'}, authorize_params={'duration': 'temporary', 'scope': 'identity'},
access_token_url='https://www.reddit.com/api/v1/access_token', access_token_url='https://www.reddit.com/api/v1/access_token',
access_token_method='POST', access_token_method='POST',
access_token_headers={ access_token_headers={
'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(app.config['REDDIT_OAUTH_CLIENT_ID'], app.config['REDDIT_OAUTH_CLIENT_SECRET']).encode('ascii')).decode('ascii'), 'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(app.config['REDDIT_OAUTH_CLIENT_ID'], app.config['REDDIT_OAUTH_CLIENT_SECRET']).encode('ascii')).decode('ascii'),
'User-Agent': app.config['REDDIT_USER_AGENT'] 'User-Agent': app.config['REDDIT_USER_AGENT']
}, },
consumer_key=app.config['REDDIT_OAUTH_CLIENT_ID'], client_id=app.config['REDDIT_OAUTH_CLIENT_ID'],
consumer_secret=app.config['REDDIT_OAUTH_CLIENT_SECRET'] client_secret=app.config['REDDIT_OAUTH_CLIENT_SECRET'],
fetch_token=lambda: flask.session.get('user').oauth_token
) )
@reddit.tokengetter
def get_reddit_oauth_token():
return (flask.session.get('user').oauth_token, '')
@blueprint.route('/auth/reddit/login') @blueprint.route('/auth/reddit/login')
def reddit_login(): def reddit_login():
return reddit.authorize(callback=app.config['BASE_URI'] + flask.url_for('eosweb.redditauth.reddit_oauth_authorized'), state=uuid.uuid4()) return oauth.reddit.authorize_redirect(redirect_uri=app.config['BASE_URI'] + flask.url_for('eosweb.redditauth.reddit_oauth_authorized'), state=str(uuid.uuid4()))
@blueprint.route('/auth/reddit/oauth_callback') @blueprint.route('/auth/reddit/oauth_callback')
def reddit_oauth_authorized(): def reddit_oauth_authorized():
resp = reddit.authorized_response() try:
if resp is None: token = oauth.reddit.authorize_access_token()
except:
# Request denied # Request denied
return flask.redirect(flask.url_for('login_cancelled')) return flask.redirect(flask.url_for('login_cancelled'))
user = RedditUser() user = RedditUser()
user.oauth_token = resp['access_token'] user.oauth_token = token
flask.session['user'] = user flask.session['user'] = user
me = reddit.get('https://oauth.reddit.com/api/v1/me', headers={ me = oauth.reddit.get('https://oauth.reddit.com/api/v1/me', headers={
'User-Agent': app.config['REDDIT_USER_AGENT'] 'User-Agent': app.config['REDDIT_USER_AGENT']
}) })
user.username = me.data['name'] user.username = me.json()['name']
return flask.redirect(flask.url_for('login_complete')) return flask.redirect(flask.url_for('login_complete'))

View File

@ -1,16 +1,16 @@
Authlib==0.14.3
coverage==4.4.1 coverage==4.4.1
Flask==0.12.2 Flask==0.12.2
Flask-Mail==0.9.1 Flask-Mail==0.9.1
Flask-OAuthlib==0.9.4 flask-paginate==0.7.0
Flask-Session==0.3.1 Flask-Session==0.3.1
Flask-SQLAlchemy==2.3.2 Flask-SQLAlchemy==2.3.2
gunicorn==19.7.1 gunicorn==19.7.1
libsass==0.13.4 libsass==0.13.4
oauthlib==2.1.0
premailer==3.1.1 premailer==3.1.1
psycopg2==2.7.3.2 psycopg2==2.8.5
PyExecJS==1.4.1 PyExecJS==1.4.1
pymongo==3.5.1 pymongo[srv]==3.10.1
pyRCV==0.3 pyRCV==0.3
pytz==2017.3 pytz==2017.3
timeago==1.0.8 timeago==1.0.8