Skip to content

Commit

Permalink
[IMP] fastapi_auth_partner: Add request param to set_auth_cookie for …
Browse files Browse the repository at this point in the history
…inheritance (checking anon partner for instance)
  • Loading branch information
paradoxxxzero committed Dec 12, 2024
1 parent 5692903 commit 1068822
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion fastapi_auth_partner/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __call__(
helper = env["fastapi.auth.service"].new(
{"endpoint_id": endpoint}
)
helper._set_auth_cookie(auth_partner, response)
helper._set_auth_cookie(auth_partner, request, response)
return partner
_logger.info("Could not determine partner from 'fastapi_auth_partner' cookie.")
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED)
Expand Down
2 changes: 1 addition & 1 deletion fastapi_auth_partner/models/auth_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def local_impersonate(self):

for endpoint in self.directory_id.fastapi_endpoint_ids:
helper = self.env["fastapi.auth.service"].new({"endpoint_id": endpoint})
helper._set_auth_cookie(self, request.future_response)
helper._set_auth_cookie(self, request, request.future_response)

return {
"type": "ir.actions.client",
Expand Down
16 changes: 10 additions & 6 deletions fastapi_auth_partner/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from odoo.addons.fastapi.dependencies import fastapi_endpoint, odoo_env
from odoo.addons.fastapi.models import FastapiEndpoint

from fastapi import APIRouter, Depends, Response
from fastapi import APIRouter, Depends, Request, Response
from fastapi.responses import RedirectResponse

from ..dependencies import auth_partner_authenticated_partner
Expand All @@ -45,11 +45,12 @@ def register(
data: AuthRegisterInput,
env: Annotated[Environment, Depends(odoo_env)],
endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)],
request: Request,
response: Response,
) -> AuthPartnerResponse:
helper = env["fastapi.auth.service"].new({"endpoint_id": endpoint})
auth_partner = helper._signup(data)
helper._set_auth_cookie(auth_partner, response)
helper._set_auth_cookie(auth_partner, request, response)
return AuthPartnerResponse.from_auth_partner(auth_partner)


Expand All @@ -58,11 +59,12 @@ def login(
data: AuthLoginInput,
env: Annotated[Environment, Depends(odoo_env)],
endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)],
request: Request,
response: Response,
) -> AuthPartnerResponse:
helper = env["fastapi.auth.service"].new({"endpoint_id": endpoint})
auth_partner = helper._login(data)
helper._set_auth_cookie(auth_partner, response)
helper._set_auth_cookie(auth_partner, request, response)
return AuthPartnerResponse.from_auth_partner(auth_partner)


Expand Down Expand Up @@ -105,11 +107,12 @@ def set_password(
data: AuthSetPasswordInput,
env: Annotated[Environment, Depends(odoo_env)],
endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)],
request: Request,
response: Response,
) -> AuthPartnerResponse:
helper = env["fastapi.auth.service"].new({"endpoint_id": endpoint})
auth_partner = helper._set_password(data)
helper._set_auth_cookie(auth_partner, response)
helper._set_auth_cookie(auth_partner, request, response)
return AuthPartnerResponse.from_auth_partner(auth_partner)


Expand All @@ -129,6 +132,7 @@ def impersonate(
token: str,
env: Annotated[Environment, Depends(odoo_env)],
endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)],
request: Request,
) -> RedirectResponse:
helper = env["fastapi.auth.service"].new({"endpoint_id": endpoint})
auth_partner = helper._impersonate(token)
Expand All @@ -141,7 +145,7 @@ def impersonate(
)
)
response = RedirectResponse(url=base)
helper._set_auth_cookie(auth_partner, response)
helper._set_auth_cookie(auth_partner, request, response)
return response


Expand Down Expand Up @@ -239,7 +243,7 @@ def _prepare_cookie(self, partner):
vals["secure"] = False
return vals

def _set_auth_cookie(self, auth_partner, response):
def _set_auth_cookie(self, auth_partner, request, response):
response.set_cookie(
COOKIE_AUTH_NAME, **self.sudo()._prepare_cookie(auth_partner.partner_id)
)
Expand Down

0 comments on commit 1068822

Please sign in to comment.