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
8 changes: 4 additions & 4 deletions api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from fastapi_users.jwt import SecretType, decode_jwt
from fastapi_users.manager import BaseUserManager
from passlib.context import CryptContext
import bcrypt

from .config import AuthSettings

Expand Down Expand Up @@ -96,16 +96,16 @@ async def _decode_and_lookup(
class Authentication:
"""Authentication utility class"""

CRYPT_CTX = CryptContext(schemes=["bcrypt"], deprecated="auto")

def __init__(self, token_url: str):
self._settings = AuthSettings()
self._token_url = token_url

@classmethod
def get_password_hash(cls, password):
"""Get a password hash for a given clear text password string"""
return cls.CRYPT_CTX.hash(password)
return bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt()
).decode("utf-8")

def get_jwt_strategy(self) -> DualSecretJWTStrategy:
"""Get JWT strategy for authentication backend"""
Expand Down
2 changes: 1 addition & 1 deletion docker/api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fastapi-users[beanie, oauth]==15.0.5
fastapi-users-db-beanie==5.0.0
MarkupSafe==2.0.1
pymongo==4.16.0
passlib==1.7.4
bcrypt==5.0.0
pydantic==2.9.2
pymongo-migrate==0.11.0
python-jose[cryptography]==3.5.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies = [
"fastapi-users-db-beanie == 5.0.0",
"MarkupSafe == 3.0.3",
"pymongo == 4.17.0",
"passlib == 1.7.4",
"bcrypt == 5.0.0",
"pydantic == 2.13.4",
"pymongo-migrate == 1.0.0",
"python-jose[cryptography] == 3.5.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_tests/test_token_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pytest

from api.auth import Authentication
from api.models import User


Expand Down Expand Up @@ -74,3 +75,8 @@ async def test_token_endpoint_incorrect_password(
print("response json", response.json())
assert response.status_code == 400
assert response.json() == {"detail": "LOGIN_BAD_CREDENTIALS"}


def test_get_password_hash():
hashed = Authentication.get_password_hash("test-password")
assert hashed.startswith("$2b$12$")