diff --git a/app/accounts/__init__.py b/app/accounts/__init__.py index 81afe41..e69de29 100644 --- a/app/accounts/__init__.py +++ b/app/accounts/__init__.py @@ -1,117 +0,0 @@ -from app.core import bcrypt -from flask import Blueprint -from .models import Account, Role - -accounts_bp = Blueprint('accounts', __name__) - - -def create_account(username, email, password): - """ - Tries to create account with given parameters. Raises error on failure - - :param username: Desired username for Account - :param email: Desired email for Account - :param password: Desired password for Account - :type username: string - :type email: string - :type password: string - :returns: True if account is successfully created - :rtype: Boolean - :raises: ValueError if account already exists - """ - if not Account.exists_with_any_of(username=username, email=email): - pw_hash = bcrypt.generate_password_hash(password).decode('utf-8') - account = Account(username, pw_hash, email) - account.save() - return True - - raise ValueError("Account with given parameters already exists") - - -def update_account_role(account_id, role_id): - """ - Tries to update account role - - :param account_id: Target account id - :param role_id: New role role_id - :type account_id: int - :type role_id: int - :returns: True if role is updated successfully - :rtype: Boolean - """ - acc = Account.get(id=account_id) - acc.role_id = role_id - acc.save() - - -def create_role(display_name, permissions): - """ - Tries to create role - - :param display_name: Name of role - display only - :param permissions: List of strings - permissions that this role has - :type display_name: String - :type permissions: List of String - :returns: True if role is successfully created - :rtype: Boolean - :raises: ValueError if role already exists - """ - role = Role(display_name, permissions) - role.save() - - -def get_role(role_id): - """ - Tries to get role - - :param role_id: Id of role - :type role_id: int - :returns: Role if found - :rtype: Role - """ - return Role.get(role_id) - - -def get_all_roles(): - """ - Gets all roles - - :returns: Role list if found - :rtype: List of Roles - """ - return Role.get_all() - - -def create_token(username, password): - """ - Tries to create token for account with given parameters. - Raises error on failure - - :param username: username of Account - :param password: password of Account - :type username: string - :type password: string - :returns: created token - :rtype: string - :raises: ValueError if credentials are invalid or account does not exist - """ - if not Account.exists(username=username): - raise ValueError("Invalid credentials") - - account = Account.get(username=username) - if not bcrypt.check_password_hash(account.password, password): - raise ValueError("Invalid credentials") - - return account.create_auth_token() - - -def validate_token(token): - """ - Validates token and returns associated account - - :param token: auth token to validate - :type token: string - :returns: created token - :rtype: Account - """ - return Account.validate_token(token) diff --git a/app/accounts/api.py b/app/accounts/api.py new file mode 100644 index 0000000..4a2d9d8 --- /dev/null +++ b/app/accounts/api.py @@ -0,0 +1,114 @@ +from app.core import bcrypt +from .models import Account, Role + + +def create_account(username, email, password): + """ + Tries to create account with given parameters. Raises error on failure + + :param username: Desired username for Account + :param email: Desired email for Account + :param password: Desired password for Account + :type username: string + :type email: string + :type password: string + :returns: True if account is successfully created + :rtype: Boolean + :raises: ValueError if account already exists + """ + if not Account.exists_with_any_of(username=username, email=email): + pw_hash = bcrypt.generate_password_hash(password).decode('utf-8') + account = Account(username, pw_hash, email) + account.save() + return True + + raise ValueError("Account with given parameters already exists") + + +def update_account_role(account_id, role_id): + """ + Tries to update account role + + :param account_id: Target account id + :param role_id: New role role_id + :type account_id: int + :type role_id: int + :returns: True if role is updated successfully + :rtype: Boolean + """ + acc = Account.get(id=account_id) + acc.role_id = role_id + acc.save() + + +def create_role(display_name, permissions): + """ + Tries to create role + + :param display_name: Name of role - display only + :param permissions: List of strings - permissions that this role has + :type display_name: String + :type permissions: List of String + :returns: True if role is successfully created + :rtype: Boolean + :raises: ValueError if role already exists + """ + role = Role(display_name, permissions) + role.save() + + +def get_role(role_id): + """ + Tries to get role + + :param role_id: Id of role + :type role_id: int + :returns: Role if found + :rtype: Role + """ + return Role.get(role_id) + + +def get_all_roles(): + """ + Gets all roles + + :returns: Role list if found + :rtype: List of Roles + """ + return Role.get_all() + + +def create_token(username, password): + """ + Tries to create token for account with given parameters. + Raises error on failure + + :param username: username of Account + :param password: password of Account + :type username: string + :type password: string + :returns: created token + :rtype: string + :raises: ValueError if credentials are invalid or account does not exist + """ + if not Account.exists(username=username): + raise ValueError("Invalid credentials") + + account = Account.get(username=username) + if not bcrypt.check_password_hash(account.password, password): + raise ValueError("Invalid credentials") + + return account.create_auth_token() + + +def validate_token(token): + """ + Validates token and returns associated account + + :param token: auth token to validate + :type token: string + :returns: created token + :rtype: Account + """ + return Account.validate_token(token) diff --git a/app/accounts/blueprint.py b/app/accounts/blueprint.py new file mode 100644 index 0000000..3ba41b8 --- /dev/null +++ b/app/accounts/blueprint.py @@ -0,0 +1,3 @@ +from flask import Blueprint + +accounts_bp = Blueprint('accounts', __name__) diff --git a/app/api/__init__.py b/app/api/__init__.py index c862280..0b077dc 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -3,7 +3,7 @@ from flask import Blueprint, request, g from flask_restful import Api, Resource, abort from functools import wraps from marshmallow import ValidationError -from app.accounts import validate_token +from app.accounts.api import validate_token api_bp = Blueprint('api', __name__) diff --git a/app/api/resources/account.py b/app/api/resources/account.py index 382f46b..8fbe961 100644 --- a/app/api/resources/account.py +++ b/app/api/resources/account.py @@ -3,7 +3,7 @@ from flask import g from marshmallow import Schema, fields from webargs.flaskparser import use_args from flasgger import swag_from -import app.accounts as accounts +import app.accounts.api as accounts from app.api import ProtectedResource, requires_permission diff --git a/app/api/resources/token.py b/app/api/resources/token.py index d98f2b4..26d80bf 100644 --- a/app/api/resources/token.py +++ b/app/api/resources/token.py @@ -3,7 +3,7 @@ from webargs import fields from webargs.flaskparser import use_args from flasgger import swag_from from app.api import ProtectedResource -import app.accounts as accounts +import app.accounts.api as accounts class TokenResource(Resource): diff --git a/app/core.py b/app/core.py index ef5a5bc..c4ca7ea 100644 --- a/app/core.py +++ b/app/core.py @@ -4,7 +4,7 @@ from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from flasgger import Swagger from flask_cors import CORS -from .tasks import celery as celery_configurator +from .tasks import celery_configurator app = FlaskAPI(__name__, instance_relative_config=True) app.config.from_object('config') @@ -27,7 +27,7 @@ def setup_blueprints(app): They are exposed as blueprints just for consistency, otherwise they are just simple python packages/modules """ - from .accounts import accounts_bp + from .accounts.blueprint import accounts_bp from .devices import devices_bp from .dashboard import dashboard_bp from .api import api_bp diff --git a/app/tasks/celery.py b/app/tasks/celery_configurator.py similarity index 100% rename from app/tasks/celery.py rename to app/tasks/celery_configurator.py diff --git a/config.py b/config.py index f9e0237..55e4c11 100644 --- a/config.py +++ b/config.py @@ -28,7 +28,7 @@ CSRF_SESSION_KEY = "secret" SECRET_KEY = "?['Z(Z\x83Y \x06T\x12\x96<\xff\x12\xe0\x1b\xd1J\xe0\xd9ld" # MQTT configuration -MQTT_CLIENT_ID = 'final-iot-backend-server' +MQTT_CLIENT_ID = 'final-iot-backend-server-local2' MQTT_BROKER_URL = 'broker.hivemq.com' MQTT_BROKER_PORT = 1883 MQTT_USERNAME = 'user'