Skip to content

Commit

Permalink
Merge PR #1482 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by sbidoul
  • Loading branch information
shopinvader-git-bot committed Mar 14, 2024
2 parents 6a31ecf + 77fc96a commit 3110630
Show file tree
Hide file tree
Showing 15 changed files with 734 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/shopinvader_api_signin_jwt/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
67 changes: 67 additions & 0 deletions shopinvader_api_signin_jwt/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
==========================
Shopinvader Api Signin JWT
==========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0c78d267b23414519ffd0969275305ffdcffeb4dcea4e76f63c2d8d3d0d35735
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-shopinvader%2Fodoo--shopinvader-lightgray.png?logo=github
:target: https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_api_signin_jwt
:alt: shopinvader/odoo-shopinvader

|badge1| |badge2| |badge3|

This addon adds a web API to signin into the application and create a partner
if the email in the jwt payload is unknown.

**Table of contents**

.. contents::
:local:

Known issues / Roadmap
======================

* Manage anonymous cart (see https://github.com/shopinvader/odoo-shopinvader/issues/1428)
* Use ``fastapi_auth_jwt.auth_jwt_authenticated_odoo_env`` dependency for the env (see https://github.com/OCA/rest-framework/issues/406)

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/shopinvader/odoo-shopinvader/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/shopinvader/odoo-shopinvader/issues/new?body=module:%20shopinvader_api_signin_jwt%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* ACSONE SA/NV

Contributors
~~~~~~~~~~~~

* Quentin Groulard <[email protected]>

Maintainers
~~~~~~~~~~~

This module is part of the `shopinvader/odoo-shopinvader <https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_api_signin_jwt>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions shopinvader_api_signin_jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import routers
18 changes: 18 additions & 0 deletions shopinvader_api_signin_jwt/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Shopinvader Api Signin JWT",
"summary": """This module adds a signin service with jwt token.""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV",
"website": "https://github.com/shopinvader/odoo-shopinvader",
"depends": ["fastapi_auth_jwt"],
"data": [
"security/res_groups.xml",
"security/acl_res_partner.xml",
],
"demo": [],
"installable": True,
}
1 change: 1 addition & 0 deletions shopinvader_api_signin_jwt/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Quentin Groulard <[email protected]>
2 changes: 2 additions & 0 deletions shopinvader_api_signin_jwt/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This addon adds a web API to signin into the application and create a partner
if the email in the jwt payload is unknown.
2 changes: 2 additions & 0 deletions shopinvader_api_signin_jwt/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Manage anonymous cart (see https://github.com/shopinvader/odoo-shopinvader/issues/1428)
* Use ``fastapi_auth_jwt.auth_jwt_authenticated_odoo_env`` dependency for the env (see https://github.com/OCA/rest-framework/issues/406)
1 change: 1 addition & 0 deletions shopinvader_api_signin_jwt/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .signin import signin_router
87 changes: 87 additions & 0 deletions shopinvader_api_signin_jwt/routers/signin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2023 ACSONE SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
from typing import Annotated, Union

from fastapi import APIRouter, Depends, Response, status

from odoo import api, models

from odoo.addons.base.models.res_partner import Partner
from odoo.addons.fastapi.dependencies import odoo_env
from odoo.addons.fastapi_auth_jwt.dependencies import (
Payload,
auth_jwt_authenticated_payload,
auth_jwt_default_validator_name,
auth_jwt_optionally_authenticated_partner,
)

_logger = logging.getLogger(__name__)

signin_router = APIRouter(tags=["signin"])


@signin_router.post("/signin", status_code=200)
def signin(
env: Annotated[api.Environment, Depends(odoo_env)],
partner: Annotated[Partner, Depends(auth_jwt_optionally_authenticated_partner)],
payload: Annotated[Payload, Depends(auth_jwt_authenticated_payload)],
response: Response,
) -> None:
"""
Authenticate the partner based on a JWT token or a session cookie.
Set the session cookie if allowed.
Return HTTP code 201 if res.partner created (case of the first signin).
"""
if not partner:
env[
"shopinvader_api_signin_jwt.signin_router.helper"
]._create_partner_from_payload(payload)
response.status_code = status.HTTP_201_CREATED


@signin_router.post("/signout")
def signout(
env: Annotated[api.Environment, Depends(odoo_env)],
default_validator_name: Annotated[
Union[str, None], Depends(auth_jwt_default_validator_name)
],
response: Response,
) -> None:
"""
Remove the session cookie.
"""
validator = (
env["auth.jwt.validator"].sudo()._get_validator_by_name(default_validator_name)
)
if not validator:
_logger.info("No validator found with name '%s'", default_validator_name)
return
if not validator.cookie_name:
_logger.info("Cookie name not set for validator %s", validator.name)
return
response.delete_cookie(
key=validator.cookie_name,
path=validator.cookie_path or "/",
secure=validator.cookie_secure,
httponly=True,
)


class ShopinvaderApSigninJwtRouterHelper(models.AbstractModel):
_name = "shopinvader_api_signin_jwt.signin_router.helper"
_description = "ShopInvader API Signin Jwt Router Helper"

@api.model
def _get_partner_create_vals(self, payload: Payload):
return {"name": payload.get("name"), "email": payload.get("email")}

@api.model
def _create_partner_from_payload(self, payload: Payload):
partner = (
self.env["res.partner"]
.sudo()
.create(self._get_partner_create_vals(payload))
)
return self.env["res.partner"].browse(partner.id)
15 changes: 15 additions & 0 deletions shopinvader_api_signin_jwt/security/acl_res_partner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="shopinvader_signin_res_partner_access" model="ir.model.access">
<field name="name">Shopinvader Signin: user read/write/create partners</field>
<field name="model_id" ref="base.model_res_partner" />
<field name="group_id" ref="shopinvader_signin_user_group" />
<field name="perm_read" eval="1" />
<field name="perm_write" eval="1" />
<field name="perm_create" eval="1" />
<field name="perm_unlink" eval="0" />
</record>

</odoo>
15 changes: 15 additions & 0 deletions shopinvader_api_signin_jwt/security/res_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="shopinvader_signin_user_group" model="res.groups">
<field name="name">Shopinvader Signin user</field>
<field
name="implied_ids"
eval="[
(4, ref('fastapi.group_fastapi_endpoint_runner')),
]"
/>
</record>

</odoo>
Loading

0 comments on commit 3110630

Please sign in to comment.