Skip to content
Merged
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
22 changes: 22 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,28 @@ async def whoami(current_user: User = Depends(get_user)):
return current_user


@app.post('/password')
async def reset_password(
username: str, current_password: Password, new_password: Password):
"""Set a new password for an authenticated user"""
authenticated = await auth.authenticate_user(
username, current_password.password.get_secret_value())
if not authenticated:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
)
Comment on lines +359 to +365
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks good atm. Just thinking if we can use dependency injection for the authentication here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add myself a TODO for this. Could that injection be also reused in the /token endpoint (that's where I first saw this flow)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, we can also optimize /token with that.


user = await db.find_one_by_attributes(
User, {'profile.username': username})
user.profile.hashed_password = auth.get_password_hash(
new_password.password.get_secret_value())
obj = await db.update(user)
await pubsub.publish_cloudevent('user', {'op': 'updated',
'id': str(obj.id)})
return obj


@app.post('/hash')
def get_password_hash(password: Password):
"""Get a password hash from the provided string password"""
Expand Down
34 changes: 34 additions & 0 deletions tests/e2e_tests/test_password_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2023 Collabora Limited
# Author: Paweł Wieczorek <pawel.wieczorek@collabora.com>

"""End-to-end test functions for KernelCI API password reset handler"""

import json
import pytest


@pytest.mark.dependency(
depends=["e2e_tests/test_user_creation.py::test_create_regular_user"],
scope="session",
)
@pytest.mark.order("last")
@pytest.mark.asyncio
async def test_password_endpoint(test_async_client):
"""
Test Case : Test KernelCI API /password endpoint to set a new password
when requested with current user's password
Expected Result :
HTTP Response Code 200 OK
"""
response = await test_async_client.post(
"password?username=test_user",
data=json.dumps(
{
"current_password": {"password": "test"},
"new_password": {"password": "foo"},
}
),
)
assert response.status_code == 200