correção de parsing da chave
This commit is contained in:
@@ -10,12 +10,24 @@ class UserAuth(BaseModel):
|
|||||||
id: str
|
id: str
|
||||||
token: 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:
|
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> UserAuth:
|
||||||
token = credentials.credentials
|
token = credentials.credentials
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# A chave pública pode vir com '\n' escapado do .env
|
# Garante que a chave terá o formato PEM válido, independentemente de como foi definida no .env
|
||||||
public_key = settings.JWT_PUBLIC_KEY_PEM.replace('\\n', '\n')
|
public_key = format_public_key(settings.JWT_PUBLIC_KEY_PEM)
|
||||||
|
|
||||||
payload = jwt.decode(
|
payload = jwt.decode(
|
||||||
token,
|
token,
|
||||||
|
|||||||
Reference in New Issue
Block a user