|
| 1 | +# Copyright 2020 Creu Blanca |
| 2 | +# Copyright 2022 Camptocamp SA |
| 3 | +# @author Simone Orsi <simahawk@gmail.com> |
| 4 | +# @author Alexandre Fayolle <alexandre.fayolle@camptocamp.com> |
| 5 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). |
| 6 | +import logging |
| 7 | + |
| 8 | +from odoo import api, fields, models |
| 9 | +from odoo.tools import config |
| 10 | + |
| 11 | +_logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class WebserviceBackend(models.Model): |
| 15 | + _inherit = "webservice.backend" |
| 16 | + |
| 17 | + auth_type = fields.Selection(selection_add=[("oauth2", "OAuth2")]) |
| 18 | + oauth2_flow = fields.Selection( |
| 19 | + [ |
| 20 | + ("backend_application", "Backend Application (Client Credentials Grant)"), |
| 21 | + ("web_application", "Web Application (Authorization Code Grant)"), |
| 22 | + ], |
| 23 | + ) |
| 24 | + oauth2_clientid = fields.Char(string="Client ID", auth_type="oauth2") |
| 25 | + oauth2_client_secret = fields.Char(string="Client Secret", auth_type="oauth2") |
| 26 | + oauth2_token_url = fields.Char(string="Token URL", auth_type="oauth2") |
| 27 | + oauth2_authorization_url = fields.Char(string="Authorization URL") |
| 28 | + oauth2_audience = fields.Char( |
| 29 | + string="Audience" |
| 30 | + # no auth_type because not required |
| 31 | + ) |
| 32 | + oauth2_scope = fields.Char(help="scope of the the authorization") |
| 33 | + oauth2_token = fields.Char(help="the OAuth2 token (serialized JSON)") |
| 34 | + redirect_url = fields.Char( |
| 35 | + compute="_compute_redirect_url", |
| 36 | + help="The redirect URL to be used as part of the OAuth2 authorisation flow", |
| 37 | + ) |
| 38 | + oauth2_state = fields.Char( |
| 39 | + help="random key generated when authorization flow starts " |
| 40 | + "to ensure that no CSRF attack happen" |
| 41 | + ) |
| 42 | + |
| 43 | + def _get_adapter_protocol(self): |
| 44 | + protocol = super()._get_adapter_protocol() |
| 45 | + if self.auth_type.startswith("oauth2"): |
| 46 | + protocol += f"+{self.auth_type}-{self.oauth2_flow}" |
| 47 | + return protocol |
| 48 | + |
| 49 | + @api.depends("auth_type", "oauth2_flow") |
| 50 | + def _compute_redirect_url(self): |
| 51 | + get_param = self.env["ir.config_parameter"].sudo().get_param |
| 52 | + base_url = get_param("web.base.url") |
| 53 | + if base_url.startswith("http://") and not config["test_enable"]: |
| 54 | + _logger.warning( |
| 55 | + "web.base.url is configured in http. Oauth2 requires using https" |
| 56 | + ) |
| 57 | + base_url = base_url[len("http://") :] |
| 58 | + if not base_url.startswith("https://"): |
| 59 | + base_url = f"https://{base_url}" |
| 60 | + for rec in self: |
| 61 | + if rec.auth_type == "oauth2" and rec.oauth2_flow == "web_application": |
| 62 | + rec.redirect_url = f"{base_url}/webservice/{rec.id}/oauth2/redirect" |
| 63 | + else: |
| 64 | + rec.redirect_url = False |
| 65 | + |
| 66 | + def button_authorize(self): |
| 67 | + _logger.info("Button OAuth2 Authorize") |
| 68 | + authorize_url = self._get_adapter().redirect_to_authorize() |
| 69 | + _logger.info("Redirecting to %s", authorize_url) |
| 70 | + return { |
| 71 | + "type": "ir.actions.act_url", |
| 72 | + "url": authorize_url, |
| 73 | + "target": "self", |
| 74 | + } |
0 commit comments