MarseyWorld/files/routes/oauth.py

327 lines
8.9 KiB
Python
Raw Normal View History

[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
import sqlalchemy.exc
from files.classes import *
2022-05-04 23:09:46 +00:00
from files.helpers.alerts import *
from files.helpers.config.const import *
[DO NOT MERGE] import detanglation (#442) * move Base definition to files.classes.__init__.py * fix ImportError * move userpage listing to users.py * don't import the app from classes * consts: set default values to avoid crashes consts: warn if the secret key is the default config value * card view: sneed (user db schema) * cloudflare: use DEFAULT_CONFIG_VALUE * const: set default values * decouple media.py from __main__ * pass database to avoid imports * import cleanup and import request not in const, but in the requests mega import * move asset_submissions site check to __init__ * asset submissions feature flag * flag * g.is_tor * don't import request where it's not needed * i think this is fine * mail: move to own routes and helper * wrappers * required wrappers move * unfuck wrappers a bit * move snappy quotes and marseys to stateful consts * marsify * :pepodrool: * fix missing import * import cache * ...and settings.py * and static.py * static needs cache * route * lmao all of the jinja shit was in feeds.py amazing * classes should only import what they need from flask * import Response * hdjbjdhbhjf * ... * dfdfdfdf * make get a non-required import * isort imports (mostly) * but actually * configs * reload config on import * fgfgfgfg * config * config * initialize snappy and test * cookie of doom debug * edfjnkf * xikscdfd * debug config * set session cookie domain, i think this fixes the can't login bug * sdfbgnhvfdsghbnjfbdvvfghnn * hrsfxgf * dump the entire config on a request * kyskyskyskyskyskyskyskyskys * duifhdskfjdfd * dfdfdfdfdfdfdfdfdfdfdfdf * dfdfdfdf * imoprt all of the consts beacuse fuck it * 😭 * dfdfdfdfdfdfsdasdf * print the entire session * rffdfdfjkfksj * fgbhffh * not the secret keys * minor bug fixes * be helpful in the warning * gfgfgfg * move warning lower * isort main imports (i hope this doesn't fuck something up) * test * session cookie domain redux * dfdfdfd * try only importing Flask * formkeys fix * y * :pepodrool: * route helper * remove before flight * dfdfdfdfdf * isort classes * isort helpers * move check_for_alts to routehelpers and also sort imports and get rid of unused ones * that previous commit but actkally * readd the cache in a dozen places they were implicitly imported * use g.is_tor instead of request.headers. bla bla bla * upgrade streamers to their own route file * get rid of unused imports in __main__ * fgfgf * don't pull in the entire ORM where we don't need it * features * explicit imports for the get helper * explicit imports for the get helper redux * testing allroutes * remove unused import * decouple flask from classes * syntax fix also remember these have side fx for some reason (why?) * move side effects out of the class * posts * testing on devrama * settings * reloading * settingssdsdsds * streamer features * site settings * testing settings on devrama * import * fix modlog * remove debug stuff * revert commit 67275b21ab6e2f2520819e84d10bfc1c746a15b6 * archiveorg to _archiveorg * skhudkfkjfd * fix cron for PCM * fix bugs that snekky wants me to * Fix call to realbody passing db, standardize kwarg * test * import check_for_alts from the right place * cloudflare * testing on devrama * fix cron i think * shadow properly * tasks * Remove print which will surely be annoying in prod. * v and create new session * use files.classes * make errors import little and fix rare 500 in /allow_nsfw * Revert "use files.classes" This reverts commit 98c10b876cf86ce058b7fb955cf1ec0bfb9996c6. * pass v to media functions rather than using g * fix * dfdfdfdfd * cleanup, py type checking is dumb so don't use it where it causes issues * Fix some merge bugs, add DEFAULT_RATELIMIT to main. * Fix imports on sqlalchemy expressions. * `from random import random` is an error. * Fix replies db param. * errors: fix missing import * fix rare 500: only send to GIFT_NOTIF_ID if it exists, and send them the right text * Fix signup formkey. * fix 2 500s * propagate db to submissions * fix replies * dfdfdfdf * Fix verifiedcolor. * is_manual * can't use getters outside of an app context * don't attempt to do gumroad on sites where it's not enabled * don't attempt to do gumraod on sites's where it's unnecessary * Revert "don't attempt to do gumroad on sites where it's not enabled" This reverts commit 6f8a6331878655492dfaf1907b27f8be513c14d3. * fix 500 * validate media type Co-authored-by: TLSM <duolsm@outlook.com>
2022-11-15 09:19:08 +00:00
from files.helpers.get import *
from files.routes.wrappers import *
2022-05-04 23:09:46 +00:00
from files.__main__ import app, limiter
@app.get("/authorize")
@limiter.limit(DEFAULT_RATELIMIT)
2023-01-21 04:39:46 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@auth_required
2022-11-26 21:00:03 +00:00
def authorize_prompt(v:User):
2022-05-04 23:09:46 +00:00
client_id = request.values.get("client_id")
2023-03-16 06:27:58 +00:00
application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none()
2022-05-04 23:09:46 +00:00
if not application: return {"oauth_error": "Invalid `client_id`"}, 401
return render_template("oauth.html", v=v, application=application)
@app.post("/authorize")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@auth_required
def authorize(v):
client_id = request.values.get("client_id")
2023-03-16 06:27:58 +00:00
application = g.db.query(OauthApp).filter_by(client_id=client_id).one_or_none()
2022-05-04 23:09:46 +00:00
if not application: return {"oauth_error": "Invalid `client_id`"}, 401
access_token = secrets.token_urlsafe(128)[:128]
try:
new_auth = ClientAuth(oauth_client = application.id, user_id = v.id, access_token=access_token)
2023-03-16 06:27:58 +00:00
g.db.add(new_auth)
2022-05-04 23:09:46 +00:00
except sqlalchemy.exc.IntegrityError:
2023-03-16 06:27:58 +00:00
g.db.rollback()
old_auth = g.db.query(ClientAuth).filter_by(oauth_client = application.id, user_id = v.id).one()
2022-05-04 23:09:46 +00:00
access_token = old_auth.access_token
return redirect(f"{application.redirect_uri}?token={access_token}")
@app.post("/rescind/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-07-15 00:12:54 +00:00
@auth_required
def rescind(v, aid):
2023-03-16 06:27:58 +00:00
auth = g.db.query(ClientAuth).filter_by(oauth_client = aid, user_id = v.id).one_or_none()
2022-07-15 00:12:54 +00:00
if not auth: abort(400)
2023-03-16 06:27:58 +00:00
g.db.delete(auth)
2022-07-15 00:12:54 +00:00
return {"message": "Authorization revoked!"}
2022-05-04 23:09:46 +00:00
@app.post("/api_keys")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@is_not_permabanned
def request_api_keys(v):
new_app = OauthApp(
app_name=request.values.get('name').replace('<','').replace('>',''),
redirect_uri=request.values.get('redirect_uri'),
author_id=v.id,
description=request.values.get("description")[:256]
)
2023-03-16 06:27:58 +00:00
g.db.add(new_app)
2022-05-04 23:09:46 +00:00
body = f"@{v.username} has requested API keys for `{request.values.get('name')}`. You can approve or deny the request [here](/admin/apps)."
2023-02-07 03:31:49 +00:00
body_html = sanitize(body, blackjack="app description")
2022-05-04 23:09:46 +00:00
new_comment = Comment(author_id=AUTOJANNY_ID,
2022-09-04 23:15:37 +00:00
parent_submission=None,
level=1,
body_html=body_html,
sentto=MODMAIL_ID,
2022-09-04 23:15:37 +00:00
distinguish_level=6,
is_bot=True
)
2023-03-16 06:27:58 +00:00
g.db.add(new_comment)
g.db.flush()
2022-05-04 23:09:46 +00:00
new_comment.top_comment_id = new_comment.id
2023-03-16 06:27:58 +00:00
admin_ids = [x[0] for x in g.db.query(User.id).filter(User.admin_level >= PERMS['APPS_MODERATION']).all()]
2022-05-04 23:09:46 +00:00
2023-02-24 01:58:52 +00:00
for admin_id in admin_ids:
notif = Notification(comment_id=new_comment.id, user_id=admin_id)
2023-03-16 06:27:58 +00:00
g.db.add(notif)
2023-05-05 21:45:25 +00:00
2023-02-24 07:16:50 +00:00
push_notif(admin_ids, 'New notification', body, f'{SITE_FULL}/comment/{new_comment.id}?read=true#context')
2022-05-04 23:09:46 +00:00
return redirect('/settings/apps')
@app.post("/delete_app/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@auth_required
def delete_oauth_app(v, aid):
2022-10-16 09:51:42 +00:00
try:
aid = int(aid)
except:
abort(404)
2023-03-16 06:27:58 +00:00
app = g.db.get(OauthApp, aid)
if not app: abort(404)
2023-01-01 11:36:20 +00:00
2022-05-04 23:09:46 +00:00
if app.author_id != v.id: abort(403)
2023-03-16 06:27:58 +00:00
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all():
g.db.delete(auth)
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
g.db.delete(app)
2022-05-04 23:09:46 +00:00
return redirect('/apps')
@app.post("/edit_app/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@is_not_permabanned
def edit_oauth_app(v, aid):
2022-10-16 09:51:42 +00:00
try:
aid = int(aid)
except:
abort(404)
2023-03-16 06:27:58 +00:00
app = g.db.get(OauthApp, aid)
if not app: abort(404)
2022-05-04 23:09:46 +00:00
if app.author_id != v.id: abort(403)
app.redirect_uri = request.values.get('redirect_uri')
app.app_name = request.values.get('name')
app.description = request.values.get("description")[:256]
2023-03-16 06:27:58 +00:00
g.db.add(app)
2022-05-04 23:09:46 +00:00
return redirect('/settings/apps')
@app.post("/admin/app/approve/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-05-04 23:09:46 +00:00
def admin_app_approve(v, aid):
2023-03-16 06:27:58 +00:00
app = g.db.get(OauthApp, aid)
if not app: abort(404)
2022-05-04 23:09:46 +00:00
user = app.author
if not app.client_id:
app.client_id = secrets.token_urlsafe(64)[:64]
2023-03-16 06:27:58 +00:00
g.db.add(app)
access_token = secrets.token_urlsafe(128)[:128]
new_auth = ClientAuth(
oauth_client = app.id,
user_id = user.id,
access_token=access_token
)
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
g.db.add(new_auth)
2022-05-04 23:09:46 +00:00
send_repeatable_notification(user.id, f"@{v.username} (a site admin) has approved your application `{app.app_name}`. Here's your access token: ||{access_token}||\n\nPlease check the guide [here](/api) if you don't know what to do next!")
2022-05-04 23:09:46 +00:00
ma = ModAction(
kind="approve_app",
user_id=v.id,
target_user_id=user.id,
)
2023-03-16 06:27:58 +00:00
g.db.add(ma)
2022-05-04 23:09:46 +00:00
return {"message": f"'{app.app_name}' approved!"}
2022-05-04 23:09:46 +00:00
@app.post("/admin/app/revoke/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-05-04 23:09:46 +00:00
def admin_app_revoke(v, aid):
2023-03-16 06:27:58 +00:00
app = g.db.get(OauthApp, aid)
2022-05-04 23:09:46 +00:00
if app:
2023-03-16 06:27:58 +00:00
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
2022-05-04 23:09:46 +00:00
2022-08-25 15:56:51 +00:00
if v.id != app.author.id:
send_repeatable_notification(app.author.id, f"@{v.username} (a site admin) has revoked your application `{app.app_name}`.")
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
g.db.delete(app)
2022-05-04 23:09:46 +00:00
ma = ModAction(
kind="revoke_app",
user_id=v.id,
target_user_id=app.author.id,
)
2023-03-16 06:27:58 +00:00
g.db.add(ma)
2022-05-04 23:09:46 +00:00
return {"message": f"'{app.app_name}' revoked!"}
2022-05-04 23:09:46 +00:00
@app.post("/admin/app/reject/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-05-04 23:09:46 +00:00
def admin_app_reject(v, aid):
2023-03-16 06:27:58 +00:00
app = g.db.get(OauthApp, aid)
2022-05-04 23:09:46 +00:00
if app:
2023-03-16 06:27:58 +00:00
for auth in g.db.query(ClientAuth).filter_by(oauth_client=app.id).all(): g.db.delete(auth)
2022-05-04 23:09:46 +00:00
2022-09-12 17:52:07 +00:00
if v.id != app.author.id:
send_repeatable_notification(app.author.id, f"@{v.username} (a site admin) has rejected your application `{app.app_name}`.")
2022-05-04 23:09:46 +00:00
2023-03-16 06:27:58 +00:00
g.db.delete(app)
2022-05-04 23:09:46 +00:00
ma = ModAction(
kind="reject_app",
user_id=v.id,
target_user_id=app.author.id,
)
2023-03-16 06:27:58 +00:00
g.db.add(ma)
2022-05-04 23:09:46 +00:00
return {"message": f"'{app.app_name}' rejected!"}
2022-05-04 23:09:46 +00:00
@app.get("/admin/app/<int:aid>/posts")
@limiter.limit(DEFAULT_RATELIMIT)
2023-01-21 04:39:46 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-10-28 20:13:58 +00:00
def admin_app_id_posts(v, aid):
2023-03-16 06:27:58 +00:00
oauth = g.db.get(OauthApp, aid)
if not oauth: abort(404)
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page()
pids, total = oauth.idlist(Submission, page=page)
2022-05-04 23:09:46 +00:00
posts = get_posts(pids, v=v)
2022-05-04 23:09:46 +00:00
return render_template("admin/app.html",
2022-09-04 23:15:37 +00:00
v=v,
app=oauth,
listing=posts,
total=total
2022-09-04 23:15:37 +00:00
)
2022-05-04 23:09:46 +00:00
@app.get("/admin/app/<int:aid>/comments")
@limiter.limit(DEFAULT_RATELIMIT)
2023-01-21 04:39:46 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-05-04 23:09:46 +00:00
def admin_app_id_comments(v, aid):
2023-03-16 06:27:58 +00:00
oauth = g.db.get(OauthApp, aid)
if not oauth: abort(404)
2022-05-04 23:09:46 +00:00
2023-05-05 05:23:59 +00:00
page = get_page()
cids, total = oauth.idlist(Comment, page=page)
2022-05-04 23:09:46 +00:00
comments = get_comments(cids, v=v)
2022-05-04 23:09:46 +00:00
return render_template("admin/app.html",
2022-09-04 23:15:37 +00:00
v=v,
app=oauth,
comments=comments,
total=total,
2022-09-04 23:15:37 +00:00
standalone=True
)
2022-05-04 23:09:46 +00:00
@app.get("/admin/apps")
@limiter.limit(DEFAULT_RATELIMIT)
2023-01-21 04:39:46 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@admin_level_required(PERMS['APPS_MODERATION'])
2022-05-04 23:09:46 +00:00
def admin_apps_list(v):
2023-03-16 06:27:58 +00:00
not_approved = g.db.query(OauthApp).filter(OauthApp.client_id == None).order_by(OauthApp.id.desc()).all()
2023-03-16 06:27:58 +00:00
approved = g.db.query(OauthApp).filter(OauthApp.client_id != None).order_by(OauthApp.id.desc()).all()
apps = not_approved + approved
2022-05-04 23:09:46 +00:00
return render_template("admin/apps.html", v=v, apps=apps)
@app.post("/reroll/<int:aid>")
2023-02-27 05:33:45 +00:00
@limiter.limit('1/second', scope=rpath)
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
@limiter.limit(DEFAULT_RATELIMIT)
2023-02-26 01:42:39 +00:00
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
2022-05-04 23:09:46 +00:00
@auth_required
def reroll_oauth_tokens(aid, v):
aid = aid
2023-03-16 06:27:58 +00:00
a = g.db.get(OauthApp, aid)
if not a: abort(404)
2022-05-04 23:09:46 +00:00
if a.author_id != v.id: abort(403)
a.client_id = secrets.token_urlsafe(64)[:64]
2023-03-16 06:27:58 +00:00
g.db.add(a)
2022-05-04 23:09:46 +00:00
return {"message": f"Client ID for '{a.app_name}' has been rerolled!", "id": a.client_id}