This commit is contained in:
Carneiro 2023-10-04 18:41:03 -03:00
parent 1a89736c83
commit 1939180939
7 changed files with 90 additions and 19 deletions

View File

@ -1,3 +1,41 @@
'''
For managing streams
'''
from tinydb import TinyDB, Query
from stream_auth.models.stream import Stream as StreamModel
from stream_auth import settings
live_db = TinyDB(settings.LIVE_STREAM_DATABASE)
stream_db = TinyDB(settings.STREAM_DATABASE)
query = Query()
def create_stream(username: str, title: str, description: str):
'''
Creates a new stream for a user
'''
if len(live_db.search(query.username == username)) > 0:
raise ValueError('User already live')
stream = StreamModel(username, title, description)
live_db.insert(stream.__dict__)
return stream
def search_live_stream(username: str):
return live_db.search(query.username == username)
def stop_stream(username: str):
'''
Search for a live stream
'''
stream = search_live_stream(username)
live_db.remove(query.username == username)
stream_db.insert(dict(stream[0]))
return stream

View File

@ -10,7 +10,7 @@ db = TinyDB(settings.USER_DATABASE)
query = Query()
def create_user(username: str, hash_pass: str):
def create_user(username: str, password: str):
'''
Creates a new unique user, with a unique stream_key
'''
@ -18,7 +18,7 @@ def create_user(username: str, hash_pass: str):
if len(db.search(query.username == username)) != 0:
raise ValueError("Username already exists.")
user = User(username, hash_pass)
user = User(username, password)
while db.search(query.stream_key == user.stream_key):
user.regenerate_stream_key()
@ -26,6 +26,13 @@ def create_user(username: str, hash_pass: str):
return user
def search_user(username: str):
'''
Search for a specific stream key
'''
return db.search(query.username == username)
def search_stream_key(stream_key: str):
'''
Search for a specific stream key

View File

@ -5,10 +5,10 @@ class Stream:
A single stream
'''
def __init__(self, name: str, description: str, username: str):
self.name = name
def __init__(self, username: str, title: str, description: str):
self.username = username
self.title = title
self.description = description
self.user = username
def start(self):
'''

View File

@ -27,6 +27,7 @@ class User:
self.username = username
self.password = str(bcrypt.hashpw(password.encode('utf-8'), salt), 'utf-8')
self.stream_key = generate_stream_key(STREAM_KEY_LENGTH)
self.live = False
def check_passwrod(self, password: str):
input_pass = password.encode('utf-8')

View File

@ -0,0 +1,21 @@
'''
For controlling streams
'''
# import logging
from flask import Blueprint, Response, request
from stream_auth.middlewares.auth import auth
from stream_auth.models.stream import Stream as StreamModel
stream = Blueprint('user', __name__)
@stream.route('/create_stream', methods=['POST'])
@auth
def create_stream():
json = request.get_json()
username = json['username']
title = json['name']
description = json['description']
# stream_key = json['stream_key']
StreamModel(title, description, username)

View File

@ -3,13 +3,15 @@ For controlling user routes
'''
import logging
from flask import Blueprint, Response, request
from flask import Blueprint, Response, request, jsonify, make_response
from stream_auth.models.user import User
from stream_auth.database import user as userdb
from stream_auth.middlewares import jwt
user = Blueprint('user', __name__)
@user.route("/signin", methods=["POST"])
@user.route('/signin', methods=['POST'])
def create():
'''
Create a new user
@ -19,7 +21,7 @@ def create():
username = json['username']
password = json['password']
new_user = User(username, password)
new_user = userdb.create_user(username, password)
logging.info('User %s created with stream key %s',
new_user.username, new_user.stream_key)
@ -27,26 +29,28 @@ def create():
return Response('OK', 200)
@user.route("/login", methods=["POST"])
@user.route('/login', methods=['POST'])
def login():
'''
Create a new user
User log in
'''
json = request.get_json()
username = json['username']
password = json['password']
# TODO: actully do this
new_user = User(username, password)
log_user = userdb.search_user(username)[0]
# logging.info('User %s created with stream key %s',
# new_user.username, new_user.stream_key)
if not log_user.check_password(password):
return Response('User or password incorrect', 401)
return Response('OK', 200)
token = jwt.create_token(log_user.username, log_user.stream_key)
logging.info('User %s logged in', log_user.username)
return make_response(jsonify({'token': token}), 201)
@user.route("/logout", methods=["POST"])
@user.route('/logout', methods=['POST'])
def logout():
'''
Create a new user
@ -56,10 +60,8 @@ def logout():
username = json['username']
password = json['password']
# TODO: actully do this
new_user = User(username, password)
# logging.info('User %s created with stream key %s',
# new_user.username, new_user.stream_key)
logging.info('User %s logged out', new_user.username)
return Response('OK', 200)

View File

@ -14,4 +14,6 @@ JWT_PUB_PATH = os.path.join(KEY_DIR, 'jwtRS256.key.pub')
JWT_EXP_TIME = 2592000
USER_DATABASE = '/home/gabriel/db.json'
STREAM_DATABASE = '/home/gabriel/streams.json'
LIVE_STREAM_DATABASE = '/home/gabriel/live_streams.json'
STREAM_KEY_LENGTH = 32