Added some api endpoints
This commit is contained in:
parent
c5b6e85c4d
commit
00921dc505
55
webservice/api.py
Normal file
55
webservice/api.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
|
from db import db
|
||||||
|
from helper_functions import check_password, hash_password
|
||||||
|
from models import User
|
||||||
|
|
||||||
|
api = Blueprint('api', __name__, url_prefix='/api/v1/resources/devices')
|
||||||
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||||
|
|
||||||
|
|
||||||
|
@api.route('/api/users', methods=['GET'])
|
||||||
|
def users():
|
||||||
|
res = []
|
||||||
|
for i in User.query.all():
|
||||||
|
res.append(i.as_dict())
|
||||||
|
|
||||||
|
return jsonify(res)
|
||||||
|
|
||||||
|
|
||||||
|
@api.route('/api/login', methods=['POST'])
|
||||||
|
def login():
|
||||||
|
request_data = request.get_json()
|
||||||
|
username = request_data['username']
|
||||||
|
password = request_data['password']
|
||||||
|
|
||||||
|
user = User.filter_by(username=username).first()
|
||||||
|
if check_password(user.password, password):
|
||||||
|
return True # TODO Return token
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@api.route('/api/logout', methods=['GET'])
|
||||||
|
def logout():
|
||||||
|
# TODO
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@api.route('/api/register', methods=['POST'])
|
||||||
|
def register():
|
||||||
|
request_data = request.get_json()
|
||||||
|
username = request_data['username']
|
||||||
|
password = request_data['password']
|
||||||
|
|
||||||
|
user = User(
|
||||||
|
username=username,
|
||||||
|
password=hash_password(password),
|
||||||
|
admin=False
|
||||||
|
)
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
pass
|
@ -1,7 +1,9 @@
|
|||||||
from flask import Flask, jsonify
|
from flask import Flask
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from models import *
|
from models import *
|
||||||
|
from api import api
|
||||||
|
from interface import interface
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
@ -18,17 +20,8 @@ def create_app():
|
|||||||
# Create all tables
|
# Create all tables
|
||||||
db.create_all()
|
db.create_all()
|
||||||
|
|
||||||
@application.route('/')
|
application.register_blueprint(api)
|
||||||
def hello_world():
|
application.register_blueprint(interface)
|
||||||
return 'Hello World!'
|
|
||||||
|
|
||||||
@application.route('/users')
|
|
||||||
def users():
|
|
||||||
res = []
|
|
||||||
for i in User.query.all():
|
|
||||||
res.append(i.as_dict())
|
|
||||||
|
|
||||||
return jsonify(res)
|
|
||||||
|
|
||||||
return application
|
return application
|
||||||
|
|
||||||
|
12
webservice/helper_functions.py
Normal file
12
webservice/helper_functions.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import hashlib
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
def hash_password(password):
|
||||||
|
salt = uuid.uuid4().hex
|
||||||
|
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
|
||||||
|
|
||||||
|
|
||||||
|
def check_password(hashed_password, user_password):
|
||||||
|
password, salt = hashed_password.split(':')
|
||||||
|
return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
|
12
webservice/interface.py
Normal file
12
webservice/interface.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from flask import Blueprint
|
||||||
|
|
||||||
|
interface = Blueprint('interface', __name__, url_prefix='/api/v1/resources/devices')
|
||||||
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||||
|
|
||||||
|
|
||||||
|
# Return all tags
|
||||||
|
@interface.route('/', methods=['GET'])
|
||||||
|
def get_tags():
|
||||||
|
return "Konfiguration"
|
Loading…
Reference in New Issue
Block a user