File tree 5 files changed +75
-17
lines changed
5 files changed +75
-17
lines changed Original file line number Diff line number Diff line change 17
17
templates = Jinja2Templates (directory = TEMPLATES_PATH )
18
18
templates .env .add_extension ("jinja2.ext.i18n" )
19
19
20
+
20
21
# Configure logger
21
22
logger = LoggerCustomizer .make_logger (
22
23
config .LOG_PATH ,
Original file line number Diff line number Diff line change
1
+ from typing import Optional
2
+
1
3
from fastapi import Depends , HTTPException
2
4
from starlette .requests import Request
3
5
from starlette .status import HTTP_401_UNAUTHORIZED
@@ -90,3 +92,21 @@ async def current_user(
90
92
detail = "Your token is not valid. Please log in again" ,
91
93
)
92
94
return schema .CurrentUser (user_id = user_id , username = username )
95
+
96
+
97
+ def get_jinja_current_user (request : Request ) -> Optional [schema .CurrentUser ]:
98
+ """Return the currently logged in user.
99
+ Returns logged in User object if exists, None if not.
100
+ Set as a jinja global parameter.
101
+ """
102
+ if "Authorization" not in request .cookies :
103
+ return None
104
+ jwt_payload = get_jwt_token (request .cookies ["Authorization" ])
105
+ username = jwt_payload .get ("sub" )
106
+ user_id = jwt_payload .get ("user_id" )
107
+ if not user_id :
108
+ raise HTTPException (
109
+ status_code = HTTP_401_UNAUTHORIZED ,
110
+ detail = "Your token is not valid. Please log in again" ,
111
+ )
112
+ return schema .CurrentUser (user_id = user_id , username = username )
Original file line number Diff line number Diff line change 6
6
from fastapi .staticfiles import StaticFiles
7
7
from sqlalchemy .orm import Session
8
8
9
+ import app .internal .features as internal_features
9
10
from app import config
10
11
from app .database import engine , models
11
12
from app .dependencies import (
12
13
MEDIA_PATH ,
13
14
SOUNDS_PATH ,
14
15
STATIC_PATH ,
15
16
UPLOAD_PATH ,
17
+ SessionLocal ,
16
18
get_db ,
17
19
logger ,
18
20
templates ,
19
- SessionLocal ,
20
21
)
21
22
from app .internal import daily_quotes , json_data_loader
22
- import app .internal .features as internal_features
23
23
from app .internal .languages import set_ui_language
24
+ from app .internal .security .dependencies import get_jinja_current_user
24
25
from app .internal .security .ouath2 import auth_exception_handler
25
26
from app .routers .salary import routes as salary
26
27
from app .utils .extending_openapi import custom_openapi
@@ -51,6 +52,7 @@ def create_tables(engine, psql_environment):
51
52
app .logger = logger
52
53
53
54
app .add_exception_handler (status .HTTP_401_UNAUTHORIZED , auth_exception_handler )
55
+ templates .env .globals ["jinja_current_user" ] = get_jinja_current_user
54
56
55
57
# This MUST come before the app.routers imports.
56
58
set_ui_language ()
Original file line number Diff line number Diff line change 31
31
</ div >
32
32
< div class ="collapse navbar-collapse " id ="navbarToggler ">
33
33
< ul class ="navbar-nav mr-auto mb-2 mb-lg-0 ">
34
- < li class ="nav-item ">
35
- < a class ="nav-link " href ="{{ url_for('profile') }} "> Profile</ a >
36
- </ li >
37
- < li class ="nav-item ">
38
- < a class ="nav-link " href ="{{ url_for('login') }} "> Sign In</ a >
39
- </ li >
40
- < li class ="nav-item ">
41
- < a class ="nav-link " href ="{{ url_for('logout') }} "> {{ gettext("Sign Out") }}</ a >
42
- </ li >
43
- < li class ="nav-item ">
44
- < a class ="nav-link " href ="{{ url_for('register') }} "> Sign Up</ a >
45
- </ li >
46
- < li class ="nav-item ">
47
- < a class ="nav-link " href ="{{ url_for('agenda') }} "> Agenda</ a >
48
- </ li >
34
+ {% if jinja_current_user(request) %}
35
+ < li class ="nav-item ">
36
+ < a class ="nav-link " href ="{{ url_for('profile') }} "> Profile</ a >
37
+ </ li >
38
+ < li class ="nav-item ">
39
+ < a class ="nav-link " href ="{{ url_for('logout') }} "> Sign Out</ a >
40
+ </ li >
41
+ < li class ="nav-item ">
42
+ < a class ="nav-link " href ="{{ url_for('agenda') }} "> Agenda</ a >
43
+ </ li >
44
+ {% else %}
45
+ < li class ="nav-item ">
46
+ < a class ="nav-link " href ="{{ url_for('login') }} "> Sign In</ a >
47
+ </ li >
48
+ < li class ="nav-item ">
49
+ < a class ="nav-link " href ="{{ url_for('register') }} "> Sign Up</ a >
50
+ </ li >
51
+ {% endif %}
49
52
< li class ="nav-item ">
50
53
< a class ="nav-link " href ="{{ url_for( 'audio_settings') }} "> Audio Settings</ a >
51
54
</ li >
Original file line number Diff line number Diff line change
1
+ REGISTER_DETAIL = {
2
+ "username" : "correct_user" ,
3
+ "full_name" : "full_name" ,
4
+ "password" : "correct_password" ,
5
+ "confirm_password" : "correct_password" ,
6
+
7
+ "description" : "" ,
8
+ }
9
+
10
+ LOGIN_DATA = {"username" : "correct_user" , "password" : "correct_password" }
11
+
12
+
13
+ def test_user_not_logged_in (session , security_test_client ):
14
+ security_test_client .get (security_test_client .app .url_path_for ("logout" ))
15
+ response = security_test_client .get ("/about" )
16
+ assert b"Sign Out" not in response .content
17
+ assert b"Sign In" in response .content
18
+
19
+
20
+ def test_user_is_logged_in (session , security_test_client ):
21
+ security_test_client .get (security_test_client .app .url_path_for ("logout" ))
22
+ security_test_client .post (
23
+ security_test_client .app .url_path_for ("register" ),
24
+ data = REGISTER_DETAIL ,
25
+ )
26
+ security_test_client .post (
27
+ security_test_client .app .url_path_for ("login" ),
28
+ data = LOGIN_DATA ,
29
+ )
30
+ response = security_test_client .get ("/about" )
31
+ assert b"Sign Out" in response .content
32
+ assert b"Sign In" not in response .content
You can’t perform that action at this time.
0 commit comments