add dlt keys to the model

This commit is contained in:
Cayo Puigdefabregas 2022-11-22 19:12:11 +01:00
parent 5d8c26ade6
commit 9d8fb6b04a
4 changed files with 53 additions and 2 deletions

View File

@ -94,3 +94,4 @@ class DevicehubConfig(Config):
MAIL_PORT = config('MAIL_PORT', 587) MAIL_PORT = config('MAIL_PORT', 587)
MAIL_USE_TLS = config('MAIL_USE_TLS', True) MAIL_USE_TLS = config('MAIL_USE_TLS', True)
MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', '') MAIL_DEFAULT_SENDER = config('MAIL_DEFAULT_SENDER', '')
API_DLT = config('API_DLT', None)

View File

@ -1,4 +1,5 @@
from flask import g from flask import current_app as app
from flask import g, session
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from werkzeug.security import generate_password_hash from werkzeug.security import generate_password_hash
from wtforms import BooleanField, EmailField, PasswordField, validators from wtforms import BooleanField, EmailField, PasswordField, validators
@ -60,6 +61,12 @@ class LoginForm(FlaskForm):
if not user.is_active: if not user.is_active:
self.form_errors.append(self.error_messages['inactive']) self.form_errors.append(self.error_messages['inactive'])
if 'trublo' not in app.blueprints.keys():
token_dlt = (
user.get_dlt_keys(self.password.data).get('data', {}).get('api_token')
)
session['token_dlt'] = token_dlt
return user.is_active return user.is_active
@ -95,6 +102,15 @@ class PasswordForm(FlaskForm):
return True return True
def save(self, commit=True): def save(self, commit=True):
if 'trublo' not in app.blueprints.keys():
keys_dlt = g.user.get_dlt_keys(self.password.data)
g.user.reset_dlt_keys(self.newpassword.data, keys_dlt)
token_dlt = (
user.get_dlt_keys(self.password.data).get('data', {}).get('api_token')
)
session['token_dlt'] = token_dlt
g.user.password = self.newpassword.data g.user.password = self.newpassword.data
db.session.add(g.user) db.session.add(g.user)

View File

@ -1,5 +1,7 @@
import json
from uuid import uuid4 from uuid import uuid4
from citext import CIText
from flask import current_app as app from flask import current_app as app
from flask_login import UserMixin from flask_login import UserMixin
from sqlalchemy import BigInteger, Boolean, Column, Sequence from sqlalchemy import BigInteger, Boolean, Column, Sequence
@ -28,6 +30,7 @@ class User(UserMixin, Thing):
token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False) token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False)
active = Column(Boolean, default=True, nullable=False) active = Column(Boolean, default=True, nullable=False)
phantom = Column(Boolean, default=False, nullable=False) phantom = Column(Boolean, default=False, nullable=False)
api_keys_dlt = Column(CIText(), nullable=True)
inventories = db.relationship( inventories = db.relationship(
Inventory, Inventory,
backref=db.backref('users', lazy=True, collection_class=set), backref=db.backref('users', lazy=True, collection_class=set),
@ -93,6 +96,37 @@ class User(UserMixin, Thing):
# take advantage of SQL Alchemy PasswordType to verify password # take advantage of SQL Alchemy PasswordType to verify password
return self.password == password return self.password == password
def set_new_dlt_keys(self, password):
if 'trublo' not in app.blueprints.keys():
return
from ereuseapi.methods import register_user
from modules.trublo.utils import encrypt
api_dlt = app.config.get('API_DLT')
data = register_user(api_dlt)
data = json.dumps(data)
self.api_keys_dlt = encrypt(password, data)
def get_dlt_keys(self, password):
if 'trublo' not in app.blueprints.keys():
return {}
from modules.trublo.utils import decrypt
data = decrypt(password, self.api_keys_dlt)
return json.loads(data)
def reset_dlt_keys(self, password, data):
if 'trublo' not in app.blueprints.keys():
return
from modules.trublo.utils import encrypt
data = json.dumps(data)
self.api_keys_dlt = encrypt(password, data)
class UserInventory(db.Model): class UserInventory(db.Model):
"""Relationship between users and their inventories.""" """Relationship between users and their inventories."""

View File

@ -1,7 +1,7 @@
import flask import flask
from flask import Blueprint from flask import Blueprint
from flask import current_app as app from flask import current_app as app
from flask import g from flask import g, session
from flask.views import View from flask.views import View
from flask_login import current_user, login_required, login_user, logout_user from flask_login import current_user, login_required, login_user, logout_user
from sqlalchemy import or_ from sqlalchemy import or_