Nuevos cambios de la implementacion token interno de dolibarr a los microservicios
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Depends
|
||||||
from Back_comercial_iko.services.Comercial_service import ComercialService
|
from Back_comercial_iko.services.Comercial_service import ComercialService
|
||||||
from Back_comercial_iko.core.Response import ApiResponse
|
from Back_comercial_iko.core.Response import ApiResponse
|
||||||
from Back_comercial_iko.core.HttpStatus import HttpStatus
|
from Back_comercial_iko.core.HttpStatus import HttpStatus
|
||||||
|
from Back_comercial_iko.core.AuthTokenDoli import TokenDoliManager
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/comercial", tags=["Comercial"])
|
router = APIRouter(prefix="/comercial", tags=["Comercial"])
|
||||||
|
|
||||||
@router.post("/create-project")
|
@router.post("/create-project")
|
||||||
def create_project(data: dict):
|
def create_project(data: dict, token_data: dict = Depends(TokenDoliManager.verify_header)):
|
||||||
try:
|
try:
|
||||||
service = ComercialService()
|
service = ComercialService()
|
||||||
result = service.create_project_commercial(data)
|
result = service.create_project_commercial(data)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import jwt
|
import jwt
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
from core.Config import settings
|
from Back_comercial_iko.core.Config import settings
|
||||||
|
|
||||||
|
|
||||||
class TokenManager:
|
class TokenManager:
|
||||||
|
|||||||
45
Back_comercial_iko/core/AuthTokenDoli.py
Normal file
45
Back_comercial_iko/core/AuthTokenDoli.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
import hmac
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
from typing import Dict, Any
|
||||||
|
from fastapi import Header, HTTPException, Depends
|
||||||
|
from Back_comercial_iko.core.Config import settings
|
||||||
|
|
||||||
|
class TokenDoliManager:
|
||||||
|
"""
|
||||||
|
Manages the generation and validation of Dolibarr session tokens.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def verify(cls, token: str) -> Dict[str, Any]:
|
||||||
|
try:
|
||||||
|
payload_b64, signature = token.split(".")
|
||||||
|
|
||||||
|
expected_signature = hmac.new(
|
||||||
|
settings.token_secret_key.encode(),
|
||||||
|
payload_b64.encode(),
|
||||||
|
hashlib.sha256
|
||||||
|
).hexdigest()
|
||||||
|
|
||||||
|
if not hmac.compare_digest(signature, expected_signature):
|
||||||
|
raise ValueError("Firma inválida")
|
||||||
|
|
||||||
|
payload_json = base64.b64decode(payload_b64 + "===").decode()
|
||||||
|
payload = json.loads(payload_json)
|
||||||
|
|
||||||
|
if payload["exp"] < time.time():
|
||||||
|
raise ValueError("Token expirado")
|
||||||
|
|
||||||
|
return payload
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
raise ValueError("Token inválido")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def verify_header(cls, token: str = Header(...)) -> Dict[str, Any]:
|
||||||
|
try:
|
||||||
|
return cls.verify(token)
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=401, detail=str(e))
|
||||||
@@ -16,6 +16,7 @@ class ApiResponse:
|
|||||||
status_code=status_code,
|
status_code=status_code,
|
||||||
content={
|
content={
|
||||||
"success": True,
|
"success": True,
|
||||||
|
"status_code": status_code,
|
||||||
"message": message,
|
"message": message,
|
||||||
"data": data
|
"data": data
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user