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