From de6fa399561c65c4c63b379c16c69c4a16d1c2cb Mon Sep 17 00:00:00 2001 From: Luckaskl Date: Thu, 28 May 2026 14:18:54 -0500 Subject: [PATCH] =?UTF-8?q?corre=C3=A7=C3=A3o=20de=20parsing=20da=20chave?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/security.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/core/security.py b/app/core/security.py index b730aaa..4ef5107 100644 --- a/app/core/security.py +++ b/app/core/security.py @@ -10,12 +10,24 @@ class UserAuth(BaseModel): id: str token: str +def format_public_key(key: str) -> str: + key = key.replace('\\n', '\n') + header = "-----BEGIN PUBLIC KEY-----" + footer = "-----END PUBLIC KEY-----" + + if header in key and footer in key: + # Extrai o miolo, remove todos os espaços e quebras de linha + body = key.replace(header, "").replace(footer, "") + body = "".join(body.split()) + return f"{header}\n{body}\n{footer}" + return key + def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> UserAuth: token = credentials.credentials try: - # A chave pública pode vir com '\n' escapado do .env - public_key = settings.JWT_PUBLIC_KEY_PEM.replace('\\n', '\n') + # Garante que a chave terá o formato PEM válido, independentemente de como foi definida no .env + public_key = format_public_key(settings.JWT_PUBLIC_KEY_PEM) payload = jwt.decode( token,