update
This commit is contained in:
parent
1a89736c83
commit
1939180939
|
@ -1,3 +1,41 @@
|
||||||
'''
|
'''
|
||||||
For managing streams
|
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
|
||||||
|
|
|
@ -10,7 +10,7 @@ db = TinyDB(settings.USER_DATABASE)
|
||||||
query = Query()
|
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
|
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:
|
if len(db.search(query.username == username)) != 0:
|
||||||
raise ValueError("Username already exists.")
|
raise ValueError("Username already exists.")
|
||||||
|
|
||||||
user = User(username, hash_pass)
|
user = User(username, password)
|
||||||
while db.search(query.stream_key == user.stream_key):
|
while db.search(query.stream_key == user.stream_key):
|
||||||
user.regenerate_stream_key()
|
user.regenerate_stream_key()
|
||||||
|
|
||||||
|
@ -26,6 +26,13 @@ def create_user(username: str, hash_pass: str):
|
||||||
return user
|
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):
|
def search_stream_key(stream_key: str):
|
||||||
'''
|
'''
|
||||||
Search for a specific stream key
|
Search for a specific stream key
|
||||||
|
|
|
@ -5,10 +5,10 @@ class Stream:
|
||||||
A single stream
|
A single stream
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, name: str, description: str, username: str):
|
def __init__(self, username: str, title: str, description: str):
|
||||||
self.name = name
|
self.username = username
|
||||||
|
self.title = title
|
||||||
self.description = description
|
self.description = description
|
||||||
self.user = username
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
'''
|
'''
|
||||||
|
|
|
@ -27,6 +27,7 @@ class User:
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = str(bcrypt.hashpw(password.encode('utf-8'), salt), 'utf-8')
|
self.password = str(bcrypt.hashpw(password.encode('utf-8'), salt), 'utf-8')
|
||||||
self.stream_key = generate_stream_key(STREAM_KEY_LENGTH)
|
self.stream_key = generate_stream_key(STREAM_KEY_LENGTH)
|
||||||
|
self.live = False
|
||||||
|
|
||||||
def check_passwrod(self, password: str):
|
def check_passwrod(self, password: str):
|
||||||
input_pass = password.encode('utf-8')
|
input_pass = password.encode('utf-8')
|
||||||
|
|
|
@ -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)
|
|
@ -3,13 +3,15 @@ For controlling user routes
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import logging
|
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.models.user import User
|
||||||
|
from stream_auth.database import user as userdb
|
||||||
|
from stream_auth.middlewares import jwt
|
||||||
|
|
||||||
user = Blueprint('user', __name__)
|
user = Blueprint('user', __name__)
|
||||||
|
|
||||||
|
|
||||||
@user.route("/signin", methods=["POST"])
|
@user.route('/signin', methods=['POST'])
|
||||||
def create():
|
def create():
|
||||||
'''
|
'''
|
||||||
Create a new user
|
Create a new user
|
||||||
|
@ -19,7 +21,7 @@ def create():
|
||||||
username = json['username']
|
username = json['username']
|
||||||
password = json['password']
|
password = json['password']
|
||||||
|
|
||||||
new_user = User(username, password)
|
new_user = userdb.create_user(username, password)
|
||||||
|
|
||||||
logging.info('User %s created with stream key %s',
|
logging.info('User %s created with stream key %s',
|
||||||
new_user.username, new_user.stream_key)
|
new_user.username, new_user.stream_key)
|
||||||
|
@ -27,26 +29,28 @@ def create():
|
||||||
return Response('OK', 200)
|
return Response('OK', 200)
|
||||||
|
|
||||||
|
|
||||||
@user.route("/login", methods=["POST"])
|
@user.route('/login', methods=['POST'])
|
||||||
def login():
|
def login():
|
||||||
'''
|
'''
|
||||||
Create a new user
|
User log in
|
||||||
'''
|
'''
|
||||||
|
|
||||||
json = request.get_json()
|
json = request.get_json()
|
||||||
username = json['username']
|
username = json['username']
|
||||||
password = json['password']
|
password = json['password']
|
||||||
|
|
||||||
# TODO: actully do this
|
log_user = userdb.search_user(username)[0]
|
||||||
new_user = User(username, password)
|
|
||||||
|
|
||||||
# logging.info('User %s created with stream key %s',
|
if not log_user.check_password(password):
|
||||||
# new_user.username, new_user.stream_key)
|
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():
|
def logout():
|
||||||
'''
|
'''
|
||||||
Create a new user
|
Create a new user
|
||||||
|
@ -56,10 +60,8 @@ def logout():
|
||||||
username = json['username']
|
username = json['username']
|
||||||
password = json['password']
|
password = json['password']
|
||||||
|
|
||||||
# TODO: actully do this
|
|
||||||
new_user = User(username, password)
|
new_user = User(username, password)
|
||||||
|
|
||||||
# logging.info('User %s created with stream key %s',
|
logging.info('User %s logged out', new_user.username)
|
||||||
# new_user.username, new_user.stream_key)
|
|
||||||
|
|
||||||
return Response('OK', 200)
|
return Response('OK', 200)
|
||||||
|
|
|
@ -14,4 +14,6 @@ JWT_PUB_PATH = os.path.join(KEY_DIR, 'jwtRS256.key.pub')
|
||||||
JWT_EXP_TIME = 2592000
|
JWT_EXP_TIME = 2592000
|
||||||
|
|
||||||
USER_DATABASE = '/home/gabriel/db.json'
|
USER_DATABASE = '/home/gabriel/db.json'
|
||||||
|
STREAM_DATABASE = '/home/gabriel/streams.json'
|
||||||
|
LIVE_STREAM_DATABASE = '/home/gabriel/live_streams.json'
|
||||||
STREAM_KEY_LENGTH = 32
|
STREAM_KEY_LENGTH = 32
|
||||||
|
|
Loading…
Reference in New Issue