adicionado timeout no login e botão de Sair
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Numeric, Date, ForeignKey
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Numeric, Date, ForeignKey, DateTime
|
||||
from sqlalchemy.orm import relationship, sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
@@ -190,42 +190,53 @@ class Usuario(Base):
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
username = Column(String(50), unique=True, nullable=False)
|
||||
password_hash = Column(String(255), nullable=False)
|
||||
email = Column(String(100), unique=True, nullable=True)
|
||||
otp_secret = Column(String(32), nullable=True)
|
||||
role_id = Column(Integer, ForeignKey('roles.id'), nullable=True)
|
||||
setor_id = Column(Integer, ForeignKey('setores.id'), nullable=True)
|
||||
email = Column(String(100), unique=True, nullable=False)
|
||||
otp_secret = Column(String(32))
|
||||
role_id = Column(Integer, ForeignKey('roles.id'))
|
||||
setor_id = Column(Integer, ForeignKey('setores.id'))
|
||||
ativo = Column(Boolean, default=True)
|
||||
is_admin = Column(Boolean, default=False)
|
||||
|
||||
ultimo_login = Column(DateTime)
|
||||
ultimo_logout = Column(DateTime)
|
||||
motivo_logout = Column(String(100))
|
||||
|
||||
role = relationship("Role", back_populates="usuarios")
|
||||
setor = relationship("Setor", back_populates="usuarios")
|
||||
|
||||
def __init__(self, username, password, is_admin=False):
|
||||
self.username = username
|
||||
self.set_password(password)
|
||||
self.otp_secret = pyotp.random_base32()
|
||||
self.password_hash = generate_password_hash(password)
|
||||
self.is_admin = is_admin
|
||||
self.otp_secret = pyotp.random_base32() # Gerar segredo OTP na criação
|
||||
self.ativo = True
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def check_password(self, password):
|
||||
result = check_password_hash(self.password_hash, password)
|
||||
print(f"Verificação de senha para {self.username}: {'sucesso' if result else 'falha'}")
|
||||
return result
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def verify_otp(self, otp_code):
|
||||
"""Verifica se o código OTP fornecido é válido"""
|
||||
if not self.otp_secret:
|
||||
print(f"Erro: Usuário {self.username} não tem segredo OTP configurado")
|
||||
return False
|
||||
|
||||
totp = pyotp.TOTP(self.otp_secret)
|
||||
result = totp.verify(otp_code)
|
||||
print(f"Verificação OTP para {self.username}")
|
||||
is_valid = totp.verify(otp_code)
|
||||
print(f"Verificando OTP para {self.username}")
|
||||
print(f"Segredo: {self.otp_secret}")
|
||||
print(f"Código fornecido: {otp_code}")
|
||||
print(f"Resultado da verificação: {'válido' if result else 'inválido'}")
|
||||
return result
|
||||
print(f"Resultado: {'válido' if is_valid else 'inválido'}")
|
||||
return is_valid
|
||||
|
||||
def get_otp_uri(self):
|
||||
"""Gera a URI para o QR code do OTP"""
|
||||
if not self.otp_secret:
|
||||
self.otp_secret = pyotp.random_base32()
|
||||
|
||||
totp = pyotp.TOTP(self.otp_secret)
|
||||
return totp.provisioning_uri(self.username, issuer_name="Sistema de Gestão")
|
||||
return totp.provisioning_uri(
|
||||
name=self.username,
|
||||
issuer_name="Sistema de Gestão"
|
||||
)
|
||||
|
||||
class Role(Base):
|
||||
__tablename__ = 'roles'
|
||||
|
||||
Reference in New Issue
Block a user