Skip to content

Commit 91ed8ca

Browse files
frascuchonjfcalvopre-commit-ci[bot]
authored
[REFACTOR] argilla server: Remove passlib dependency (#5674)
# Description <!-- Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Closes #5664 **Type of change** <!-- Please delete options that are not relevant. Remember to title the PR according to the type of change --> - Improvement (change adding some improvement to an existing functionality) **How Has This Been Tested** <!-- Please add some reference about how your feature has been tested. --> **Checklist** <!-- Please go over the list and make sure you've taken everything into account --> - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/) --------- Co-authored-by: José Francisco Calvo <[email protected]> Co-authored-by: José Francisco Calvo <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f9ab910 commit 91ed8ca

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

argilla-server/pdm.lock

Lines changed: 1 addition & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

argilla-server/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies = [
3939
"PyYAML >= 5.4.1,< 6.1.0",
4040
# security dependencies
4141
"python-jose[cryptography] ~= 3.3.0",
42-
"passlib[bcrypt] ~= 1.7.4",
42+
"bcrypt ~= 4.2.0",
4343
# required by fastapi
4444
"python-multipart ~= 0.0.16",
4545
# OAuth2 integration

argilla-server/src/argilla_server/contexts/accounts.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Iterable, List, Sequence, Union
1616
from uuid import UUID
1717

18-
from passlib.context import CryptContext
18+
import bcrypt
1919
from sqlalchemy import exists, select
2020
from sqlalchemy.ext.asyncio import AsyncSession
2121
from sqlalchemy.orm import selectinload
@@ -27,8 +27,6 @@
2727
from argilla_server.security.authentication.jwt import JWT
2828
from argilla_server.security.authentication.userinfo import UserInfo
2929

30-
_CRYPT_CONTEXT = CryptContext(schemes=["bcrypt"], deprecated="auto")
31-
3230

3331
async def create_workspace_user(db: AsyncSession, workspace_user_attrs: dict) -> WorkspaceUser:
3432
workspace_id = workspace_user_attrs["workspace_id"]
@@ -168,19 +166,21 @@ async def authenticate_user(db: AsyncSession, username: str, password: str):
168166
elif user:
169167
return
170168
else:
171-
_CRYPT_CONTEXT.dummy_verify()
169+
_dummy_verify()
172170

173171

174172
def hash_password(password: str) -> str:
175-
return _CRYPT_CONTEXT.hash(password)
173+
return bcrypt.hashpw(
174+
bytes(password, encoding="utf-8"),
175+
bcrypt.gensalt(),
176+
).decode("utf-8")
176177

177178

178179
def verify_password(password: str, password_hash: str) -> bool:
179-
return _CRYPT_CONTEXT.verify(password, password_hash)
180-
181-
182-
def _generate_random_password() -> str:
183-
return secrets.token_urlsafe()
180+
return bcrypt.checkpw(
181+
bytes(password, encoding="utf-8"),
182+
bytes(password_hash, encoding="utf-8"),
183+
)
184184

185185

186186
def generate_user_token(user: User) -> str:
@@ -192,3 +192,15 @@ def generate_user_token(user: User) -> str:
192192
role=user.role,
193193
),
194194
)
195+
196+
197+
_DUMMY_SECRET = "dummy_secret"
198+
_DUMMY_HASH = hash_password(_DUMMY_SECRET)
199+
200+
201+
def _dummy_verify():
202+
verify_password(_DUMMY_SECRET, _DUMMY_HASH)
203+
204+
205+
def _generate_random_password() -> str:
206+
return secrets.token_urlsafe()

0 commit comments

Comments
 (0)