"""Authentication module. """ import os import quart import quart_cors import quart_jwt_extended as jwt api_password = os.getenv("API_PASSWORD") if not api_password: raise RuntimeError("API_PASSWORD env not set") app = quart.Blueprint("auth", __name__) app = quart_cors.cors(app) @app.route("/login", methods=["POST"]) async def login(): """Gets a JWT token.""" if not quart.request.is_json: return {"error": "missing JSON in request"}, 400 request = await quart.request.json password = request.get("password", None) if not password: return {"error": "missing password parameter"}, 400 if password != api_password: return {"error": "bad password"}, 401
import quart import datetime import os try: from app import config except ImportError: pass blueprint = quart.Blueprint("api", __name__) async def calc_exam_timestamp() -> float: try: exam_month = config.EXAM_MONTH exam_day = config.EXAM_DAY exam_hour = config.EXAM_HOUR exam_min = config.EXAM_MIN timedelta = config.TIMEDELTA except NameError: exam_month = int(os.environ["EXAM_MONTH"]) exam_day = int(os.environ["EXAM_DAY"]) exam_hour = int(os.environ["EXAM_HOUR"]) exam_min = int(os.environ["EXAM_MIN"]) timedelta = int(os.environ["TIMEDELTA"]) tzinfo = datetime.timezone(datetime.timedelta(hours=timedelta)) curr_datetime = datetime.datetime.now(tz=tzinfo) curr_year = curr_datetime.year if curr_datetime > datetime.datetime( curr_year, exam_month, exam_day, exam_hour, exam_min, tzinfo=tzinfo): exam_datetime = datetime.datetime(curr_year + 1,
import quart from app.utils import decorators from bot.api import tokens as bot_tokens from bot.api import kicks as bot_kicks blueprint = quart.Blueprint("kicks", __name__) @blueprint.route("/api/kicks/kick", methods=["POST"]) @decorators.req_fields({"token": str, "user_id": int}) @decorators.token_verify async def ban_user(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) result = await bot_kicks.kick_user(chat_id, req_json["user_id"]) return quart.jsonify({"result": result})
"""Bot module. """ import ipc import quart import quart_cors import quart_jwt_extended as jwt app = quart.Blueprint("bot", __name__) app = quart_cors.cors(app) @app.route("/health") async def health(): """Runs a health check request on the bot.""" response = await ipc.ipc_client.request("health") return response @app.route("/describe") @jwt.jwt_required async def describe(): """Runs a describe request on the bot.""" response = await ipc.ipc_client.request("describe") return response @app.route("/config/main") @jwt.jwt_required async def main_config(): """Runs a main config request on the bot."""
"""Blueprint for total endpoint Contains the endpoint that calculates the total sum """ import quart from app.numbers_api import NumbersAPI total = quart.Blueprint('total', __name__) def get_api() -> NumbersAPI: """Gets and creates the NumbersAPI client""" if 'numbers_api' not in quart.g: quart.g.numbers_api = NumbersAPI() return quart.g.numbers_api @total.route('') async def total_endpoint() -> quart.Response: """Endpoint, calculates total sum from the numbers api""" numbers = await get_api().get() return quart.jsonify({"total": sum(numbers)})
import quart blueprint = quart.Blueprint("root", __name__) @blueprint.route("/") async def root(): return quart.redirect("/index")
import quart blueprint = quart.Blueprint("index", __name__) @blueprint.route("/index") async def index(): if "token" in quart.session: return quart.redirect("/chat/dashboard") else: return quart.redirect("/login")
import quart import quart.flask_patch import aiohttp from app import session, config, logger from app.utils import utils from app.data import login_form blueprint = quart.Blueprint("login", __name__) async def verify_token(token: str): url = await utils.join_uri((config.BOT_URI, "api", "tokens", "verify")) json = {"token": token} async with session.post(url, json=json) as resp: json_resp = await resp.json() return json_resp["result"] @blueprint.route("/login", methods=("GET", "POST")) async def login(): form = login_form.LoginForm() if form.validate_on_submit(): token = form.token.data url = await utils.join_uri((config.BOT_URI, "api", "tokens", "verify")) json = {"token": token} logger.debug("%s", token) logger.debug("%s", url) try: if await verify_token(token): quart.session["token"] = token
request, Response, flash, ) import babel import wtforms import flask_wtf from flask_babel import lazy_gettext as _l, _ from . import xmpputil, _version from .infra import client bp = quart.Blueprint("main", __name__) class LoginForm(flask_wtf.FlaskForm): # type:ignore address = wtforms.TextField( _l("Address"), validators=[wtforms.validators.InputRequired()], ) password = wtforms.PasswordField( _l("Password"), validators=[wtforms.validators.InputRequired()], ) action_signin = wtforms.SubmitField(_l("Sign in"), )
import quart from app.utils import decorators from bot.api import tokens as bot_tokens from bot.api import bans as bot_bans blueprint = quart.Blueprint("bans", __name__) @blueprint.route("/api/bans/ban", methods=["POST"]) @decorators.req_fields({"token": str, "user_id": int}) @decorators.token_verify async def ban_user(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) result = await bot_bans.ban_user(chat_id, req_json["user_id"]) return quart.jsonify({"result": result}) @blueprint.route("/api/bans/unban", methods=["POST"]) @decorators.req_fields({"token": str, "user_id": int}) @decorators.token_verify async def unban_user(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) result = await bot_bans.unban_user(chat_id, req_json["user_id"]) return quart.jsonify({"result": result})
import quart import typing from app import logger from bot.api import users as bot_users from app.utils import decorators, utils blueprint = quart.Blueprint("users", __name__) @blueprint.route("/api/users/info", methods=["POST"]) @decorators.req_fields({"token": str, "ids": typing.Iterable}) @decorators.token_verify async def get_user(): req_json = await quart.request.json try: users = await bot_users.get_users_info(req_json["ids"]) except ValueError as exc: await quart.abort(400, exc) user_dict = { user.id: { item[0]: item[1] for item in vars(user).items() if utils.is_jsonable(item[1]) } for user in users } return quart.jsonify(user_dict) @blueprint.route("/api/users/<int:user_id>/photo", methods=["GET"]) async def photo(user_id): photo = await bot_users.get_last_photo(user_id)
import dataclasses import logging import quart from quart import current_app as app from quart import render_template from .indicator import Indicator log = logging.getLogger(__name__) bp = quart.Blueprint('view', __name__) @bp.route('/') async def index(): lat = quart.request.args.get('lat') lng = quart.request.args.get('lng') return await render_template('view/index.html', lat=lat, lng=lng) @bp.route('/location') async def location(): try: latitude = str(quart.request.args['lat']) longitude = str(quart.request.args['lng']) except KeyError: log.info('Failed to get coordinates from parameters.') return quart.abort(400)
#!/usr/bin/python import quart from quart import render_template from . import getTemplateDictBase err_page = quart.Blueprint("err_page", __name__) @err_page.route('/500.html') async def err_page(): """ Err page. """ return await render_template("500.html", **getTemplateDictBase())
import quart from bot.api import tokens as bot_tokens from app.utils import decorators blueprint = quart.Blueprint("tokens", __name__) @blueprint.route("/api/tokens/verify", methods=["POST"]) @decorators.req_fields({"token": str}) async def token_verify(): json_resp = await quart.request.json token = json_resp["token"] result = await bot_tokens.verify_token(token) return quart.jsonify({"result": result})
import quart from app.utils import decorators, utils from bot.api import tokens as bot_tokens from bot.api import users as bot_users from bot.api import chats as bot_chats blueprint = quart.Blueprint("chats", __name__) @blueprint.route("/api/chats/users", methods=["POST"]) @decorators.req_fields({"token": str}) @decorators.token_verify async def users(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) users = await bot_chats.get_chat_members(chat_id) user_dict = {user.id: {item[0]: item[1] for item in vars(user).items() if utils.is_jsonable(item[1])} for user in users} return quart.jsonify(user_dict) @blueprint.route("/api/chats/info", methods=["POST"]) @decorators.req_fields({"token": str}) @decorators.token_verify async def title(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) chat = await bot_chats.get_chat_info(chat_id) return quart.jsonify(chat)
import quart from app.utils import decorators blueprint = quart.Blueprint("logout", __name__) @blueprint.route("/logout") @decorators.need_token async def logout(): del quart.session["token"] return quart.redirect("/login")
import quart from app.utils import decorators from bot.api import promotions as bot_promotions from bot.api import tokens as bot_tokens blueprint = quart.Blueprint("promotions", __name__) @blueprint.route("/api/promotions/promote", methods=["POST"]) @decorators.req_fields({"token": str, "user_id": int}) @decorators.token_verify async def ban_user(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) result = await bot_promotions.promote_user(chat_id, req_json["user_id"]) return quart.jsonify({"result": result}) @blueprint.route("/api/promotions/demote", methods=["POST"]) @decorators.req_fields({"token": str, "user_id": int}) @decorators.token_verify async def unban_user(): req_json = await quart.request.json token = req_json["token"] chat_id = await bot_tokens.get_chat_id_by_token(token) result = await bot_promotions.demote_user(chat_id, req_json["user_id"]) return quart.jsonify({"result": result})