From 1c46089ae884c27fd9f7f545d97f6ccec76af872 Mon Sep 17 00:00:00 2001 From: Gabriel Carneiro Date: Wed, 4 Oct 2023 20:43:38 -0300 Subject: [PATCH] add publish verify --- setup.cfg | 8 ++++---- src/stream_auth/main.py | 3 +++ src/stream_auth/middlewares/jwt.py | 4 ++-- src/stream_auth/routes/stream.py | 30 +++++++++++++++++++++++++++--- src/stream_auth/routes/user.py | 5 ++++- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/setup.cfg b/setup.cfg index a52045e..dd5e258 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,10 +6,10 @@ long_description = file: README.md long_description_content_type = text/markdown author = Gabriel Carneiro author_email = therealcarneiro@gmail.com -license = MIT -license_files = LICENSE +#license = MIT +#license_files = LICENSE #url = https://github.com/theRealCarneiro/pulsemeeter -keywords = audio, mixing, mixer +#keywords = audio, mixing, mixer classifiers = Programming Language :: Python :: 3 Environment :: X11 Applications @@ -26,7 +26,7 @@ package_dir = [options.entry_points] console_scripts = - meexer = meexer.main:main + stream_auth = stream_auth.main:main [options.packages.find] where=src diff --git a/src/stream_auth/main.py b/src/stream_auth/main.py index 21b5710..8bef0ef 100644 --- a/src/stream_auth/main.py +++ b/src/stream_auth/main.py @@ -6,6 +6,7 @@ import logging from flask import Flask from stream_auth import settings from stream_auth.routes.user import user as user_routes +from stream_auth.routes.stream import user as stream_routes app = Flask(__name__) @@ -14,6 +15,8 @@ logging.basicConfig(level=logging.INFO) def main(): app.register_blueprint(user_routes) + app.register_blueprint(stream_routes) + app.run(host=settings.HOST, port=settings.PORT) diff --git a/src/stream_auth/middlewares/jwt.py b/src/stream_auth/middlewares/jwt.py index b6de10d..1a07c0b 100644 --- a/src/stream_auth/middlewares/jwt.py +++ b/src/stream_auth/middlewares/jwt.py @@ -13,8 +13,8 @@ JWT_PRIV_KEY = read_key(settings.JWT_PRIV_PATH) JWT_PUB_KEY = read_key(settings.JWT_PUB_PATH) -def create_token(username: str, stream_key: str): - exp = time.time() + settings.JWT_EXP_TIME +def create_token(username: str, stream_key: str, exp: int = settings.JWT_EXP_TIME): + exp = time.time() + exp payload = {'username': username, 'stream_key': stream_key, 'exp': exp} return jwt.encode(payload, JWT_PRIV_KEY, algorithm="RS256") diff --git a/src/stream_auth/routes/stream.py b/src/stream_auth/routes/stream.py index 9d42203..a0785fa 100644 --- a/src/stream_auth/routes/stream.py +++ b/src/stream_auth/routes/stream.py @@ -3,9 +3,11 @@ For controlling streams ''' # import logging -from flask import Blueprint, Response, request +from flask import Blueprint, Response, request, redirect from stream_auth.middlewares.auth import auth +from stream_auth.middlewares import jwt from stream_auth.models.stream import Stream as StreamModel +from stream_auth.database import user stream = Blueprint('user', __name__) @@ -17,5 +19,27 @@ def create_stream(): username = json['username'] title = json['name'] description = json['description'] - # stream_key = json['stream_key'] - StreamModel(title, description, username) + StreamModel(username, title, description) + + +@stream.route('/publish_check') +def publish_check(): + + # TODO: check if user created stream + + # check if already redirected + token = request.form.get('token') + if jwt.verify(token): + return Response('OK', 200) + + # get user + try: + stream_key = request.form.get('name') + stream_user = user.search_stream_key(stream_key)[0]['username'] + username = stream_user['username'] + + except IndexError: + return Response('Invalid Stream Key', 401) + + token = jwt.create_token(username, stream_key, 10) + return redirect(f'rtmp://127.0.0.1:33000/live/{username}?{token}', code=302) diff --git a/src/stream_auth/routes/user.py b/src/stream_auth/routes/user.py index 5b10259..d34d5b3 100644 --- a/src/stream_auth/routes/user.py +++ b/src/stream_auth/routes/user.py @@ -39,7 +39,10 @@ def login(): username = json['username'] password = json['password'] - log_user = userdb.search_user(username)[0] + try: + log_user = userdb.search_user(username)[0] + except IndexError: + return Response('User or password incorrect', 401) if not log_user.check_password(password): return Response('User or password incorrect', 401)