Skip to content

Commit

Permalink
feat(authentication): add safe get user
Browse files Browse the repository at this point in the history
  • Loading branch information
tepelbaum committed Feb 7, 2024
1 parent ff08e21 commit 52aed15
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 305 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2024 Thomas EPELBAUM
Copyright 2024 Thomas EPELBAUM for EcoAct

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
Expand Down
4 changes: 2 additions & 2 deletions ecodev_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ecodev_core.authentication import is_authorized_user
from ecodev_core.authentication import is_monitoring_user
from ecodev_core.authentication import JwtAuth
from ecodev_core.authentication import safe_get_user
from ecodev_core.authentication import SCHEME
from ecodev_core.authentication import Token
from ecodev_core.check_dependencies import check_dependencies
Expand Down Expand Up @@ -75,5 +76,4 @@
'enum_converter', 'ServerSideFilter', 'get_rows', 'count_rows', 'ServerSideField', 'get_raw_df',
'generic_insertion', 'custom_equal', 'is_authorized_user', 'get_method', 'AppActivity',
'fastapi_monitor', 'dash_monitor', 'is_monitoring_user', 'get_recent_activities', 'select_user',
'get_access_token'
]
'get_access_token', 'safe_get_user']
10 changes: 10 additions & 0 deletions ecodev_core/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ def is_authorized_user(token: str = Depends(SCHEME)) -> bool:
return False


def safe_get_user(token: Dict) -> Union[AppUser, None]:
"""
Safe method returning a user if one found given the passed token
"""
try:
return get_user(get_access_token(token))
except (HTTPException, AttributeError):
return None


def get_user(token: str = Depends(SCHEME)) -> AppUser:
"""
Retrieves (if it exists) the db user corresponding to the passed token
Expand Down
2 changes: 1 addition & 1 deletion ecodev_core/db_insertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Insertor(CustomFrozen):
- selector: the criteria on which to decide whether to create or update (example: only add
a user if a user with the same name is not already present in the db)
- convertor: how to convert the raw csv/excel passed by the user to json like db rows
- whether to insert data based on an xlsx (if true) or a csv (if false)
- read_excel_file: whether to insert data based on an xlsx (if true) or a csv (if false)
"""
reductor: Callable[[Any, Any], Any]
db_schema: Callable
Expand Down
597 changes: 297 additions & 300 deletions poetry.lock

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
**Type of change**
- [ ] Feature
- [ ] Bugfix
- [ ] Refactoring
- [ ] Documentation

-----------------------

**Context**

>_What does this PR implement and how? What are the requirements?_
>_Why is this change needed? Related issue #?_
>_Any diagrams or screenshot to help reviewers?_
>_Please describe the tests you have performed to ensure the feature or fix are robust & effective?_

Your comments


-----------------------

**What should the reviewer focus on?**
>_Do you have specific questions/ areas for the reviewer?_
>_Which alternative solutions have you already considered and why did you not implement it?_
>_Where is the highest risk/ most complicated change that the reviewer should focus on?_
>_Is there a specific code review topic the reviewer should focus on (e.g., correct error handling, API usage)_
>_What is a good place to start the review (e.g., specific file/method?)_

Your comments


-----------------------

**For reviewer:**

[PR Review guidelines](https://google.github.io/eng-practices/review/reviewer/standard.html)
14 changes: 14 additions & 0 deletions tests/functional/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from ecodev_core.authentication import is_authorized_user
from ecodev_core.authentication import JwtAuth
from ecodev_core.authentication import MONITORING_ERROR
from ecodev_core.authentication import safe_get_user


DATA_DIR = Path('/app/tests/unitary/data')
Expand Down Expand Up @@ -157,6 +158,19 @@ def test_standard_auth(self):
self.assertEqual(client.user, 'client')
self.assertTrue(isinstance(client, AppUser))

def test_safe_get_user(self):
"""
Test that safe user retrieval works as expected
"""
with Session(engine) as session:
token = {'token': attempt_to_log('client', 'client', session)}
client = safe_get_user(token)
self.assertEqual(client.user, 'client')
self.assertTrue(isinstance(client, AppUser))

wrong = safe_get_user({'token': 'toto'})
self.assertTrue(wrong is None)

def test_admin_auth(self):
"""
Test that admin authentication works as expected
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_db_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


DATA_DIR = Path('/app/tests/unitary/data')
APP_FILTER = ServerSideField(col_name='user', field_name='field_name', field=AppUser.user,
APP_FILTER = ServerSideField(col_name='user', field_name='user', field=AppUser.user,
filter=ServerSideFilter.ILIKESTR)


Expand Down

0 comments on commit 52aed15

Please sign in to comment.