15
15
oauth2_scheme = OAuth2PasswordBearer (tokenUrl = "/authentication/token" )
16
16
17
17
18
+ async def get_current_community (
19
+ request : Request ,
20
+ token : Annotated [str , Depends (oauth2_scheme )],
21
+ ) -> DBCommunity :
22
+ credentials_exception = HTTPException (
23
+ status_code = status .HTTP_401_UNAUTHORIZED ,
24
+ detail = "Could not validate credentials" ,
25
+ headers = {"WWW-Authenticate" : "Bearer" },
26
+ )
27
+
28
+ try :
29
+ payload = jwt .decode (
30
+ token , auth .SECRET_KEY , algorithms = [auth .ALGORITHM ]
31
+ )
32
+ username = payload .get ("sub" )
33
+ if username is None :
34
+ raise credentials_exception
35
+ token_data = TokenPayload (username = username )
36
+ except InvalidTokenError :
37
+ raise credentials_exception
38
+ session : AsyncSession = request .app .db_session_factory
39
+ community = await get_community_by_username (
40
+ session = session , username = token_data .username
41
+ )
42
+ if community is None :
43
+ raise credentials_exception
44
+
45
+ return community
46
+
47
+
48
+ async def get_current_active_community (
49
+ current_user : Annotated [DBCommunity , Depends (get_current_community )],
50
+ ) -> DBCommunity :
51
+ # A função simplesmente retorna o usuário.
52
+ # Pode ser estendido futuramente para verificar um status "ativo".
53
+ return current_user
54
+
55
+
18
56
def setup ():
19
57
router = APIRouter (prefix = "/authentication" , tags = ["authentication" ])
20
58
@@ -32,43 +70,6 @@ async def authenticate_community(
32
70
return None
33
71
return found_community
34
72
35
- # Teste
36
- async def get_current_community (
37
- request : Request ,
38
- token : Annotated [str , Depends (oauth2_scheme )],
39
- ) -> DBCommunity :
40
- credentials_exception = HTTPException (
41
- status_code = status .HTTP_401_UNAUTHORIZED ,
42
- detail = "Could not validate credentials" ,
43
- headers = {"WWW-Authenticate" : "Bearer" },
44
- )
45
-
46
- try :
47
- payload = jwt .decode (
48
- token , auth .SECRET_KEY , algorithms = [auth .ALGORITHM ]
49
- )
50
- username = payload .get ("sub" )
51
- if username is None :
52
- raise credentials_exception
53
- token_data = TokenPayload (username = username )
54
- except InvalidTokenError :
55
- raise credentials_exception
56
- session : AsyncSession = request .app .db_session_factory
57
- community = await get_community_by_username (
58
- session = session , username = token_data .username
59
- )
60
- if community is None :
61
- raise credentials_exception
62
-
63
- return community
64
-
65
- async def get_current_active_community (
66
- current_user : Annotated [DBCommunity , Depends (get_current_community )],
67
- ) -> DBCommunity :
68
- # A função simplesmente retorna o usuário.
69
- # Pode ser estendido futuramente para verificar um status "ativo".
70
- return current_user
71
-
72
73
# Teste
73
74
74
75
@router .post ("/create_commumity" )
0 commit comments