use requests session

This commit is contained in:
Johann150 2021-11-10 07:51:38 +01:00
parent ba72b8acf9
commit dbd7193636
No known key found for this signature in database
GPG Key ID: 9EE6577A2A06F8F1
2 changed files with 27 additions and 19 deletions

View File

@ -1,22 +1,22 @@
from app import db, sentry from app import db, sentry
from model import MisskeyApp, MisskeyInstance, Account, OAuthToken from model import MisskeyApp, MisskeyInstance, Account, OAuthToken
from requests import get, post
from uuid import uuid4 from uuid import uuid4
from hashlib import sha256 from hashlib import sha256
from libforget.exceptions import TemporaryError, PermanentError from libforget.exceptions import TemporaryError, PermanentError
from libforget.session import make_session
def get_or_create_app(instance_url, callback, website): def get_or_create_app(instance_url, callback, website, session):
instance_url = instance_url instance_url = instance_url
app = MisskeyApp.query.get(instance_url) app = MisskeyApp.query.get(instance_url)
if not app: if not app:
# check if the instance uses https while getting instance infos # check if the instance uses https while getting instance infos
try: try:
r = post('https://{}/api/meta'.format(instance_url)) r = session.post('https://{}/api/meta'.format(instance_url))
r.raise_for_status() r.raise_for_status()
proto = 'https' proto = 'https'
except Exception: except Exception:
r = post('http://{}/api/meta'.format(instance_url)) r = session.post('http://{}/api/meta'.format(instance_url))
r.raise_for_status() r.raise_for_status()
proto = 'http' proto = 'http'
@ -33,7 +33,7 @@ def get_or_create_app(instance_url, callback, website):
app.client_secret = None app.client_secret = None
else: else:
# register the app # register the app
r = post('{}://{}/api/app/create'.format(app.protocol, app.instance), json = { r = session.post('{}://{}/api/app/create'.format(app.protocol, app.instance), json = {
'name': 'forget', 'name': 'forget',
'description': website, 'description': website,
'permission': ['read:favorites', 'write:notes'], 'permission': ['read:favorites', 'write:notes'],
@ -44,12 +44,12 @@ def get_or_create_app(instance_url, callback, website):
return app return app
def login_url(app, callback): def login_url(app, callback, session):
if app.miauth: if app.miauth:
return "{}://{}/miauth/{}?name=forget&callback={}&permission=read:favorites,write:notes".format(app.protocol, app.instance, uuid4(), callback) return "{}://{}/miauth/{}?name=forget&callback={}&permission=read:favorites,write:notes".format(app.protocol, app.instance, uuid4(), callback)
else: else:
# will use the callback we gave the server in `get_or_create_app` # will use the callback we gave the server in `get_or_create_app`
r = post('{}://{}/api/auth/session/generate'.format(app.protocol, app.instance), json = { r = session.post('{}://{}/api/auth/session/generate'.format(app.protocol, app.instance), json = {
'appSecret': app.client_secret 'appSecret': app.client_secret
}) })
r.raise_for_status() r.raise_for_status()
@ -58,8 +58,10 @@ def login_url(app, callback):
return r.json()['url'] return r.json()['url']
def receive_token(token, app): def receive_token(token, app):
session = make_session()
if app.miauth: if app.miauth:
r = get('{}://{}/api/miauth/{}/check'.format(app.protocol, app.instance, token)) r = session.get('{}://{}/api/miauth/{}/check'.format(app.protocol, app.instance, token))
r.raise_for_status() r.raise_for_status()
token = r.json()['token'] token = r.json()['token']
@ -70,7 +72,7 @@ def receive_token(token, app):
token = db.session.merge(token) token = db.session.merge(token)
token.account = acc token.account = acc
else: else:
r = post('{}://{}/api/auth/session/userkey'.format(app.protocol, app.instance), json = { r = session.post('{}://{}/api/auth/session/userkey'.format(app.protocol, app.instance), json = {
'appSecret': app.client_secret, 'appSecret': app.client_secret,
'token': token 'token': token
}) })
@ -86,9 +88,9 @@ def receive_token(token, app):
return token return token
def check_auth(account, app): def check_auth(account, app, session):
if app.miauth: if app.miauth:
r = get('{}://{}/api/miauth/{}/check'.format(app.protocol, app.instance, account.token)) r = session.get('{}://{}/api/miauth/{}/check'.format(app.protocol, app.instance, account.token))
if r.status_code != 200: if r.status_code != 200:
raise TemporaryError("{} {}".format(r.status_code, r.body)) raise TemporaryError("{} {}".format(r.status_code, r.body))
@ -104,7 +106,7 @@ def check_auth(account, app):
else: else:
# there is no such check for legacy auth, instead we check if we can # there is no such check for legacy auth, instead we check if we can
# get the user info # get the user info
r = post('{}://{}/api/i'.format(app.protocol, app.instance), json = {'i': account.token}) r = session.post('{}://{}/api/i'.format(app.protocol, app.instance), json = {'i': account.token})
if r.status_code != 200: if r.status_code != 200:
raise TemporaryError("{} {}".format(r.status_code, r.body)) raise TemporaryError("{} {}".format(r.status_code, r.body))
@ -139,7 +141,8 @@ def post_from_api_object(obj):
def fetch_posts(acc, max_id, since_id): def fetch_posts(acc, max_id, since_id):
app = MisskeyApp.query.get(acc.misskey_instance) app = MisskeyApp.query.get(acc.misskey_instance)
check_auth(acc, app) session = make_session()
check_auth(acc, app, session)
if not verify_credentials(acc, app): if not verify_credentials(acc, app):
raise PermanentError() raise PermanentError()
try: try:
@ -149,7 +152,7 @@ def fetch_posts(acc, max_id, since_id):
if since_id: if since_id:
kwargs['sinceId'] = since_id kwargs['sinceId'] = since_id
notes = post('{}://{}/api/users/notes'.format(app.protocol, app.misskey_instance), json=kwargs) notes = session.post('{}://{}/api/users/notes'.format(app.protocol, app.misskey_instance), json=kwargs)
notes.raise_for_status() notes.raise_for_status()
return [post_from_api_object(status) for note in notes.json()] return [post_from_api_object(status) for note in notes.json()]
@ -161,13 +164,14 @@ def fetch_posts(acc, max_id, since_id):
def refresh_posts(posts): def refresh_posts(posts):
acc = posts[0].author acc = posts[0].author
app = MisskeyApp.query.get(acc.misskey_instance) app = MisskeyApp.query.get(acc.misskey_instance)
check_auth(acc, app) session = make_session()
check_auth(acc, app, session)
new_posts = list() new_posts = list()
with db.session.no_autoflush: with db.session.no_autoflush:
for post in posts: for post in posts:
print('Refreshing {}'.format(post)) print('Refreshing {}'.format(post))
r = post('{}://{}/api/notes/show'.format(app.protocol, app.misskey_instance), json={ r = session.post('{}://{}/api/notes/show'.format(app.protocol, app.misskey_instance), json={
'noteId': post.misskey_id 'noteId': post.misskey_id
}) })
if r.status_code != 200: if r.status_code != 200:
@ -186,12 +190,13 @@ def refresh_posts(posts):
def delete(post): def delete(post):
app = MisskeyApp.query.get(post.misskey_instance) app = MisskeyApp.query.get(post.misskey_instance)
session = make_session()
if not app: if not app:
# how? if this happens, it doesnt make sense to repeat it, # how? if this happens, it doesnt make sense to repeat it,
# so use a permanent error # so use a permanent error
raise PermanentError("instance not registered for delete") raise PermanentError("instance not registered for delete")
r = post('{}://{}/api/notes/delete'.format(app.protocol, app.misskey_instance), json = { r = session.post('{}://{}/api/notes/delete'.format(app.protocol, app.misskey_instance), json = {
'noteId': post.misskey_id 'noteId': post.misskey_id
}) })

View File

@ -6,6 +6,7 @@ import libforget.mastodon
import libforget.misskey import libforget.misskey
from libforget.auth import require_auth, csrf,\ from libforget.auth import require_auth, csrf,\
get_viewer get_viewer
from libforget.session import make_session
from model import Session, TwitterArchive, MastodonApp from model import Session, TwitterArchive, MastodonApp
from app import app, db, sentry, imgproxy from app import app, db, sentry, imgproxy
import tasks import tasks
@ -260,15 +261,17 @@ def misskey_login(instance=None):
instance_url=instance_url, _external=True) instance_url=instance_url, _external=True)
try: try:
session = make_session()
app = libforget.misskey.get_or_create_app( app = libforget.misskey.get_or_create_app(
instance_url, instance_url,
callback_legacy, callback_legacy,
url_for('index', _external=True)) url_for('index', _external=True),
session)
db.session.merge(app) db.session.merge(app)
db.session.commit() db.session.commit()
return redirect(libforget.misskey.login_url(app, callback)) return redirect(libforget.misskey.login_url(app, callback, session))
except Exception: except Exception:
if sentry: if sentry: