Skip to content

Commit

Permalink
feat: token can pass in payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Valbou committed May 24, 2024
1 parent 2bdba04 commit 768f3a4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/libs/flask/utils/request_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ def get_bearer_token(request: Request) -> str | None:
return bearer


def get_payload_token(request: Request) -> str | None:
token = request.get_json().get("token")
return token


def get_auth_token(request: Request) -> str | None:
token = None
methods = [get_bearer_token, get_payload_token]

for method in methods:
token = method(request=request)
if token:
break

return token


def get_ip(request) -> str:
return (
request.environ.get("HTTP_X_FORWARDED_FOR")
Expand Down
4 changes: 2 additions & 2 deletions src/users/hmi/flask/api/v1/confirm_2fa.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta

from flask import current_app, request
from src.libs.flask.utils import ResponseAPI, get_bearer_token
from src.libs.flask.utils import ResponseAPI, get_auth_token
from src.libs.redis import rate_limited
from src.users.services import UserService

Expand Down Expand Up @@ -61,7 +61,7 @@ def confirm_2fa():
Return a 200
"""

sha_token = get_bearer_token(request)
sha_token = get_auth_token(request)
if not sha_token:
return ResponseAPI.get_401_response("Invalid token")

Expand Down
4 changes: 2 additions & 2 deletions src/users/hmi/flask/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Callable

from flask import current_app, g, request
from src.libs.flask.utils import ResponseAPI, get_bearer_token
from src.libs.flask.utils import ResponseAPI, get_auth_token
from src.libs.iam.config import PermissionError
from src.users.services import UserService

Expand All @@ -13,7 +13,7 @@ def decorator(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
try:
sha_token = get_bearer_token(request)
sha_token = get_auth_token(request)
if not sha_token:
return ResponseAPI.get_401_response("Invalid token")

Expand Down
2 changes: 1 addition & 1 deletion src/users/services/user_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Tuple

from src.libs.dependencies import DependencyInjector
from src.libs.iam.constants import Permissions
from src.libs.hmi.querystring import Filter
from src.libs.iam.constants import Permissions
from src.users.events import UsersEventService
from src.users.hmi.dto import UserDTO
from src.users.models import (
Expand Down

0 comments on commit 768f3a4

Please sign in to comment.