Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions backend/btrixcloud/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from uuid import UUID, uuid4
from datetime import timedelta
from typing import Optional, Tuple, List
from passlib import pwd
from passlib.context import CryptContext
import string
import secrets
from pwdlib import PasswordHash
from pwdlib.hashers.argon2 import Argon2Hasher
from pwdlib.hashers.bcrypt import BcryptHasher

from pydantic import BaseModel
import jwt
Expand Down Expand Up @@ -38,7 +41,12 @@

RESET_VERIFY_TOKEN_LIFETIME_MINUTES = 60

PWD_CONTEXT = CryptContext(schemes=["bcrypt"], deprecated="auto")
PWD_CONTEXT = PasswordHash(
(
Argon2Hasher(),
BcryptHasher(),
)
)

# Audiences
CUSTOM_AUTH_AUD = "btrix:custom-auth"
Expand Down Expand Up @@ -163,7 +171,8 @@ def get_password_hash(password: str) -> str:
# ============================================================================
def generate_password() -> str:
"""generate new secure password"""
return pwd.genword()
alphabet = string.ascii_letters + string.digits
return "".join(secrets.choice(alphabet) for i in range(20))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I compared this to pwd.genword()'s defaults, just to make sure we were creating passwords of equivalent security: https://passlib.readthedocs.io/en/stable/lib/passlib.pwd.html#passlib.pwd.genword

Looks like we should be good. string.ascii_letters + string.digits is the same character set, and genword() defaults to 48 bits of entropy (9-character passwords), so the 20 character passwords here are an improvement.



# ============================================================================
Expand Down
4 changes: 2 additions & 2 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ gunicorn
uvicorn[standard]
fastapi==0.128.0
motor
passlib
pwdlib[argon2,bcrypt]
PyJWT==2.8.0
pydantic==2.12.5
email-validator
Expand All @@ -29,4 +29,4 @@ remotezip
json-stream
aiostream
iso639-lang>=2.6.0
setuptools<82.0.0
setuptools
Loading